setup.py 9.9 KB

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