setup.py 9.6 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, 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. 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_lz4_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. # allows us to build docs without the C modules fully loaded during help generation
  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. print('generating help for %s' % command)
  121. with open('docs/usage/%s.rst.inc' % command, 'w') as doc:
  122. if command == 'help':
  123. for topic in Archiver.helptext:
  124. params = {"topic": topic,
  125. "underline": '~' * len('borg help ' + topic)}
  126. doc.write(".. _borg_{topic}:\n\n".format(**params))
  127. doc.write("borg help {topic}\n{underline}\n::\n\n".format(**params))
  128. doc.write(Archiver.helptext[topic])
  129. else:
  130. params = {"command": command,
  131. "underline": '-' * len('borg ' + command)}
  132. doc.write(".. _borg_{command}:\n\n".format(**params))
  133. doc.write("borg {command}\n{underline}\n::\n\n".format(**params))
  134. epilog = parser.epilog
  135. parser.epilog = None
  136. doc.write(re.sub("^", " ", parser.format_help(), flags=re.M))
  137. doc.write("\nDescription\n~~~~~~~~~~~\n")
  138. doc.write(epilog)
  139. class build_api(Command):
  140. description = "generate a basic api.rst file based on the modules available"
  141. user_options = [
  142. ('output=', 'O', 'output directory'),
  143. ]
  144. def initialize_options(self):
  145. pass
  146. def finalize_options(self):
  147. pass
  148. def run(self):
  149. print("auto-generating API documentation")
  150. with open("docs/api.rst", "w") as doc:
  151. doc.write("""
  152. API Documentation
  153. =================
  154. """)
  155. for mod in glob('borg/*.py') + glob('borg/*.pyx'):
  156. print("examining module %s" % mod)
  157. mod = mod.replace('.pyx', '').replace('.py', '').replace('/', '.')
  158. if "._" not in mod:
  159. doc.write("""
  160. .. automodule:: %s
  161. :members:
  162. :undoc-members:
  163. """ % mod)
  164. cmdclass = {
  165. 'build_ext': build_ext,
  166. 'build_api': build_api,
  167. 'build_usage': build_usage,
  168. 'sdist': Sdist
  169. }
  170. ext_modules = []
  171. if not on_rtd:
  172. ext_modules += [
  173. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  174. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  175. Extension('borg.chunker', [chunker_source]),
  176. Extension('borg.hashindex', [hashindex_source])
  177. ]
  178. if sys.platform.startswith('linux'):
  179. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  180. elif sys.platform.startswith('freebsd'):
  181. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  182. elif sys.platform == 'darwin':
  183. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  184. setup(
  185. name='borgbackup',
  186. use_scm_version={
  187. 'write_to': 'borg/_version.py',
  188. },
  189. author='The Borg Collective (see AUTHORS file)',
  190. author_email='borgbackup@python.org',
  191. url='https://borgbackup.readthedocs.org/',
  192. description='Deduplicated, encrypted, authenticated and compressed backups',
  193. long_description=long_description,
  194. license='BSD',
  195. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  196. classifiers=[
  197. 'Development Status :: 4 - Beta',
  198. 'Environment :: Console',
  199. 'Intended Audience :: System Administrators',
  200. 'License :: OSI Approved :: BSD License',
  201. 'Operating System :: POSIX :: BSD :: FreeBSD',
  202. 'Operating System :: POSIX :: BSD :: OpenBSD',
  203. 'Operating System :: POSIX :: BSD :: NetBSD',
  204. 'Operating System :: MacOS :: MacOS X',
  205. 'Operating System :: POSIX :: Linux',
  206. 'Programming Language :: Python',
  207. 'Programming Language :: Python :: 3',
  208. 'Programming Language :: Python :: 3.2',
  209. 'Programming Language :: Python :: 3.3',
  210. 'Programming Language :: Python :: 3.4',
  211. 'Programming Language :: Python :: 3.5',
  212. 'Topic :: Security :: Cryptography',
  213. 'Topic :: System :: Archiving :: Backup',
  214. ],
  215. packages=['borg', 'borg.testsuite', 'borg.support', ],
  216. entry_points={
  217. 'console_scripts': [
  218. 'borg = borg.archiver:main',
  219. ]
  220. },
  221. cmdclass=cmdclass,
  222. ext_modules=ext_modules,
  223. setup_requires=['setuptools_scm>=1.7'],
  224. install_requires=install_requires,
  225. )