setup.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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', None) == 'True'
  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 all(os.path.exists(path) for path in [
  60. compress_source, crypto_source, chunker_source, hashindex_source,
  61. platform_linux_source, platform_freebsd_source]) and not on_rtd:
  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. import pdb
  109. print('generating usage docs')
  110. from borg.archiver import Archiver
  111. parser = Archiver().build_parser(prog='borg')
  112. choices = {}
  113. for action in parser._actions:
  114. if action.choices is not None:
  115. choices.update(action.choices)
  116. print('found commands: %s' % list(choices.keys()))
  117. if not os.path.exists('docs/usage'):
  118. os.mkdir('docs/usage')
  119. for command, parser in choices.items():
  120. if command is 'help':
  121. continue
  122. with open('docs/usage/%s.rst.inc' % command, 'w') as cmdfile:
  123. print('generating help for %s' % command)
  124. cmdfile.write(""".. _borg_{command}:
  125. borg {command}
  126. {underline}
  127. ::
  128. """.format(**{"command": command,
  129. "underline": '-' * len('borg ' + command)}))
  130. epilog = parser.epilog
  131. parser.epilog = None
  132. cmdfile.write(re.sub("^", " ", parser.format_help(), flags=re.M))
  133. cmdfile.write("""
  134. Description
  135. ~~~~~~~~~~~
  136. """)
  137. cmdfile.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 api:
  150. api.write("""
  151. Borg Backup API documentation"
  152. =============================
  153. """)
  154. for mod in glob('borg/*.py') + glob('borg/*.pyx'):
  155. print("examining module %s" % mod)
  156. if "/_" not in mod:
  157. api.write("""
  158. .. automodule:: %s
  159. :members:
  160. :undoc-members:
  161. """ % mod)
  162. # (function, predicate), see http://docs.python.org/2/distutils/apiref.html#distutils.cmd.Command.sub_commands
  163. build.sub_commands.append(('build_api', None))
  164. build.sub_commands.append(('build_usage', None))
  165. cmdclass = {
  166. 'build_ext': build_ext,
  167. 'build_api': build_api,
  168. 'build_usage': build_usage,
  169. 'sdist': Sdist
  170. }
  171. ext_modules = []
  172. if not on_rtd:
  173. ext_modules += [
  174. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  175. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  176. Extension('borg.chunker', [chunker_source]),
  177. Extension('borg.hashindex', [hashindex_source])
  178. ]
  179. if sys.platform.startswith('linux'):
  180. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  181. elif sys.platform.startswith('freebsd'):
  182. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  183. elif sys.platform == 'darwin':
  184. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  185. setup(
  186. name='borgbackup',
  187. use_scm_version={
  188. 'write_to': 'borg/_version.py',
  189. },
  190. author='The Borg Collective (see AUTHORS file)',
  191. author_email='borgbackup@librelist.com',
  192. url='https://borgbackup.github.io/',
  193. description='Deduplicated, encrypted, authenticated and compressed backups',
  194. long_description=long_description,
  195. license='BSD',
  196. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  197. classifiers=[
  198. 'Development Status :: 4 - Beta',
  199. 'Environment :: Console',
  200. 'Intended Audience :: System Administrators',
  201. 'License :: OSI Approved :: BSD License',
  202. 'Operating System :: POSIX :: BSD :: FreeBSD',
  203. 'Operating System :: POSIX :: BSD :: OpenBSD',
  204. 'Operating System :: POSIX :: BSD :: NetBSD',
  205. 'Operating System :: MacOS :: MacOS X',
  206. 'Operating System :: POSIX :: Linux',
  207. 'Programming Language :: Python',
  208. 'Programming Language :: Python :: 3',
  209. 'Programming Language :: Python :: 3.2',
  210. 'Programming Language :: Python :: 3.3',
  211. 'Programming Language :: Python :: 3.4',
  212. 'Programming Language :: Python :: 3.5',
  213. 'Topic :: Security :: Cryptography',
  214. 'Topic :: System :: Archiving :: Backup',
  215. ],
  216. packages=['borg', 'borg.testsuite', 'borg.support', ],
  217. entry_points={
  218. 'console_scripts': [
  219. 'borg = borg.archiver:main',
  220. ]
  221. },
  222. cmdclass=cmdclass,
  223. ext_modules=ext_modules,
  224. setup_requires=['setuptools_scm>=1.7'],
  225. install_requires=install_requires,
  226. )