setup.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # -*- encoding: utf-8 *-*
  2. import os
  3. import sys
  4. from glob import glob
  5. from distutils.command.build import build
  6. from distutils.core import Command
  7. from distutils.errors import DistutilsOptionError
  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', None) == 'True'
  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 all(os.path.exists(path) for path in [
  59. compress_source, crypto_source, chunker_source, hashindex_source,
  60. platform_linux_source, platform_freebsd_source]) and not on_rtd:
  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_openssl_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_api(Command):
  98. description = "generate a basic api.rst file based on the modules available"
  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("auto-generating API documentation")
  108. with open("docs/api.rst", "w") as api:
  109. api.write("""
  110. Borg Backup API documentation"
  111. =============================
  112. """)
  113. for mod in glob('borg/*.py') + glob('borg/*.pyx'):
  114. print("examining module %s" % mod)
  115. if "/_" not in mod:
  116. api.write("""
  117. .. automodule:: %s
  118. :members:
  119. :undoc-members:
  120. """ % mod)
  121. # (function, predicate), see http://docs.python.org/2/distutils/apiref.html#distutils.cmd.Command.sub_commands
  122. build.sub_commands.append(('build_api', None))
  123. cmdclass = {
  124. 'build_ext': build_ext,
  125. 'build_api': build_api,
  126. 'sdist': Sdist
  127. }
  128. ext_modules = []
  129. if not on_rtd:
  130. ext_modules += [
  131. Extension('borg.compress', [compress_source], libraries=['lz4'], include_dirs=include_dirs, library_dirs=library_dirs),
  132. Extension('borg.crypto', [crypto_source], libraries=['crypto'], include_dirs=include_dirs, library_dirs=library_dirs),
  133. Extension('borg.chunker', [chunker_source]),
  134. Extension('borg.hashindex', [hashindex_source])
  135. ]
  136. if sys.platform.startswith('linux'):
  137. ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
  138. elif sys.platform.startswith('freebsd'):
  139. ext_modules.append(Extension('borg.platform_freebsd', [platform_freebsd_source]))
  140. elif sys.platform == 'darwin':
  141. ext_modules.append(Extension('borg.platform_darwin', [platform_darwin_source]))
  142. setup(
  143. name='borgbackup',
  144. use_scm_version={
  145. 'write_to': 'borg/_version.py',
  146. },
  147. author='The Borg Collective (see AUTHORS file)',
  148. author_email='borgbackup@librelist.com',
  149. url='https://borgbackup.github.io/',
  150. description='Deduplicated, encrypted, authenticated and compressed backups',
  151. long_description=long_description,
  152. license='BSD',
  153. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  154. classifiers=[
  155. 'Development Status :: 4 - Beta',
  156. 'Environment :: Console',
  157. 'Intended Audience :: System Administrators',
  158. 'License :: OSI Approved :: BSD License',
  159. 'Operating System :: POSIX :: BSD :: FreeBSD',
  160. 'Operating System :: POSIX :: BSD :: OpenBSD',
  161. 'Operating System :: POSIX :: BSD :: NetBSD',
  162. 'Operating System :: MacOS :: MacOS X',
  163. 'Operating System :: POSIX :: Linux',
  164. 'Programming Language :: Python',
  165. 'Programming Language :: Python :: 3',
  166. 'Programming Language :: Python :: 3.2',
  167. 'Programming Language :: Python :: 3.3',
  168. 'Programming Language :: Python :: 3.4',
  169. 'Programming Language :: Python :: 3.5',
  170. 'Topic :: Security :: Cryptography',
  171. 'Topic :: System :: Archiving :: Backup',
  172. ],
  173. packages=['borg', 'borg.testsuite', 'borg.support', ],
  174. entry_points={
  175. 'console_scripts': [
  176. 'borg = borg.archiver:main',
  177. ]
  178. },
  179. cmdclass=cmdclass,
  180. ext_modules=ext_modules,
  181. setup_requires=['setuptools_scm>=1.7'],
  182. install_requires=install_requires,
  183. )