setup.py 12 KB

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