setup.py 12 KB

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