setup.py 9.8 KB

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