setup.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 choices.items():
  150. print('generating help for %s' % command)
  151. if self.generate_level(command + " ", parser, Archiver):
  152. break
  153. with open('docs/usage/%s.rst.inc' % command.replace(" ", "_"), 'w') as doc:
  154. doc.write(".. IMPORTANT: this file is auto-generated from borg's built-in help, do not edit!\n\n")
  155. if command == 'help':
  156. for topic in Archiver.helptext:
  157. params = {"topic": topic,
  158. "underline": '~' * len('borg help ' + topic)}
  159. doc.write(".. _borg_{topic}:\n\n".format(**params))
  160. doc.write("borg help {topic}\n{underline}\n\n".format(**params))
  161. doc.write(Archiver.helptext[topic])
  162. else:
  163. params = {"command": command,
  164. "command_": command.replace(' ', '_'),
  165. "underline": '-' * len('borg ' + command)}
  166. doc.write(".. _borg_{command_}:\n\n".format(**params))
  167. doc.write("borg {command}\n{underline}\n::\n\n".format(**params))
  168. epilog = parser.epilog
  169. parser.epilog = None
  170. doc.write(re.sub("^", " ", parser.format_help(), flags=re.M))
  171. doc.write("\nDescription\n~~~~~~~~~~~\n")
  172. doc.write(epilog)
  173. return is_subcommand
  174. class build_api(Command):
  175. description = "generate a basic api.rst file based on the modules available"
  176. user_options = [
  177. ('output=', 'O', 'output directory'),
  178. ]
  179. def initialize_options(self):
  180. pass
  181. def finalize_options(self):
  182. pass
  183. def run(self):
  184. print("auto-generating API documentation")
  185. with open("docs/api.rst", "w") as doc:
  186. doc.write("""
  187. API Documentation
  188. =================
  189. """)
  190. for mod in glob('borg/*.py') + glob('borg/*.pyx'):
  191. print("examining module %s" % mod)
  192. mod = mod.replace('.pyx', '').replace('.py', '').replace('/', '.')
  193. if "._" not in mod:
  194. doc.write("""
  195. .. automodule:: %s
  196. :members:
  197. :undoc-members:
  198. """ % mod)
  199. cmdclass = {
  200. 'build_ext': build_ext,
  201. 'build_api': build_api,
  202. 'build_usage': build_usage,
  203. 'sdist': Sdist
  204. }
  205. ext_modules = []
  206. if not on_rtd:
  207. ext_modules += [
  208. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  209. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  210. Extension('borg.chunker', [chunker_source]),
  211. Extension('borg.hashindex', [hashindex_source])
  212. ]
  213. if sys.platform == 'linux':
  214. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  215. elif sys.platform.startswith('freebsd'):
  216. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  217. elif sys.platform == 'darwin':
  218. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  219. setup(
  220. name='borgbackup',
  221. use_scm_version={
  222. 'write_to': '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 :: 4 - Beta',
  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.4',
  244. 'Programming Language :: Python :: 3.5',
  245. 'Topic :: Security :: Cryptography',
  246. 'Topic :: System :: Archiving :: Backup',
  247. ],
  248. packages=['borg', 'borg.testsuite', ],
  249. entry_points={
  250. 'console_scripts': [
  251. 'borg = borg.archiver:main',
  252. ]
  253. },
  254. cmdclass=cmdclass,
  255. ext_modules=ext_modules,
  256. setup_requires=['setuptools_scm>=1.7'],
  257. install_requires=install_requires,
  258. extras_require=extras_require,
  259. )