setup.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # -*- encoding: utf-8 *-*
  2. import os
  3. import io
  4. import re
  5. import sys
  6. from collections import OrderedDict
  7. from datetime import datetime
  8. from glob import glob
  9. try:
  10. import multiprocessing
  11. except ImportError:
  12. multiprocessing = None
  13. from distutils.command.clean import clean
  14. from setuptools.command.build_ext import build_ext
  15. from setuptools import setup, find_packages, Extension
  16. from setuptools.command.sdist import sdist
  17. try:
  18. from Cython.Build import cythonize
  19. except ImportError:
  20. cythonize = None
  21. import setup_lz4
  22. import setup_zstd
  23. import setup_b2
  24. import setup_docs
  25. # True: use the shared liblz4 (>= 1.7.0 / r129) from the system, False: use the bundled lz4 code
  26. prefer_system_liblz4 = True
  27. # True: use the shared libzstd (>= 1.3.0) from the system, False: use the bundled zstd code
  28. prefer_system_libzstd = True
  29. # True: use the shared libb2 from the system, False: use the bundled blake2 code
  30. prefer_system_libb2 = True
  31. cpu_threads = multiprocessing.cpu_count() if multiprocessing else 1
  32. # Are we building on ReadTheDocs?
  33. on_rtd = os.environ.get('READTHEDOCS')
  34. install_requires = [
  35. # we are rather picky about msgpack versions, because a good working msgpack is
  36. # very important for borg, see https://github.com/borgbackup/borg/issues/3753
  37. # as of now, 0.5.6 and 0.6.0 are the only preferred versions of msgpack:
  38. 'msgpack >=0.5.6, !=0.5.7, !=0.5.8, !=0.5.9, <=0.6.0',
  39. # if you can't satisfy the above requirement, these are versions that might
  40. # also work ok, IF you make sure to use the COMPILED version of msgpack-python,
  41. # NOT the PURE PYTHON fallback implementation: ==0.5.1, ==0.5.4
  42. #
  43. # Please note:
  44. # using any other version is not supported by borg development and
  45. # any feedback related to issues caused by this will be ignored.
  46. ]
  47. # note for package maintainers: if you package borgbackup for distribution,
  48. # please add llfuse as a *requirement* on all platforms that have a working
  49. # llfuse package. "borg mount" needs llfuse to work.
  50. # if you do not have llfuse, do not require it, most of borgbackup will work.
  51. extras_require = {
  52. # llfuse 1.x should work, llfuse 2.0 will break API
  53. 'fuse': [
  54. 'llfuse >=1.1, <2.0',
  55. 'llfuse >=1.3.4; python_version >="3.7"',
  56. ],
  57. }
  58. compress_source = 'src/borg/compress.pyx'
  59. crypto_ll_source = 'src/borg/crypto/low_level.pyx'
  60. crypto_helpers = 'src/borg/crypto/_crypto_helpers.c'
  61. chunker_source = 'src/borg/chunker.pyx'
  62. hashindex_source = 'src/borg/hashindex.pyx'
  63. item_source = 'src/borg/item.pyx'
  64. checksums_source = 'src/borg/algorithms/checksums.pyx'
  65. platform_posix_source = 'src/borg/platform/posix.pyx'
  66. platform_linux_source = 'src/borg/platform/linux.pyx'
  67. platform_darwin_source = 'src/borg/platform/darwin.pyx'
  68. platform_freebsd_source = 'src/borg/platform/freebsd.pyx'
  69. cython_sources = [
  70. compress_source,
  71. crypto_ll_source,
  72. chunker_source,
  73. hashindex_source,
  74. item_source,
  75. checksums_source,
  76. platform_posix_source,
  77. platform_linux_source,
  78. platform_freebsd_source,
  79. platform_darwin_source,
  80. ]
  81. if cythonize:
  82. Sdist = sdist
  83. else:
  84. class Sdist(sdist):
  85. def __init__(self, *args, **kwargs):
  86. raise Exception('Cython is required to run sdist')
  87. if not on_rtd and not all(os.path.exists(path) for path in [
  88. compress_source, crypto_ll_source, chunker_source, hashindex_source, item_source, checksums_source,
  89. platform_posix_source, platform_linux_source, platform_freebsd_source, platform_darwin_source]):
  90. raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
  91. def detect_openssl(prefixes):
  92. for prefix in prefixes:
  93. filename = os.path.join(prefix, 'include', 'openssl', 'evp.h')
  94. if os.path.exists(filename):
  95. with open(filename, 'rb') as fd:
  96. if b'PKCS5_PBKDF2_HMAC(' in fd.read():
  97. return prefix
  98. include_dirs = []
  99. library_dirs = []
  100. define_macros = []
  101. possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl',
  102. '/usr/local/borg', '/opt/local', '/opt/pkg', ]
  103. if os.environ.get('BORG_OPENSSL_PREFIX'):
  104. possible_openssl_prefixes.insert(0, os.environ.get('BORG_OPENSSL_PREFIX'))
  105. ssl_prefix = detect_openssl(possible_openssl_prefixes)
  106. if not ssl_prefix:
  107. raise Exception('Unable to find OpenSSL >= 1.0 headers. (Looked here: {})'.format(', '.join(possible_openssl_prefixes)))
  108. include_dirs.append(os.path.join(ssl_prefix, 'include'))
  109. library_dirs.append(os.path.join(ssl_prefix, 'lib'))
  110. possible_liblz4_prefixes = ['/usr', '/usr/local', '/usr/local/opt/lz4', '/usr/local/lz4',
  111. '/usr/local/borg', '/opt/local', '/opt/pkg', ]
  112. if os.environ.get('BORG_LIBLZ4_PREFIX'):
  113. possible_liblz4_prefixes.insert(0, os.environ.get('BORG_LIBLZ4_PREFIX'))
  114. liblz4_prefix = setup_lz4.lz4_system_prefix(possible_liblz4_prefixes)
  115. if prefer_system_liblz4 and liblz4_prefix:
  116. print('Detected and preferring liblz4 over bundled LZ4')
  117. define_macros.append(('BORG_USE_LIBLZ4', 'YES'))
  118. liblz4_system = True
  119. else:
  120. liblz4_system = False
  121. possible_libb2_prefixes = ['/usr', '/usr/local', '/usr/local/opt/libb2', '/usr/local/libb2',
  122. '/usr/local/borg', '/opt/local', '/opt/pkg', ]
  123. if os.environ.get('BORG_LIBB2_PREFIX'):
  124. possible_libb2_prefixes.insert(0, os.environ.get('BORG_LIBB2_PREFIX'))
  125. libb2_prefix = setup_b2.b2_system_prefix(possible_libb2_prefixes)
  126. if prefer_system_libb2 and libb2_prefix:
  127. print('Detected and preferring libb2 over bundled BLAKE2')
  128. define_macros.append(('BORG_USE_LIBB2', 'YES'))
  129. libb2_system = True
  130. else:
  131. libb2_system = False
  132. possible_libzstd_prefixes = ['/usr', '/usr/local', '/usr/local/opt/libzstd', '/usr/local/libzstd',
  133. '/usr/local/borg', '/opt/local', '/opt/pkg', ]
  134. if os.environ.get('BORG_LIBZSTD_PREFIX'):
  135. possible_libzstd_prefixes.insert(0, os.environ.get('BORG_LIBZSTD_PREFIX'))
  136. libzstd_prefix = setup_zstd.zstd_system_prefix(possible_libzstd_prefixes)
  137. if prefer_system_libzstd and libzstd_prefix:
  138. print('Detected and preferring libzstd over bundled ZSTD')
  139. define_macros.append(('BORG_USE_LIBZSTD', 'YES'))
  140. libzstd_system = True
  141. else:
  142. libzstd_system = False
  143. with open('README.rst', 'r') as fd:
  144. long_description = fd.read()
  145. # remove header, but have one \n before first headline
  146. start = long_description.find('What is BorgBackup?')
  147. assert start >= 0
  148. long_description = '\n' + long_description[start:]
  149. # remove badges
  150. long_description = re.compile(r'^\.\. start-badges.*^\.\. end-badges', re.M | re.S).sub('', long_description)
  151. # remove unknown directives
  152. long_description = re.compile(r'^\.\. highlight:: \w+$', re.M).sub('', long_description)
  153. def rm(file):
  154. try:
  155. os.unlink(file)
  156. print('rm', file)
  157. except FileNotFoundError:
  158. pass
  159. class Clean(clean):
  160. def run(self):
  161. super().run()
  162. for source in cython_sources:
  163. genc = source.replace('.pyx', '.c')
  164. rm(genc)
  165. compiled_glob = source.replace('.pyx', '.cpython*')
  166. for compiled in sorted(glob(compiled_glob)):
  167. rm(compiled)
  168. cmdclass = {
  169. 'build_ext': build_ext,
  170. 'build_usage': setup_docs.build_usage,
  171. 'build_man': setup_docs.build_man,
  172. 'sdist': Sdist,
  173. 'clean': Clean,
  174. }
  175. ext_modules = []
  176. if not on_rtd:
  177. compress_ext_kwargs = dict(sources=[compress_source], include_dirs=include_dirs, library_dirs=library_dirs,
  178. define_macros=define_macros)
  179. compress_ext_kwargs = setup_lz4.lz4_ext_kwargs(bundled_path='src/borg/algorithms/lz4',
  180. system_prefix=liblz4_prefix, system=liblz4_system,
  181. **compress_ext_kwargs)
  182. compress_ext_kwargs = setup_zstd.zstd_ext_kwargs(bundled_path='src/borg/algorithms/zstd',
  183. system_prefix=libzstd_prefix, system=libzstd_system,
  184. multithreaded=False, legacy=False, **compress_ext_kwargs)
  185. crypto_ext_kwargs = dict(sources=[crypto_ll_source, crypto_helpers], libraries=['crypto'],
  186. include_dirs=include_dirs, library_dirs=library_dirs, define_macros=define_macros)
  187. crypto_ext_kwargs = setup_b2.b2_ext_kwargs(bundled_path='src/borg/algorithms/blake2',
  188. system_prefix=libb2_prefix, system=libb2_system,
  189. **crypto_ext_kwargs)
  190. ext_modules += [
  191. Extension('borg.compress', **compress_ext_kwargs),
  192. Extension('borg.crypto.low_level', **crypto_ext_kwargs),
  193. Extension('borg.hashindex', [hashindex_source]),
  194. Extension('borg.item', [item_source]),
  195. Extension('borg.chunker', [chunker_source]),
  196. Extension('borg.algorithms.checksums', [checksums_source]),
  197. ]
  198. posix_ext = Extension('borg.platform.posix', [platform_posix_source])
  199. linux_ext = Extension('borg.platform.linux', [platform_linux_source], libraries=['acl'])
  200. freebsd_ext = Extension('borg.platform.freebsd', [platform_freebsd_source])
  201. darwin_ext = Extension('borg.platform.darwin', [platform_darwin_source])
  202. if not sys.platform.startswith(('win32', )):
  203. ext_modules.append(posix_ext)
  204. if sys.platform == 'linux':
  205. ext_modules.append(linux_ext)
  206. elif sys.platform.startswith('freebsd'):
  207. ext_modules.append(freebsd_ext)
  208. elif sys.platform == 'darwin':
  209. ext_modules.append(darwin_ext)
  210. # sometimes there's no need to cythonize
  211. # this breaks chained commands like 'clean sdist'
  212. cythonizing = len(sys.argv) > 1 and sys.argv[1] not in ('clean', 'egg_info', '--help-commands', '--version') \
  213. and '--help' not in sys.argv[1:]
  214. if cythonize and cythonizing:
  215. cython_opts = dict(
  216. # compile .pyx extensions to .c in parallel
  217. nthreads=cpu_threads + 1,
  218. # default language_level will be '3str' starting from Cython 3.0.0,
  219. # but old cython versions (< 0.29) do not know that, thus we use 3 for now.
  220. compiler_directives={'language_level': 3},
  221. )
  222. cythonize([posix_ext, linux_ext, freebsd_ext, darwin_ext], **cython_opts)
  223. ext_modules = cythonize(ext_modules, **cython_opts)
  224. setup(
  225. name='borgbackup',
  226. use_scm_version={
  227. 'write_to': 'src/borg/_version.py',
  228. },
  229. author='The Borg Collective (see AUTHORS file)',
  230. author_email='borgbackup@python.org',
  231. url='https://borgbackup.readthedocs.io/',
  232. description='Deduplicated, encrypted, authenticated and compressed backups',
  233. long_description=long_description,
  234. license='BSD',
  235. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  236. classifiers=[
  237. 'Development Status :: 2 - Pre-Alpha',
  238. 'Environment :: Console',
  239. 'Intended Audience :: System Administrators',
  240. 'License :: OSI Approved :: BSD License',
  241. 'Operating System :: POSIX :: BSD :: FreeBSD',
  242. 'Operating System :: POSIX :: BSD :: OpenBSD',
  243. 'Operating System :: POSIX :: BSD :: NetBSD',
  244. 'Operating System :: MacOS :: MacOS X',
  245. 'Operating System :: POSIX :: Linux',
  246. 'Programming Language :: Python',
  247. 'Programming Language :: Python :: 3',
  248. 'Programming Language :: Python :: 3.5',
  249. 'Programming Language :: Python :: 3.6',
  250. 'Programming Language :: Python :: 3.7',
  251. 'Topic :: Security :: Cryptography',
  252. 'Topic :: System :: Archiving :: Backup',
  253. ],
  254. packages=find_packages('src'),
  255. package_dir={'': 'src'},
  256. zip_safe=False,
  257. entry_points={
  258. 'console_scripts': [
  259. 'borg = borg.archiver:main',
  260. 'borgfs = borg.archiver:main',
  261. ]
  262. },
  263. # See also the MANIFEST.in file.
  264. # We want to install all the files in the package directories...
  265. include_package_data=True,
  266. # ...except the source files which have been compiled (C extensions):
  267. exclude_package_data={
  268. '': ['*.c', '*.h', '*.pyx', ],
  269. },
  270. cmdclass=cmdclass,
  271. ext_modules=ext_modules,
  272. setup_requires=['setuptools_scm>=1.7'],
  273. install_requires=install_requires,
  274. extras_require=extras_require,
  275. python_requires='>=3.5',
  276. )