setup.py 12 KB

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