setup.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/usr/bin/env python
  2. # This file is dual licensed under the terms of the Apache License, Version
  3. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  4. # for complete details.
  5. from __future__ import absolute_import, division, print_function
  6. import os
  7. import platform
  8. import subprocess
  9. import sys
  10. from distutils.command.build import build
  11. import pkg_resources
  12. from setuptools import find_packages, setup
  13. from setuptools.command.install import install
  14. from setuptools.command.test import test
  15. base_dir = os.path.dirname(__file__)
  16. src_dir = os.path.join(base_dir, "src")
  17. # When executing the setup.py, we need to be able to import ourselves, this
  18. # means that we need to add the src/ directory to the sys.path.
  19. sys.path.insert(0, src_dir)
  20. about = {}
  21. with open(os.path.join(src_dir, "cryptography", "__about__.py")) as f:
  22. exec(f.read(), about)
  23. VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
  24. requirements = [
  25. "idna>=2.1",
  26. "asn1crypto>=0.21.0",
  27. "six>=1.4.1",
  28. ]
  29. setup_requirements = []
  30. if sys.version_info < (3, 4):
  31. requirements.append("enum34")
  32. if sys.version_info < (3, 3):
  33. requirements.append("ipaddress")
  34. if platform.python_implementation() == "PyPy":
  35. if sys.pypy_version_info < (5, 3):
  36. raise RuntimeError(
  37. "cryptography 1.9 is not compatible with PyPy < 5.3. Please "
  38. "upgrade PyPy to use this library."
  39. )
  40. else:
  41. requirements.append("cffi>=1.7")
  42. setup_requirements.append("cffi>=1.7")
  43. test_requirements = [
  44. "pytest>=2.9.0",
  45. "pretend",
  46. "iso8601",
  47. "pytz",
  48. ]
  49. if sys.version_info[:2] > (2, 6):
  50. test_requirements.append("hypothesis>=1.11.4")
  51. # If there's no vectors locally that probably means we are in a tarball and
  52. # need to go and get the matching vectors package from PyPi
  53. if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
  54. test_requirements.append(VECTORS_DEPENDENCY)
  55. class PyTest(test):
  56. def finalize_options(self):
  57. test.finalize_options(self)
  58. self.test_args = []
  59. self.test_suite = True
  60. # This means there's a vectors/ folder with the package in here.
  61. # cd into it, install the vectors package and then refresh sys.path
  62. if VECTORS_DEPENDENCY not in test_requirements:
  63. subprocess.check_call(
  64. [sys.executable, "setup.py", "install"], cwd="vectors"
  65. )
  66. pkg_resources.get_distribution("cryptography_vectors").activate()
  67. def run_tests(self):
  68. # Import here because in module scope the eggs are not loaded.
  69. import pytest
  70. test_args = [os.path.join(base_dir, "tests")]
  71. errno = pytest.main(test_args)
  72. sys.exit(errno)
  73. def keywords_with_side_effects(argv):
  74. """
  75. Get a dictionary with setup keywords that (can) have side effects.
  76. :param argv: A list of strings with command line arguments.
  77. :returns: A dictionary with keyword arguments for the ``setup()`` function.
  78. This setup.py script uses the setuptools 'setup_requires' feature because
  79. this is required by the cffi package to compile extension modules. The
  80. purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
  81. build process as a result of setup.py invocations that don't need the cffi
  82. module to be built (setup.py serves the dual purpose of exposing package
  83. metadata).
  84. All of the options listed by ``python setup.py --help`` that print
  85. information should be recognized here. The commands ``clean``,
  86. ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
  87. Any combination of these options and commands is also supported.
  88. This function was originally based on the `setup.py script`_ of SciPy (see
  89. also the discussion in `pip issue #25`_).
  90. .. _pip issue #25: https://github.com/pypa/pip/issues/25
  91. .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
  92. """
  93. no_setup_requires_arguments = (
  94. '-h', '--help',
  95. '-n', '--dry-run',
  96. '-q', '--quiet',
  97. '-v', '--verbose',
  98. '-V', '--version',
  99. '--author',
  100. '--author-email',
  101. '--classifiers',
  102. '--contact',
  103. '--contact-email',
  104. '--description',
  105. '--egg-base',
  106. '--fullname',
  107. '--help-commands',
  108. '--keywords',
  109. '--licence',
  110. '--license',
  111. '--long-description',
  112. '--maintainer',
  113. '--maintainer-email',
  114. '--name',
  115. '--no-user-cfg',
  116. '--obsoletes',
  117. '--platforms',
  118. '--provides',
  119. '--requires',
  120. '--url',
  121. 'clean',
  122. 'egg_info',
  123. 'register',
  124. 'sdist',
  125. 'upload',
  126. )
  127. def is_short_option(argument):
  128. """Check whether a command line argument is a short option."""
  129. return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
  130. def expand_short_options(argument):
  131. """Expand combined short options into canonical short options."""
  132. return ('-' + char for char in argument[1:])
  133. def argument_without_setup_requirements(argv, i):
  134. """Check whether a command line argument needs setup requirements."""
  135. if argv[i] in no_setup_requires_arguments:
  136. # Simple case: An argument which is either an option or a command
  137. # which doesn't need setup requirements.
  138. return True
  139. elif (is_short_option(argv[i]) and
  140. all(option in no_setup_requires_arguments
  141. for option in expand_short_options(argv[i]))):
  142. # Not so simple case: Combined short options none of which need
  143. # setup requirements.
  144. return True
  145. elif argv[i - 1:i] == ['--egg-base']:
  146. # Tricky case: --egg-info takes an argument which should not make
  147. # us use setup_requires (defeating the purpose of this code).
  148. return True
  149. else:
  150. return False
  151. if all(argument_without_setup_requirements(argv, i)
  152. for i in range(1, len(argv))):
  153. return {
  154. "cmdclass": {
  155. "build": DummyBuild,
  156. "install": DummyInstall,
  157. "test": DummyPyTest,
  158. }
  159. }
  160. else:
  161. cffi_modules = [
  162. "src/_cffi_src/build_openssl.py:ffi",
  163. "src/_cffi_src/build_constant_time.py:ffi",
  164. "src/_cffi_src/build_padding.py:ffi",
  165. ]
  166. return {
  167. "setup_requires": setup_requirements,
  168. "cmdclass": {
  169. "test": PyTest,
  170. },
  171. "cffi_modules": cffi_modules
  172. }
  173. setup_requires_error = ("Requested setup command that needs 'setup_requires' "
  174. "while command line arguments implied a side effect "
  175. "free command or option.")
  176. class DummyBuild(build):
  177. """
  178. This class makes it very obvious when ``keywords_with_side_effects()`` has
  179. incorrectly interpreted the command line arguments to ``setup.py build`` as
  180. one of the 'side effect free' commands or options.
  181. """
  182. def run(self):
  183. raise RuntimeError(setup_requires_error)
  184. class DummyInstall(install):
  185. """
  186. This class makes it very obvious when ``keywords_with_side_effects()`` has
  187. incorrectly interpreted the command line arguments to ``setup.py install``
  188. as one of the 'side effect free' commands or options.
  189. """
  190. def run(self):
  191. raise RuntimeError(setup_requires_error)
  192. class DummyPyTest(test):
  193. """
  194. This class makes it very obvious when ``keywords_with_side_effects()`` has
  195. incorrectly interpreted the command line arguments to ``setup.py test`` as
  196. one of the 'side effect free' commands or options.
  197. """
  198. def run_tests(self):
  199. raise RuntimeError(setup_requires_error)
  200. with open(os.path.join(base_dir, "README.rst")) as f:
  201. long_description = f.read()
  202. setup(
  203. name=about["__title__"],
  204. version=about["__version__"],
  205. description=about["__summary__"],
  206. long_description=long_description,
  207. license=about["__license__"],
  208. url=about["__uri__"],
  209. author=about["__author__"],
  210. author_email=about["__email__"],
  211. classifiers=[
  212. "Intended Audience :: Developers",
  213. "License :: OSI Approved :: Apache Software License",
  214. "License :: OSI Approved :: BSD License",
  215. "Natural Language :: English",
  216. "Operating System :: MacOS :: MacOS X",
  217. "Operating System :: POSIX",
  218. "Operating System :: POSIX :: BSD",
  219. "Operating System :: POSIX :: Linux",
  220. "Operating System :: Microsoft :: Windows",
  221. "Programming Language :: Python",
  222. "Programming Language :: Python :: 2",
  223. "Programming Language :: Python :: 2.6",
  224. "Programming Language :: Python :: 2.7",
  225. "Programming Language :: Python :: 3",
  226. "Programming Language :: Python :: 3.4",
  227. "Programming Language :: Python :: 3.5",
  228. "Programming Language :: Python :: 3.6",
  229. "Programming Language :: Python :: Implementation :: CPython",
  230. "Programming Language :: Python :: Implementation :: PyPy",
  231. "Topic :: Security :: Cryptography",
  232. ],
  233. package_dir={"": "src"},
  234. packages=find_packages(where="src", exclude=["_cffi_src", "_cffi_src.*"]),
  235. include_package_data=True,
  236. install_requires=requirements,
  237. tests_require=test_requirements,
  238. extras_require={
  239. "test": test_requirements,
  240. "docstest": [
  241. "doc8",
  242. "pyenchant",
  243. "readme_renderer >= 16.0",
  244. "sphinx != 1.6.1, != 1.6.2, != 1.6.3",
  245. "sphinx_rtd_theme",
  246. "sphinxcontrib-spelling",
  247. ],
  248. "pep8test": [
  249. "flake8",
  250. "flake8-import-order",
  251. "pep8-naming",
  252. ],
  253. },
  254. # for cffi
  255. zip_safe=False,
  256. ext_package="cryptography.hazmat.bindings",
  257. **keywords_with_side_effects(sys.argv)
  258. )