setup.py 10 KB

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