blob: 692313fb02a99c70dc19ab88c37840d219882b55 [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
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040017from setuptools import find_packages, setup
Sascha Peilickec5492052014-03-31 17:59:37 +020018from setuptools.command.install import install
Alex Gaynoracac6a62014-03-04 15:24:03 -080019from setuptools.command.test import test
Donald Stufft446a4572013-08-11 17:38:13 -040020
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040021
Alex Gaynor7630d6c2014-01-03 07:34:43 -080022base_dir = os.path.dirname(__file__)
Donald Stufftc62a78c2014-11-07 19:17:08 -050023src_dir = os.path.join(base_dir, "src")
24
25# When executing the setup.py, we need to be able to import ourselves, this
26# means that we need to add the src/ directory to the sys.path.
27sys.path.insert(0, src_dir)
Alex Gaynor7630d6c2014-01-03 07:34:43 -080028
Donald Stufft5f12a1b2013-08-11 16:37:43 -040029about = {}
Donald Stufftc62a78c2014-11-07 19:17:08 -050030with open(os.path.join(src_dir, "cryptography", "__about__.py")) as f:
Alex Gaynor7630d6c2014-01-03 07:34:43 -080031 exec(f.read(), about)
Donald Stufft5f12a1b2013-08-11 16:37:43 -040032
33
Alex Stapletona39a3192014-03-14 20:03:12 +000034VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
Donald Stufft5f12a1b2013-08-11 16:37:43 -040035
Paul Kehrer4c287d72015-06-08 00:05:53 -050036setup_requirements = []
Donald Stufft5f12a1b2013-08-11 16:37:43 -040037
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050038if platform.python_implementation() == "PyPy":
Alex Gaynor6091e112017-05-23 20:31:03 -070039 if sys.pypy_version_info < (5, 3):
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050040 raise RuntimeError(
Alex Gaynor6091e112017-05-23 20:31:03 -070041 "cryptography 1.9 is not compatible with PyPy < 5.3. Please "
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050042 "upgrade PyPy to use this library."
43 )
44else:
Alex Gaynor6091e112017-05-23 20:31:03 -070045 setup_requirements.append("cffi>=1.7")
Julian Berman4cf38112015-02-24 10:51:53 -050046
koobsff0dd1e2014-02-24 21:55:04 +110047test_requirements = [
Alex Gaynorca941bd2017-08-10 22:30:07 -040048 "pytest>=3.2.1",
koobsff0dd1e2014-02-24 21:55:04 +110049 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000050 "iso8601",
InvalidInterrupt8e66ca62016-08-16 19:39:31 -070051 "pytz",
koobsff0dd1e2014-02-24 21:55:04 +110052]
Alex Gaynorde3aa052016-02-19 07:33:56 -050053if sys.version_info[:2] > (2, 6):
54 test_requirements.append("hypothesis>=1.11.4")
55
koobsff0dd1e2014-02-24 21:55:04 +110056
Alex Stapletona39a3192014-03-14 20:03:12 +000057# If there's no vectors locally that probably means we are in a tarball and
58# need to go and get the matching vectors package from PyPi
59if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
60 test_requirements.append(VECTORS_DEPENDENCY)
61
Alex Gaynor9a00f052014-01-02 13:09:34 -080062
Alex Gaynoracac6a62014-03-04 15:24:03 -080063class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +110064 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -080065 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +110066 self.test_args = []
67 self.test_suite = True
koobs06671802014-02-24 22:33:07 +110068
Alex Stapletona39a3192014-03-14 20:03:12 +000069 # This means there's a vectors/ folder with the package in here.
70 # cd into it, install the vectors package and then refresh sys.path
71 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -070072 subprocess.check_call(
73 [sys.executable, "setup.py", "install"], cwd="vectors"
74 )
Alex Stapletona39a3192014-03-14 20:03:12 +000075 pkg_resources.get_distribution("cryptography_vectors").activate()
76
koobsff0dd1e2014-02-24 21:55:04 +110077 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +110078 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +110079 import pytest
Alex Gaynor8a58e692015-02-20 07:58:39 -080080 test_args = [os.path.join(base_dir, "tests")]
81 errno = pytest.main(test_args)
koobsff0dd1e2014-02-24 21:55:04 +110082 sys.exit(errno)
83
Alex Gaynor9a00f052014-01-02 13:09:34 -080084
Peter Odding51ec05f2014-07-12 01:18:35 +020085def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +020086 """
87 Get a dictionary with setup keywords that (can) have side effects.
88
Peter Odding51ec05f2014-07-12 01:18:35 +020089 :param argv: A list of strings with command line arguments.
90 :returns: A dictionary with keyword arguments for the ``setup()`` function.
91
Peter Oddingc9b83f72014-07-12 00:52:58 +020092 This setup.py script uses the setuptools 'setup_requires' feature because
93 this is required by the cffi package to compile extension modules. The
94 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +020095 build process as a result of setup.py invocations that don't need the cffi
96 module to be built (setup.py serves the dual purpose of exposing package
97 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +020098
Peter Oddinge9144562014-07-12 03:14:55 +020099 All of the options listed by ``python setup.py --help`` that print
100 information should be recognized here. The commands ``clean``,
101 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
102 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200103
Peter Oddinge9144562014-07-12 03:14:55 +0200104 This function was originally based on the `setup.py script`_ of SciPy (see
105 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200106
107 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200108 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200109 """
Peter Oddinge9144562014-07-12 03:14:55 +0200110 no_setup_requires_arguments = (
111 '-h', '--help',
112 '-n', '--dry-run',
113 '-q', '--quiet',
114 '-v', '--verbose',
115 '-V', '--version',
116 '--author',
117 '--author-email',
118 '--classifiers',
119 '--contact',
120 '--contact-email',
121 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200122 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200123 '--fullname',
124 '--help-commands',
125 '--keywords',
126 '--licence',
127 '--license',
128 '--long-description',
129 '--maintainer',
130 '--maintainer-email',
131 '--name',
132 '--no-user-cfg',
133 '--obsoletes',
134 '--platforms',
135 '--provides',
136 '--requires',
137 '--url',
138 'clean',
139 'egg_info',
140 'register',
141 'sdist',
142 'upload',
143 )
Peter Odding97f45302014-07-14 21:40:35 +0200144
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200145 def is_short_option(argument):
146 """Check whether a command line argument is a short option."""
147 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200148
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200149 def expand_short_options(argument):
150 """Expand combined short options into canonical short options."""
151 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200152
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200153 def argument_without_setup_requirements(argv, i):
154 """Check whether a command line argument needs setup requirements."""
155 if argv[i] in no_setup_requires_arguments:
156 # Simple case: An argument which is either an option or a command
157 # which doesn't need setup requirements.
158 return True
Peter Odding97f45302014-07-14 21:40:35 +0200159 elif (is_short_option(argv[i]) and
160 all(option in no_setup_requires_arguments
161 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200162 # Not so simple case: Combined short options none of which need
163 # setup requirements.
164 return True
Peter Odding97f45302014-07-14 21:40:35 +0200165 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200166 # Tricky case: --egg-info takes an argument which should not make
167 # us use setup_requires (defeating the purpose of this code).
168 return True
169 else:
170 return False
Peter Odding97f45302014-07-14 21:40:35 +0200171
172 if all(argument_without_setup_requirements(argv, i)
173 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200174 return {
175 "cmdclass": {
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700176 "build": DummyBuild,
177 "install": DummyInstall,
Peter Odding3ae89a52014-07-12 02:06:56 +0200178 "test": DummyPyTest,
179 }
180 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200181 else:
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700182 cffi_modules = [
183 "src/_cffi_src/build_openssl.py:ffi",
184 "src/_cffi_src/build_constant_time.py:ffi",
185 "src/_cffi_src/build_padding.py:ffi",
186 ]
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700187
Peter Oddinge327cf12014-07-12 01:18:50 +0200188 return {
Paul Kehrer4c287d72015-06-08 00:05:53 -0500189 "setup_requires": setup_requirements,
Peter Oddinge327cf12014-07-12 01:18:50 +0200190 "cmdclass": {
Peter Oddinge327cf12014-07-12 01:18:50 +0200191 "test": PyTest,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700192 },
193 "cffi_modules": cffi_modules
Peter Oddinge327cf12014-07-12 01:18:50 +0200194 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200195
196
Peter Odding3ae89a52014-07-12 02:06:56 +0200197setup_requires_error = ("Requested setup command that needs 'setup_requires' "
198 "while command line arguments implied a side effect "
199 "free command or option.")
200
201
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700202class DummyBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200203 """
204 This class makes it very obvious when ``keywords_with_side_effects()`` has
205 incorrectly interpreted the command line arguments to ``setup.py build`` as
206 one of the 'side effect free' commands or options.
207 """
208
Peter Oddingdcce0802014-07-12 02:28:13 +0200209 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200210 raise RuntimeError(setup_requires_error)
211
212
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700213class DummyInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200214 """
215 This class makes it very obvious when ``keywords_with_side_effects()`` has
216 incorrectly interpreted the command line arguments to ``setup.py install``
217 as one of the 'side effect free' commands or options.
218 """
219
Peter Oddingdcce0802014-07-12 02:28:13 +0200220 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200221 raise RuntimeError(setup_requires_error)
222
223
Peter Oddingc9861f92014-07-13 04:17:20 +0200224class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200225 """
226 This class makes it very obvious when ``keywords_with_side_effects()`` has
227 incorrectly interpreted the command line arguments to ``setup.py test`` as
228 one of the 'side effect free' commands or options.
229 """
230
Peter Odding3ae89a52014-07-12 02:06:56 +0200231 def run_tests(self):
232 raise RuntimeError(setup_requires_error)
233
234
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800235with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800236 long_description = f.read()
237
238
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700239setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400240 name=about["__title__"],
241 version=about["__version__"],
242
243 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800244 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400245 license=about["__license__"],
246 url=about["__uri__"],
247
248 author=about["__author__"],
249 author_email=about["__email__"],
250
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200251 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200252 "Intended Audience :: Developers",
253 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700254 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200255 "Natural Language :: English",
256 "Operating System :: MacOS :: MacOS X",
257 "Operating System :: POSIX",
258 "Operating System :: POSIX :: BSD",
259 "Operating System :: POSIX :: Linux",
260 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200261 "Programming Language :: Python",
262 "Programming Language :: Python :: 2",
263 "Programming Language :: Python :: 2.6",
264 "Programming Language :: Python :: 2.7",
265 "Programming Language :: Python :: 3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700266 "Programming Language :: Python :: 3.4",
Alex Gaynor1d5805b2015-09-17 20:57:44 -0400267 "Programming Language :: Python :: 3.5",
Alex Gaynor31b5d782016-12-23 12:20:36 -0500268 "Programming Language :: Python :: 3.6",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200269 "Programming Language :: Python :: Implementation :: CPython",
270 "Programming Language :: Python :: Implementation :: PyPy",
271 "Topic :: Security :: Cryptography",
272 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400273
Donald Stufftc62a78c2014-11-07 19:17:08 -0500274 package_dir={"": "src"},
Frazer McLean542ea8e2016-03-26 23:10:23 +0100275 packages=find_packages(where="src", exclude=["_cffi_src", "_cffi_src.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700276 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400277
Alex Gaynor4c41ab02017-08-23 20:33:01 -0400278 install_requires=[
279 "idna >= 2.1",
280 "asn1crypto >= 0.21.0",
281 "six >= 1.4.1",
282 ],
koobsff0dd1e2014-02-24 21:55:04 +1100283 tests_require=test_requirements,
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500284 extras_require={
Alex Gaynor4c41ab02017-08-23 20:33:01 -0400285 ":python_version < '3'": ["enum34", "ipaddress"],
286 ":python_implementation != 'PyPy'": ["cffi >= 1.7"],
287
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500288 "test": test_requirements,
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500289 "docstest": [
Alex Gaynor94176082016-01-17 23:28:05 -0500290 "doc8",
Alex Gaynor0a8bdff2017-07-21 13:57:50 -0400291 "pyenchant >= 1.6.11",
Paul Kehrerf2691892016-12-09 16:31:15 -0600292 "readme_renderer >= 16.0",
Alex Gaynorbb315012017-07-02 18:32:21 -0400293 "sphinx != 1.6.1, != 1.6.2, != 1.6.3",
Alex Gaynor94176082016-01-17 23:28:05 -0500294 "sphinx_rtd_theme",
295 "sphinxcontrib-spelling",
296 ],
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500297 "pep8test": [
Alex Gaynor94176082016-01-17 23:28:05 -0500298 "flake8",
299 "flake8-import-order",
300 "pep8-naming",
301 ],
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500302 },
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400303
304 # for cffi
305 zip_safe=False,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700306 ext_package="cryptography.hazmat.bindings",
Peter Odding51ec05f2014-07-12 01:18:35 +0200307 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700308)