setup.py 9.4 KB

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