setup.py 12 KB

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