setup.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. class build_usage(Command):
  116. description = "generate usage for each command"
  117. user_options = [
  118. ('output=', 'O', 'output directory'),
  119. ]
  120. def initialize_options(self):
  121. pass
  122. def finalize_options(self):
  123. pass
  124. def run(self):
  125. print('generating usage docs')
  126. if not os.path.exists('docs/usage'):
  127. os.mkdir('docs/usage')
  128. # allows us to build docs without the C modules fully loaded during help generation
  129. from borg.archiver import Archiver
  130. parser = Archiver().build_parser(prog='borg')
  131. self.generate_level("", parser, Archiver)
  132. def generate_level(self, prefix, parser, Archiver):
  133. is_subcommand = False
  134. choices = {}
  135. for action in parser._actions:
  136. if action.choices is not None and 'SubParsersAction' in str(action.__class__):
  137. is_subcommand = True
  138. for cmd, parser in action.choices.items():
  139. choices[prefix + cmd] = parser
  140. if prefix and not choices:
  141. return
  142. print('found commands: %s' % list(choices.keys()))
  143. for command, parser in choices.items():
  144. print('generating help for %s' % command)
  145. if self.generate_level(command + " ", parser, Archiver):
  146. break
  147. with open('docs/usage/%s.rst.inc' % command.replace(" ", "_"), 'w') as doc:
  148. doc.write(".. IMPORTANT: this file is auto-generated from borg's built-in help, do not edit!\n\n")
  149. if command == 'help':
  150. for topic in Archiver.helptext:
  151. params = {"topic": topic,
  152. "underline": '~' * len('borg help ' + topic)}
  153. doc.write(".. _borg_{topic}:\n\n".format(**params))
  154. doc.write("borg help {topic}\n{underline}\n\n".format(**params))
  155. doc.write(Archiver.helptext[topic])
  156. else:
  157. params = {"command": command,
  158. "command_": command.replace(' ', '_'),
  159. "underline": '-' * len('borg ' + command)}
  160. doc.write(".. _borg_{command_}:\n\n".format(**params))
  161. doc.write("borg {command}\n{underline}\n::\n\n".format(**params))
  162. epilog = parser.epilog
  163. parser.epilog = None
  164. doc.write(re.sub("^", " ", parser.format_help(), flags=re.M))
  165. doc.write("\nDescription\n~~~~~~~~~~~\n")
  166. doc.write(epilog)
  167. return is_subcommand
  168. class build_api(Command):
  169. description = "generate a basic api.rst file based on the modules available"
  170. user_options = [
  171. ('output=', 'O', 'output directory'),
  172. ]
  173. def initialize_options(self):
  174. pass
  175. def finalize_options(self):
  176. pass
  177. def run(self):
  178. print("auto-generating API documentation")
  179. with open("docs/api.rst", "w") as doc:
  180. doc.write("""
  181. API Documentation
  182. =================
  183. """)
  184. for mod in glob('borg/*.py') + glob('borg/*.pyx'):
  185. print("examining module %s" % mod)
  186. mod = mod.replace('.pyx', '').replace('.py', '').replace('/', '.')
  187. if "._" not in mod:
  188. doc.write("""
  189. .. automodule:: %s
  190. :members:
  191. :undoc-members:
  192. """ % mod)
  193. cmdclass = {
  194. 'build_ext': build_ext,
  195. 'build_api': build_api,
  196. 'build_usage': build_usage,
  197. 'sdist': Sdist
  198. }
  199. ext_modules = []
  200. if not on_rtd:
  201. ext_modules += [
  202. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  203. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  204. Extension('borg.chunker', [chunker_source]),
  205. Extension('borg.hashindex', [hashindex_source])
  206. ]
  207. if sys.platform == 'linux':
  208. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  209. elif sys.platform.startswith('freebsd'):
  210. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  211. elif sys.platform == 'darwin':
  212. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  213. setup(
  214. name='borgbackup',
  215. use_scm_version={
  216. 'write_to': 'borg/_version.py',
  217. },
  218. author='The Borg Collective (see AUTHORS file)',
  219. author_email='borgbackup@python.org',
  220. url='https://borgbackup.readthedocs.io/',
  221. description='Deduplicated, encrypted, authenticated and compressed backups',
  222. long_description=long_description,
  223. license='BSD',
  224. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  225. classifiers=[
  226. 'Development Status :: 4 - Beta',
  227. 'Environment :: Console',
  228. 'Intended Audience :: System Administrators',
  229. 'License :: OSI Approved :: BSD License',
  230. 'Operating System :: POSIX :: BSD :: FreeBSD',
  231. 'Operating System :: POSIX :: BSD :: OpenBSD',
  232. 'Operating System :: POSIX :: BSD :: NetBSD',
  233. 'Operating System :: MacOS :: MacOS X',
  234. 'Operating System :: POSIX :: Linux',
  235. 'Programming Language :: Python',
  236. 'Programming Language :: Python :: 3',
  237. 'Programming Language :: Python :: 3.4',
  238. 'Programming Language :: Python :: 3.5',
  239. 'Topic :: Security :: Cryptography',
  240. 'Topic :: System :: Archiving :: Backup',
  241. ],
  242. packages=['borg', 'borg.testsuite', ],
  243. entry_points={
  244. 'console_scripts': [
  245. 'borg = borg.archiver:main',
  246. ]
  247. },
  248. cmdclass=cmdclass,
  249. ext_modules=ext_modules,
  250. setup_requires=['setuptools_scm>=1.7'],
  251. install_requires=install_requires,
  252. extras_require=extras_require,
  253. )