setup.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # -*- encoding: utf-8 *-*
  2. import os
  3. import re
  4. import sys
  5. from glob import glob
  6. from distutils.command.build import build
  7. from distutils.core import Command
  8. min_python = (3, 4)
  9. my_python = sys.version_info
  10. if my_python < min_python:
  11. print("Borg requires Python %d.%d or later" % min_python)
  12. sys.exit(1)
  13. # Are we building on ReadTheDocs?
  14. on_rtd = os.environ.get('READTHEDOCS')
  15. install_requires = [
  16. # msgpack pure python data corruption was fixed in 0.4.6.
  17. # msgpack 0.5.0 was a bit of a troublemaker.
  18. # also, msgpack dropped py34 support at 0.5.0.
  19. 'msgpack-python>=0.4.6,<0.5.0',
  20. ]
  21. # note for package maintainers: if you package borgbackup for distribution,
  22. # please add llfuse as a *requirement* on all platforms that have a working
  23. # llfuse package. "borg mount" needs llfuse to work.
  24. # if you do not have llfuse, do not require it, most of borgbackup will work.
  25. extras_require = {
  26. # llfuse 0.40 (tested, proven, ok), needs FUSE version >= 2.8.0
  27. # llfuse 0.41 (tested shortly, looks ok), needs FUSE version >= 2.8.0
  28. # llfuse 0.41.1 (tested shortly, looks ok), needs FUSE version >= 2.8.0
  29. # llfuse 0.42 (tested shortly, looks ok), needs FUSE version >= 2.8.0
  30. # llfuse 1.0 (tested shortly, looks ok), needs FUSE version >= 2.8.0
  31. # llfuse 1.1.1 (tested shortly, looks ok), needs FUSE version >= 2.8.0
  32. # llfuse 2.0 will break API
  33. 'fuse': ['llfuse<2.0', ],
  34. }
  35. if sys.platform.startswith('freebsd'):
  36. # llfuse was frequently broken / did not build on freebsd
  37. # llfuse 0.41.1, 1.1 are ok
  38. extras_require['fuse'] = ['llfuse <2.0, !=0.42.*, !=0.43, !=1.0', ]
  39. from setuptools import setup, Extension
  40. from setuptools.command.sdist import sdist
  41. compress_source = 'borg/compress.pyx'
  42. crypto_source = 'borg/crypto.pyx'
  43. chunker_source = 'borg/chunker.pyx'
  44. hashindex_source = 'borg/hashindex.pyx'
  45. platform_linux_source = 'borg/platform_linux.pyx'
  46. platform_darwin_source = 'borg/platform_darwin.pyx'
  47. platform_freebsd_source = 'borg/platform_freebsd.pyx'
  48. try:
  49. from Cython.Distutils import build_ext
  50. import Cython.Compiler.Main as cython_compiler
  51. class Sdist(sdist):
  52. def __init__(self, *args, **kwargs):
  53. for src in glob('borg/*.pyx'):
  54. cython_compiler.compile(src, cython_compiler.default_options)
  55. super().__init__(*args, **kwargs)
  56. def make_distribution(self):
  57. self.filelist.extend([
  58. 'borg/compress.c',
  59. 'borg/crypto.c',
  60. 'borg/chunker.c', 'borg/_chunker.c',
  61. 'borg/hashindex.c', 'borg/_hashindex.c',
  62. 'borg/platform_linux.c',
  63. 'borg/platform_freebsd.c',
  64. 'borg/platform_darwin.c',
  65. ])
  66. super().make_distribution()
  67. except ImportError:
  68. class Sdist(sdist):
  69. def __init__(self, *args, **kwargs):
  70. raise Exception('Cython is required to run sdist')
  71. compress_source = compress_source.replace('.pyx', '.c')
  72. crypto_source = crypto_source.replace('.pyx', '.c')
  73. chunker_source = chunker_source.replace('.pyx', '.c')
  74. hashindex_source = hashindex_source.replace('.pyx', '.c')
  75. platform_linux_source = platform_linux_source.replace('.pyx', '.c')
  76. platform_freebsd_source = platform_freebsd_source.replace('.pyx', '.c')
  77. platform_darwin_source = platform_darwin_source.replace('.pyx', '.c')
  78. from distutils.command.build_ext import build_ext
  79. if not on_rtd and not all(os.path.exists(path) for path in [
  80. compress_source, crypto_source, chunker_source, hashindex_source,
  81. platform_linux_source, platform_freebsd_source, platform_darwin_source]):
  82. raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
  83. def detect_openssl(prefixes):
  84. for prefix in prefixes:
  85. filename = os.path.join(prefix, 'include', 'openssl', 'evp.h')
  86. if os.path.exists(filename):
  87. with open(filename, 'rb') as fd:
  88. if b'PKCS5_PBKDF2_HMAC(' in fd.read():
  89. return prefix
  90. def detect_lz4(prefixes):
  91. for prefix in prefixes:
  92. filename = os.path.join(prefix, 'include', 'lz4.h')
  93. if os.path.exists(filename):
  94. with open(filename, 'rb') as fd:
  95. if b'LZ4_decompress_safe' in fd.read():
  96. return prefix
  97. include_dirs = []
  98. library_dirs = []
  99. possible_openssl_prefixes = ['/usr', '/usr/local', '/usr/local/opt/openssl', '/usr/local/ssl', '/usr/local/openssl', '/usr/local/borg', '/opt/local']
  100. if os.environ.get('BORG_OPENSSL_PREFIX'):
  101. possible_openssl_prefixes.insert(0, os.environ.get('BORG_OPENSSL_PREFIX'))
  102. ssl_prefix = detect_openssl(possible_openssl_prefixes)
  103. if not ssl_prefix:
  104. raise Exception('Unable to find OpenSSL >= 1.0 headers. (Looked here: {})'.format(', '.join(possible_openssl_prefixes)))
  105. include_dirs.append(os.path.join(ssl_prefix, 'include'))
  106. library_dirs.append(os.path.join(ssl_prefix, 'lib'))
  107. possible_lz4_prefixes = ['/usr', '/usr/local', '/usr/local/opt/lz4', '/usr/local/lz4', '/usr/local/borg', '/opt/local']
  108. if os.environ.get('BORG_LZ4_PREFIX'):
  109. possible_lz4_prefixes.insert(0, os.environ.get('BORG_LZ4_PREFIX'))
  110. lz4_prefix = detect_lz4(possible_lz4_prefixes)
  111. if lz4_prefix:
  112. include_dirs.append(os.path.join(lz4_prefix, 'include'))
  113. library_dirs.append(os.path.join(lz4_prefix, 'lib'))
  114. elif not on_rtd:
  115. raise Exception('Unable to find LZ4 headers. (Looked here: {})'.format(', '.join(possible_lz4_prefixes)))
  116. with open('README.rst', 'r') as fd:
  117. long_description = fd.read()
  118. # remove badges
  119. long_description = re.compile(r'^\.\. start-badges.*^\.\. end-badges', re.M | re.S).sub('', long_description)
  120. # remove |substitutions|
  121. long_description = re.compile(r'\|screencast\|').sub('', long_description)
  122. # remove unknown directives
  123. long_description = re.compile(r'^\.\. highlight:: \w+$', re.M).sub('', long_description)
  124. class build_usage(Command):
  125. description = "generate usage for each command"
  126. user_options = [
  127. ('output=', 'O', 'output directory'),
  128. ]
  129. def initialize_options(self):
  130. pass
  131. def finalize_options(self):
  132. pass
  133. def run(self):
  134. print('generating usage docs')
  135. if not os.path.exists('docs/usage'):
  136. os.mkdir('docs/usage')
  137. # allows us to build docs without the C modules fully loaded during help generation
  138. from borg.archiver import Archiver
  139. parser = Archiver().build_parser(prog='borg')
  140. self.generate_level("", parser, Archiver)
  141. def generate_level(self, prefix, parser, Archiver):
  142. is_subcommand = False
  143. choices = {}
  144. for action in parser._actions:
  145. if action.choices is not None and 'SubParsersAction' in str(action.__class__):
  146. is_subcommand = True
  147. for cmd, parser in action.choices.items():
  148. choices[prefix + cmd] = parser
  149. if prefix and not choices:
  150. return
  151. print('found commands: %s' % list(choices.keys()))
  152. for command, parser in sorted(choices.items()):
  153. if command.startswith('debug'):
  154. print('skipping', command)
  155. continue
  156. print('generating help for %s' % command)
  157. if self.generate_level(command + " ", parser, Archiver):
  158. continue
  159. with open('docs/usage/%s.rst.inc' % command.replace(" ", "_"), 'w') as doc:
  160. doc.write(".. IMPORTANT: this file is auto-generated from borg's built-in help, do not edit!\n\n")
  161. if command == 'help':
  162. for topic in Archiver.helptext:
  163. params = {"topic": topic,
  164. "underline": '~' * len('borg help ' + topic)}
  165. doc.write(".. _borg_{topic}:\n\n".format(**params))
  166. doc.write("borg help {topic}\n{underline}\n\n".format(**params))
  167. doc.write(Archiver.helptext[topic])
  168. else:
  169. params = {"command": command,
  170. "command_": command.replace(' ', '_'),
  171. "underline": '-' * len('borg ' + command)}
  172. doc.write(".. _borg_{command_}:\n\n".format(**params))
  173. doc.write("borg {command}\n{underline}\n::\n\n".format(**params))
  174. epilog = parser.epilog
  175. parser.epilog = None
  176. doc.write(re.sub("^", " ", parser.format_help(), flags=re.M))
  177. doc.write("\nDescription\n~~~~~~~~~~~\n")
  178. doc.write(epilog)
  179. return is_subcommand
  180. cmdclass = {
  181. 'build_ext': build_ext,
  182. 'build_usage': build_usage,
  183. 'sdist': Sdist
  184. }
  185. ext_modules = []
  186. if not on_rtd:
  187. ext_modules += [
  188. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  189. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  190. Extension('borg.chunker', [chunker_source]),
  191. Extension('borg.hashindex', [hashindex_source])
  192. ]
  193. if sys.platform == 'linux':
  194. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  195. elif sys.platform.startswith('freebsd'):
  196. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  197. elif sys.platform == 'darwin':
  198. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  199. setup(
  200. name='borgbackup',
  201. use_scm_version={
  202. 'write_to': 'borg/_version.py',
  203. },
  204. author='The Borg Collective (see AUTHORS file)',
  205. author_email='borgbackup@python.org',
  206. url='https://borgbackup.readthedocs.io/',
  207. description='Deduplicated, encrypted, authenticated and compressed backups',
  208. long_description=long_description,
  209. license='BSD',
  210. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  211. classifiers=[
  212. 'Development Status :: 4 - Beta',
  213. 'Environment :: Console',
  214. 'Intended Audience :: System Administrators',
  215. 'License :: OSI Approved :: BSD License',
  216. 'Operating System :: POSIX :: BSD :: FreeBSD',
  217. 'Operating System :: POSIX :: BSD :: OpenBSD',
  218. 'Operating System :: POSIX :: BSD :: NetBSD',
  219. 'Operating System :: MacOS :: MacOS X',
  220. 'Operating System :: POSIX :: Linux',
  221. 'Programming Language :: Python',
  222. 'Programming Language :: Python :: 3',
  223. 'Programming Language :: Python :: 3.4',
  224. 'Programming Language :: Python :: 3.5',
  225. 'Programming Language :: Python :: 3.6',
  226. 'Topic :: Security :: Cryptography',
  227. 'Topic :: System :: Archiving :: Backup',
  228. ],
  229. packages=['borg', 'borg.testsuite', ],
  230. entry_points={
  231. 'console_scripts': [
  232. 'borg = borg.archiver:main',
  233. ]
  234. },
  235. include_package_data=True,
  236. package_data={
  237. 'borg': ['paperkey.html'],
  238. 'borg.testsuite': ['attic.tar.gz'],
  239. },
  240. cmdclass=cmdclass,
  241. ext_modules=ext_modules,
  242. setup_requires=['setuptools_scm>=1.7'],
  243. install_requires=install_requires,
  244. extras_require=extras_require,
  245. )