setup.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 sys
  9. from distutils.command.build import build
  10. import pkg_resources
  11. import setuptools
  12. from setuptools import find_packages, setup
  13. from setuptools.command.install import install
  14. if (
  15. pkg_resources.parse_version(setuptools.__version__) <
  16. pkg_resources.parse_version("18.5")
  17. ):
  18. raise RuntimeError(
  19. "cryptography requires setuptools 18.5 or newer, please upgrade to a "
  20. "newer version of setuptools"
  21. )
  22. base_dir = os.path.dirname(__file__)
  23. src_dir = os.path.join(base_dir, "src")
  24. # When executing the setup.py, we need to be able to import ourselves, this
  25. # means that we need to add the src/ directory to the sys.path.
  26. sys.path.insert(0, src_dir)
  27. about = {}
  28. with open(os.path.join(src_dir, "cryptography", "__about__.py")) as f:
  29. exec(f.read(), about)
  30. # `setup_requirements` must be kept in sync with `pyproject.toml`
  31. setup_requirements = ["cffi>=1.8,!=1.11.3"]
  32. if platform.python_implementation() == "PyPy":
  33. if sys.pypy_version_info < (5, 4):
  34. raise RuntimeError(
  35. "cryptography is not compatible with PyPy < 5.4. Please upgrade "
  36. "PyPy to use this library."
  37. )
  38. def keywords_with_side_effects(argv):
  39. """
  40. Get a dictionary with setup keywords that (can) have side effects.
  41. :param argv: A list of strings with command line arguments.
  42. :returns: A dictionary with keyword arguments for the ``setup()`` function.
  43. This setup.py script uses the setuptools 'setup_requires' feature because
  44. this is required by the cffi package to compile extension modules. The
  45. purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
  46. build process as a result of setup.py invocations that don't need the cffi
  47. module to be built (setup.py serves the dual purpose of exposing package
  48. metadata).
  49. All of the options listed by ``python setup.py --help`` that print
  50. information should be recognized here. The commands ``clean``,
  51. ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
  52. Any combination of these options and commands is also supported.
  53. This function was originally based on the `setup.py script`_ of SciPy (see
  54. also the discussion in `pip issue #25`_).
  55. .. _pip issue #25: https://github.com/pypa/pip/issues/25
  56. .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
  57. """
  58. no_setup_requires_arguments = (
  59. '-h', '--help',
  60. '-n', '--dry-run',
  61. '-q', '--quiet',
  62. '-v', '--verbose',
  63. '-V', '--version',
  64. '--author',
  65. '--author-email',
  66. '--classifiers',
  67. '--contact',
  68. '--contact-email',
  69. '--description',
  70. '--egg-base',
  71. '--fullname',
  72. '--help-commands',
  73. '--keywords',
  74. '--licence',
  75. '--license',
  76. '--long-description',
  77. '--maintainer',
  78. '--maintainer-email',
  79. '--name',
  80. '--no-user-cfg',
  81. '--obsoletes',
  82. '--platforms',
  83. '--provides',
  84. '--requires',
  85. '--url',
  86. 'clean',
  87. 'egg_info',
  88. 'register',
  89. 'sdist',
  90. 'upload',
  91. )
  92. def is_short_option(argument):
  93. """Check whether a command line argument is a short option."""
  94. return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
  95. def expand_short_options(argument):
  96. """Expand combined short options into canonical short options."""
  97. return ('-' + char for char in argument[1:])
  98. def argument_without_setup_requirements(argv, i):
  99. """Check whether a command line argument needs setup requirements."""
  100. if argv[i] in no_setup_requires_arguments:
  101. # Simple case: An argument which is either an option or a command
  102. # which doesn't need setup requirements.
  103. return True
  104. elif (is_short_option(argv[i]) and
  105. all(option in no_setup_requires_arguments
  106. for option in expand_short_options(argv[i]))):
  107. # Not so simple case: Combined short options none of which need
  108. # setup requirements.
  109. return True
  110. elif argv[i - 1:i] == ['--egg-base']:
  111. # Tricky case: --egg-info takes an argument which should not make
  112. # us use setup_requires (defeating the purpose of this code).
  113. return True
  114. else:
  115. return False
  116. if all(argument_without_setup_requirements(argv, i)
  117. for i in range(1, len(argv))):
  118. return {
  119. "cmdclass": {
  120. "build": DummyBuild,
  121. "install": DummyInstall,
  122. }
  123. }
  124. else:
  125. cffi_modules = [
  126. "src/_cffi_src/build_openssl.py:ffi",
  127. "src/_cffi_src/build_constant_time.py:ffi",
  128. "src/_cffi_src/build_padding.py:ffi",
  129. ]
  130. return {
  131. "setup_requires": setup_requirements,
  132. "cffi_modules": cffi_modules
  133. }
  134. setup_requires_error = ("Requested setup command that needs 'setup_requires' "
  135. "while command line arguments implied a side effect "
  136. "free command or option.")
  137. class DummyBuild(build):
  138. """
  139. This class makes it very obvious when ``keywords_with_side_effects()`` has
  140. incorrectly interpreted the command line arguments to ``setup.py build`` as
  141. one of the 'side effect free' commands or options.
  142. """
  143. def run(self):
  144. raise RuntimeError(setup_requires_error)
  145. class DummyInstall(install):
  146. """
  147. This class makes it very obvious when ``keywords_with_side_effects()`` has
  148. incorrectly interpreted the command line arguments to ``setup.py install``
  149. as one of the 'side effect free' commands or options.
  150. """
  151. def run(self):
  152. raise RuntimeError(setup_requires_error)
  153. with open(os.path.join(base_dir, "README.rst")) as f:
  154. long_description = f.read()
  155. setup(
  156. name=about["__title__"],
  157. version=about["__version__"],
  158. description=about["__summary__"],
  159. long_description=long_description,
  160. long_description_content_type="text/x-rst",
  161. license=about["__license__"],
  162. url=about["__uri__"],
  163. author=about["__author__"],
  164. author_email=about["__email__"],
  165. classifiers=[
  166. "Development Status :: 5 - Production/Stable",
  167. "Intended Audience :: Developers",
  168. "License :: OSI Approved :: Apache Software License",
  169. "License :: OSI Approved :: BSD License",
  170. "Natural Language :: English",
  171. "Operating System :: MacOS :: MacOS X",
  172. "Operating System :: POSIX",
  173. "Operating System :: POSIX :: BSD",
  174. "Operating System :: POSIX :: Linux",
  175. "Operating System :: Microsoft :: Windows",
  176. "Programming Language :: Python",
  177. "Programming Language :: Python :: 2",
  178. "Programming Language :: Python :: 2.7",
  179. "Programming Language :: Python :: 3",
  180. "Programming Language :: Python :: 3.5",
  181. "Programming Language :: Python :: 3.6",
  182. "Programming Language :: Python :: 3.7",
  183. "Programming Language :: Python :: 3.8",
  184. "Programming Language :: Python :: Implementation :: CPython",
  185. "Programming Language :: Python :: Implementation :: PyPy",
  186. "Topic :: Security :: Cryptography",
  187. ],
  188. package_dir={"": "src"},
  189. packages=find_packages(where="src", exclude=["_cffi_src", "_cffi_src.*"]),
  190. include_package_data=True,
  191. python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
  192. install_requires=[
  193. "six >= 1.4.1",
  194. ] + setup_requirements,
  195. extras_require={
  196. ":python_version < '3'": ["enum34", "ipaddress"],
  197. "test": [
  198. "pytest>=3.6.0,!=3.9.0,!=3.9.1,!=3.9.2",
  199. "pretend",
  200. "iso8601",
  201. "pytz",
  202. "hypothesis>=1.11.4,!=3.79.2",
  203. ],
  204. "docs": [
  205. "sphinx >= 1.6.5,!=1.8.0",
  206. "sphinx_rtd_theme",
  207. ],
  208. "docstest": [
  209. "doc8",
  210. "pyenchant >= 1.6.11",
  211. "twine >= 1.12.0",
  212. "sphinxcontrib-spelling >= 4.0.1",
  213. ],
  214. "pep8test": [
  215. "flake8",
  216. "flake8-import-order",
  217. "pep8-naming",
  218. ],
  219. # This extra is for the U-label support that was deprecated in
  220. # cryptography 2.1. If you need this deprecated path install with
  221. # pip install cryptography[idna]
  222. "idna": [
  223. "idna >= 2.1",
  224. ]
  225. },
  226. # for cffi
  227. zip_safe=False,
  228. ext_package="cryptography.hazmat.bindings",
  229. **keywords_with_side_effects(sys.argv)
  230. )