blob: 593925e433d9b1416aec78c7cfed413da3bd8b9f [file] [log] [blame]
Dominic Chen9604d9e2015-10-10 05:13:31 +00001#!/usr/bin/env python
2
Alex Gaynor5951f462014-11-16 09:08:42 -08003# This file is dual licensed under the terms of the Apache License, Version
4# 2.0, and the BSD License. See the LICENSE file in the root of this repository
5# for complete details.
Alex Gaynorc37feed2014-03-08 08:32:56 -08006
7from __future__ import absolute_import, division, print_function
8
Alex Gaynorf51f2c12014-01-03 07:33:01 -08009import os
Terry Chia361545d2014-07-28 12:06:54 +080010import platform
Alex Stapletona39a3192014-03-14 20:03:12 +000011import subprocess
Alex Stapleton707b0082014-04-20 22:24:41 +010012import sys
Alex Stapleton4b610cc2014-03-22 08:49:35 +000013from distutils.command.build import build
Alex Stapletona39a3192014-03-14 20:03:12 +000014
15import pkg_resources
Alex Gaynor9a00f052014-01-02 13:09:34 -080016
Alex Gaynor4941fc52017-10-28 11:28:53 -040017import setuptools
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040018from setuptools import find_packages, setup
Sascha Peilickec5492052014-03-31 17:59:37 +020019from setuptools.command.install import install
Alex Gaynoracac6a62014-03-04 15:24:03 -080020from setuptools.command.test import test
Donald Stufft446a4572013-08-11 17:38:13 -040021
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040022
Alex Gaynor4941fc52017-10-28 11:28:53 -040023if (
24 pkg_resources.parse_version(setuptools.__version__) <
25 pkg_resources.parse_version("18.5")
26):
27 raise RuntimeError(
28 "cryptography requires setuptools 18.5 or newer, please upgrade to a "
29 "newer version of setuptools"
30 )
31
Alex Gaynor7630d6c2014-01-03 07:34:43 -080032base_dir = os.path.dirname(__file__)
Donald Stufftc62a78c2014-11-07 19:17:08 -050033src_dir = os.path.join(base_dir, "src")
34
35# When executing the setup.py, we need to be able to import ourselves, this
36# means that we need to add the src/ directory to the sys.path.
37sys.path.insert(0, src_dir)
Alex Gaynor7630d6c2014-01-03 07:34:43 -080038
Donald Stufft5f12a1b2013-08-11 16:37:43 -040039about = {}
Donald Stufftc62a78c2014-11-07 19:17:08 -050040with open(os.path.join(src_dir, "cryptography", "__about__.py")) as f:
Alex Gaynor7630d6c2014-01-03 07:34:43 -080041 exec(f.read(), about)
Donald Stufft5f12a1b2013-08-11 16:37:43 -040042
43
Alex Stapletona39a3192014-03-14 20:03:12 +000044VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
Donald Stufft5f12a1b2013-08-11 16:37:43 -040045
Alex Gaynore49bb152018-04-22 10:20:54 -040046setup_requirements = ["cffi>=1.7,!=1.11.3"]
Donald Stufft5f12a1b2013-08-11 16:37:43 -040047
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050048if platform.python_implementation() == "PyPy":
Alex Gaynor6091e112017-05-23 20:31:03 -070049 if sys.pypy_version_info < (5, 3):
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050050 raise RuntimeError(
Alex Gaynorba45d282018-04-08 16:39:08 -040051 "cryptography is not compatible with PyPy < 5.3. Please upgrade "
52 "PyPy to use this library."
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050053 )
Julian Berman4cf38112015-02-24 10:51:53 -050054
koobsff0dd1e2014-02-24 21:55:04 +110055test_requirements = [
Paul Kehrer9b086fd2017-11-28 21:49:32 +080056 "pytest>=3.2.1,!=3.3.0",
koobsff0dd1e2014-02-24 21:55:04 +110057 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000058 "iso8601",
InvalidInterrupt8e66ca62016-08-16 19:39:31 -070059 "pytz",
Paul Kehrer4cf6e782017-10-12 06:06:01 +080060 "hypothesis>=1.11.4",
koobsff0dd1e2014-02-24 21:55:04 +110061]
Alex Gaynorde3aa052016-02-19 07:33:56 -050062
koobsff0dd1e2014-02-24 21:55:04 +110063
Alex Stapletona39a3192014-03-14 20:03:12 +000064# If there's no vectors locally that probably means we are in a tarball and
65# need to go and get the matching vectors package from PyPi
66if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
67 test_requirements.append(VECTORS_DEPENDENCY)
68
Alex Gaynor9a00f052014-01-02 13:09:34 -080069
Alex Gaynoracac6a62014-03-04 15:24:03 -080070class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +110071 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -080072 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +110073 self.test_args = []
74 self.test_suite = True
koobs06671802014-02-24 22:33:07 +110075
Alex Stapletona39a3192014-03-14 20:03:12 +000076 # This means there's a vectors/ folder with the package in here.
77 # cd into it, install the vectors package and then refresh sys.path
78 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -070079 subprocess.check_call(
80 [sys.executable, "setup.py", "install"], cwd="vectors"
81 )
Alex Stapletona39a3192014-03-14 20:03:12 +000082 pkg_resources.get_distribution("cryptography_vectors").activate()
83
koobsff0dd1e2014-02-24 21:55:04 +110084 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +110085 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +110086 import pytest
Alex Gaynor8a58e692015-02-20 07:58:39 -080087 test_args = [os.path.join(base_dir, "tests")]
88 errno = pytest.main(test_args)
koobsff0dd1e2014-02-24 21:55:04 +110089 sys.exit(errno)
90
Alex Gaynor9a00f052014-01-02 13:09:34 -080091
Peter Odding51ec05f2014-07-12 01:18:35 +020092def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +020093 """
94 Get a dictionary with setup keywords that (can) have side effects.
95
Peter Odding51ec05f2014-07-12 01:18:35 +020096 :param argv: A list of strings with command line arguments.
97 :returns: A dictionary with keyword arguments for the ``setup()`` function.
98
Peter Oddingc9b83f72014-07-12 00:52:58 +020099 This setup.py script uses the setuptools 'setup_requires' feature because
100 this is required by the cffi package to compile extension modules. The
101 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200102 build process as a result of setup.py invocations that don't need the cffi
103 module to be built (setup.py serves the dual purpose of exposing package
104 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200105
Peter Oddinge9144562014-07-12 03:14:55 +0200106 All of the options listed by ``python setup.py --help`` that print
107 information should be recognized here. The commands ``clean``,
108 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
109 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200110
Peter Oddinge9144562014-07-12 03:14:55 +0200111 This function was originally based on the `setup.py script`_ of SciPy (see
112 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200113
114 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200115 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200116 """
Peter Oddinge9144562014-07-12 03:14:55 +0200117 no_setup_requires_arguments = (
118 '-h', '--help',
119 '-n', '--dry-run',
120 '-q', '--quiet',
121 '-v', '--verbose',
122 '-V', '--version',
123 '--author',
124 '--author-email',
125 '--classifiers',
126 '--contact',
127 '--contact-email',
128 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200129 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200130 '--fullname',
131 '--help-commands',
132 '--keywords',
133 '--licence',
134 '--license',
135 '--long-description',
136 '--maintainer',
137 '--maintainer-email',
138 '--name',
139 '--no-user-cfg',
140 '--obsoletes',
141 '--platforms',
142 '--provides',
143 '--requires',
144 '--url',
145 'clean',
146 'egg_info',
147 'register',
148 'sdist',
149 'upload',
150 )
Peter Odding97f45302014-07-14 21:40:35 +0200151
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200152 def is_short_option(argument):
153 """Check whether a command line argument is a short option."""
154 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200155
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200156 def expand_short_options(argument):
157 """Expand combined short options into canonical short options."""
158 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200159
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200160 def argument_without_setup_requirements(argv, i):
161 """Check whether a command line argument needs setup requirements."""
162 if argv[i] in no_setup_requires_arguments:
163 # Simple case: An argument which is either an option or a command
164 # which doesn't need setup requirements.
165 return True
Peter Odding97f45302014-07-14 21:40:35 +0200166 elif (is_short_option(argv[i]) and
167 all(option in no_setup_requires_arguments
168 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200169 # Not so simple case: Combined short options none of which need
170 # setup requirements.
171 return True
Peter Odding97f45302014-07-14 21:40:35 +0200172 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200173 # Tricky case: --egg-info takes an argument which should not make
174 # us use setup_requires (defeating the purpose of this code).
175 return True
176 else:
177 return False
Peter Odding97f45302014-07-14 21:40:35 +0200178
179 if all(argument_without_setup_requirements(argv, i)
180 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200181 return {
182 "cmdclass": {
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700183 "build": DummyBuild,
184 "install": DummyInstall,
Peter Odding3ae89a52014-07-12 02:06:56 +0200185 "test": DummyPyTest,
186 }
187 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200188 else:
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700189 cffi_modules = [
190 "src/_cffi_src/build_openssl.py:ffi",
191 "src/_cffi_src/build_constant_time.py:ffi",
192 "src/_cffi_src/build_padding.py:ffi",
193 ]
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700194
Peter Oddinge327cf12014-07-12 01:18:50 +0200195 return {
Paul Kehrer4c287d72015-06-08 00:05:53 -0500196 "setup_requires": setup_requirements,
Peter Oddinge327cf12014-07-12 01:18:50 +0200197 "cmdclass": {
Peter Oddinge327cf12014-07-12 01:18:50 +0200198 "test": PyTest,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700199 },
200 "cffi_modules": cffi_modules
Peter Oddinge327cf12014-07-12 01:18:50 +0200201 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200202
203
Peter Odding3ae89a52014-07-12 02:06:56 +0200204setup_requires_error = ("Requested setup command that needs 'setup_requires' "
205 "while command line arguments implied a side effect "
206 "free command or option.")
207
208
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700209class DummyBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200210 """
211 This class makes it very obvious when ``keywords_with_side_effects()`` has
212 incorrectly interpreted the command line arguments to ``setup.py build`` as
213 one of the 'side effect free' commands or options.
214 """
215
Peter Oddingdcce0802014-07-12 02:28:13 +0200216 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200217 raise RuntimeError(setup_requires_error)
218
219
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700220class DummyInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200221 """
222 This class makes it very obvious when ``keywords_with_side_effects()`` has
223 incorrectly interpreted the command line arguments to ``setup.py install``
224 as one of the 'side effect free' commands or options.
225 """
226
Peter Oddingdcce0802014-07-12 02:28:13 +0200227 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200228 raise RuntimeError(setup_requires_error)
229
230
Peter Oddingc9861f92014-07-13 04:17:20 +0200231class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200232 """
233 This class makes it very obvious when ``keywords_with_side_effects()`` has
234 incorrectly interpreted the command line arguments to ``setup.py test`` as
235 one of the 'side effect free' commands or options.
236 """
237
Peter Odding3ae89a52014-07-12 02:06:56 +0200238 def run_tests(self):
239 raise RuntimeError(setup_requires_error)
240
241
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800242with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800243 long_description = f.read()
244
245
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700246setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400247 name=about["__title__"],
248 version=about["__version__"],
249
250 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800251 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400252 license=about["__license__"],
253 url=about["__uri__"],
254
255 author=about["__author__"],
256 author_email=about["__email__"],
257
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200258 classifiers=[
Jon Dufresne95820b42018-06-16 18:33:33 -0700259 "Development Status :: 5 - Production/Stable",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200260 "Intended Audience :: Developers",
261 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700262 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200263 "Natural Language :: English",
264 "Operating System :: MacOS :: MacOS X",
265 "Operating System :: POSIX",
266 "Operating System :: POSIX :: BSD",
267 "Operating System :: POSIX :: Linux",
268 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200269 "Programming Language :: Python",
270 "Programming Language :: Python :: 2",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200271 "Programming Language :: Python :: 2.7",
272 "Programming Language :: Python :: 3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700273 "Programming Language :: Python :: 3.4",
Alex Gaynor1d5805b2015-09-17 20:57:44 -0400274 "Programming Language :: Python :: 3.5",
Alex Gaynor31b5d782016-12-23 12:20:36 -0500275 "Programming Language :: Python :: 3.6",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200276 "Programming Language :: Python :: Implementation :: CPython",
277 "Programming Language :: Python :: Implementation :: PyPy",
278 "Topic :: Security :: Cryptography",
279 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400280
Donald Stufftc62a78c2014-11-07 19:17:08 -0500281 package_dir={"": "src"},
Frazer McLean542ea8e2016-03-26 23:10:23 +0100282 packages=find_packages(where="src", exclude=["_cffi_src", "_cffi_src.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700283 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400284
Alex Gaynor0ed80b42017-12-26 13:04:31 -0500285 python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
286
Alex Gaynor4c41ab02017-08-23 20:33:01 -0400287 install_requires=[
288 "idna >= 2.1",
289 "asn1crypto >= 0.21.0",
290 "six >= 1.4.1",
Alex Gaynorca2055f2018-04-28 15:31:02 -0400291 ] + setup_requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100292 tests_require=test_requirements,
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500293 extras_require={
Alex Gaynor4c41ab02017-08-23 20:33:01 -0400294 ":python_version < '3'": ["enum34", "ipaddress"],
Alex Gaynor4c41ab02017-08-23 20:33:01 -0400295
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500296 "test": test_requirements,
Paul Kehrere3d2fc12018-03-05 20:50:10 -0400297 "docs": [
298 "sphinx >= 1.6.5",
299 "sphinx_rtd_theme",
300 ],
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500301 "docstest": [
Alex Gaynor94176082016-01-17 23:28:05 -0500302 "doc8",
Alex Gaynor0a8bdff2017-07-21 13:57:50 -0400303 "pyenchant >= 1.6.11",
Paul Kehrerf2691892016-12-09 16:31:15 -0600304 "readme_renderer >= 16.0",
Alex Gaynor30d35ae2017-10-23 19:09:27 -0400305 "sphinxcontrib-spelling >= 4.0.1",
Alex Gaynor94176082016-01-17 23:28:05 -0500306 ],
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500307 "pep8test": [
Alex Gaynor94176082016-01-17 23:28:05 -0500308 "flake8",
309 "flake8-import-order",
310 "pep8-naming",
311 ],
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500312 },
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400313
314 # for cffi
315 zip_safe=False,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700316 ext_package="cryptography.hazmat.bindings",
Peter Odding51ec05f2014-07-12 01:18:35 +0200317 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700318)