setup.py 9.4 KB

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