blob: 586ea48dd8ce2c8f090a19954492d8d634bb494f [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
Alex Gaynor91f119e2014-01-02 13:12:59 -080036requirements = [
Alex Gaynor8ad694c2017-02-20 11:45:20 -050037 "idna>=2.1",
Ofek Lev0e6a1292017-02-08 00:09:41 -050038 "asn1crypto>=0.21.0",
Alex Gaynor2119f742015-04-13 09:50:31 -040039 "six>=1.4.1",
Alex Gaynor82393f92017-04-21 23:04:27 -040040 "setuptools>=20.5",
Donald Stufft5f12a1b2013-08-11 16:37:43 -040041]
Paul Kehrer4c287d72015-06-08 00:05:53 -050042setup_requirements = []
Donald Stufft5f12a1b2013-08-11 16:37:43 -040043
Paul Kehrer5bea5ca2014-12-18 07:42:27 -060044if sys.version_info < (3, 4):
45 requirements.append("enum34")
46
Paul Kehrer31bdf792015-03-25 14:11:00 -050047if sys.version_info < (3, 3):
48 requirements.append("ipaddress")
49
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050050if platform.python_implementation() == "PyPy":
51 if sys.pypy_version_info < (2, 6):
52 raise RuntimeError(
53 "cryptography 1.0 is not compatible with PyPy < 2.6. Please "
54 "upgrade PyPy to use this library."
55 )
56else:
Christian Heimes830af272015-11-19 16:18:04 +010057 requirements.append("cffi>=1.4.1")
58 setup_requirements.append("cffi>=1.4.1")
Julian Berman4cf38112015-02-24 10:51:53 -050059
koobsff0dd1e2014-02-24 21:55:04 +110060test_requirements = [
Alex Gaynor14a9ad42016-07-01 22:32:23 -040061 "pytest>=2.9.0",
koobsff0dd1e2014-02-24 21:55:04 +110062 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000063 "iso8601",
InvalidInterrupt8e66ca62016-08-16 19:39:31 -070064 "pytz",
koobsff0dd1e2014-02-24 21:55:04 +110065]
Alex Gaynorde3aa052016-02-19 07:33:56 -050066if sys.version_info[:2] > (2, 6):
67 test_requirements.append("hypothesis>=1.11.4")
68
koobsff0dd1e2014-02-24 21:55:04 +110069
Alex Stapletona39a3192014-03-14 20:03:12 +000070# If there's no vectors locally that probably means we are in a tarball and
71# need to go and get the matching vectors package from PyPi
72if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
73 test_requirements.append(VECTORS_DEPENDENCY)
74
Alex Gaynor9a00f052014-01-02 13:09:34 -080075
Terry Chia361545d2014-07-28 12:06:54 +080076backends = [
77 "openssl = cryptography.hazmat.backends.openssl:backend"
78]
79
Terry Chia361545d2014-07-28 12:06:54 +080080
Alex Gaynoracac6a62014-03-04 15:24:03 -080081class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +110082 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -080083 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +110084 self.test_args = []
85 self.test_suite = True
koobs06671802014-02-24 22:33:07 +110086
Alex Stapletona39a3192014-03-14 20:03:12 +000087 # This means there's a vectors/ folder with the package in here.
88 # cd into it, install the vectors package and then refresh sys.path
89 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -070090 subprocess.check_call(
91 [sys.executable, "setup.py", "install"], cwd="vectors"
92 )
Alex Stapletona39a3192014-03-14 20:03:12 +000093 pkg_resources.get_distribution("cryptography_vectors").activate()
94
koobsff0dd1e2014-02-24 21:55:04 +110095 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +110096 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +110097 import pytest
Alex Gaynor8a58e692015-02-20 07:58:39 -080098 test_args = [os.path.join(base_dir, "tests")]
99 errno = pytest.main(test_args)
koobsff0dd1e2014-02-24 21:55:04 +1100100 sys.exit(errno)
101
Alex Gaynor9a00f052014-01-02 13:09:34 -0800102
Peter Odding51ec05f2014-07-12 01:18:35 +0200103def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +0200104 """
105 Get a dictionary with setup keywords that (can) have side effects.
106
Peter Odding51ec05f2014-07-12 01:18:35 +0200107 :param argv: A list of strings with command line arguments.
108 :returns: A dictionary with keyword arguments for the ``setup()`` function.
109
Peter Oddingc9b83f72014-07-12 00:52:58 +0200110 This setup.py script uses the setuptools 'setup_requires' feature because
111 this is required by the cffi package to compile extension modules. The
112 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200113 build process as a result of setup.py invocations that don't need the cffi
114 module to be built (setup.py serves the dual purpose of exposing package
115 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200116
Peter Oddinge9144562014-07-12 03:14:55 +0200117 All of the options listed by ``python setup.py --help`` that print
118 information should be recognized here. The commands ``clean``,
119 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
120 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200121
Peter Oddinge9144562014-07-12 03:14:55 +0200122 This function was originally based on the `setup.py script`_ of SciPy (see
123 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200124
125 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200126 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200127 """
Peter Oddinge9144562014-07-12 03:14:55 +0200128 no_setup_requires_arguments = (
129 '-h', '--help',
130 '-n', '--dry-run',
131 '-q', '--quiet',
132 '-v', '--verbose',
133 '-V', '--version',
134 '--author',
135 '--author-email',
136 '--classifiers',
137 '--contact',
138 '--contact-email',
139 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200140 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200141 '--fullname',
142 '--help-commands',
143 '--keywords',
144 '--licence',
145 '--license',
146 '--long-description',
147 '--maintainer',
148 '--maintainer-email',
149 '--name',
150 '--no-user-cfg',
151 '--obsoletes',
152 '--platforms',
153 '--provides',
154 '--requires',
155 '--url',
156 'clean',
157 'egg_info',
158 'register',
159 'sdist',
160 'upload',
161 )
Peter Odding97f45302014-07-14 21:40:35 +0200162
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200163 def is_short_option(argument):
164 """Check whether a command line argument is a short option."""
165 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200166
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200167 def expand_short_options(argument):
168 """Expand combined short options into canonical short options."""
169 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200170
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200171 def argument_without_setup_requirements(argv, i):
172 """Check whether a command line argument needs setup requirements."""
173 if argv[i] in no_setup_requires_arguments:
174 # Simple case: An argument which is either an option or a command
175 # which doesn't need setup requirements.
176 return True
Peter Odding97f45302014-07-14 21:40:35 +0200177 elif (is_short_option(argv[i]) and
178 all(option in no_setup_requires_arguments
179 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200180 # Not so simple case: Combined short options none of which need
181 # setup requirements.
182 return True
Peter Odding97f45302014-07-14 21:40:35 +0200183 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200184 # Tricky case: --egg-info takes an argument which should not make
185 # us use setup_requires (defeating the purpose of this code).
186 return True
187 else:
188 return False
Peter Odding97f45302014-07-14 21:40:35 +0200189
190 if all(argument_without_setup_requirements(argv, i)
191 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200192 return {
193 "cmdclass": {
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700194 "build": DummyBuild,
195 "install": DummyInstall,
Peter Odding3ae89a52014-07-12 02:06:56 +0200196 "test": DummyPyTest,
197 }
198 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200199 else:
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700200 cffi_modules = [
201 "src/_cffi_src/build_openssl.py:ffi",
202 "src/_cffi_src/build_constant_time.py:ffi",
203 "src/_cffi_src/build_padding.py:ffi",
204 ]
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700205
Peter Oddinge327cf12014-07-12 01:18:50 +0200206 return {
Paul Kehrer4c287d72015-06-08 00:05:53 -0500207 "setup_requires": setup_requirements,
Peter Oddinge327cf12014-07-12 01:18:50 +0200208 "cmdclass": {
Peter Oddinge327cf12014-07-12 01:18:50 +0200209 "test": PyTest,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700210 },
211 "cffi_modules": cffi_modules
Peter Oddinge327cf12014-07-12 01:18:50 +0200212 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200213
214
Peter Odding3ae89a52014-07-12 02:06:56 +0200215setup_requires_error = ("Requested setup command that needs 'setup_requires' "
216 "while command line arguments implied a side effect "
217 "free command or option.")
218
219
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700220class DummyBuild(build):
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 build`` as
224 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
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700231class DummyInstall(install):
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 install``
235 as one of the 'side effect free' commands or options.
236 """
237
Peter Oddingdcce0802014-07-12 02:28:13 +0200238 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200239 raise RuntimeError(setup_requires_error)
240
241
Peter Oddingc9861f92014-07-13 04:17:20 +0200242class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200243 """
244 This class makes it very obvious when ``keywords_with_side_effects()`` has
245 incorrectly interpreted the command line arguments to ``setup.py test`` as
246 one of the 'side effect free' commands or options.
247 """
248
Peter Odding3ae89a52014-07-12 02:06:56 +0200249 def run_tests(self):
250 raise RuntimeError(setup_requires_error)
251
252
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800253with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800254 long_description = f.read()
255
256
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700257setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400258 name=about["__title__"],
259 version=about["__version__"],
260
261 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800262 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400263 license=about["__license__"],
264 url=about["__uri__"],
265
266 author=about["__author__"],
267 author_email=about["__email__"],
268
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200269 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200270 "Intended Audience :: Developers",
271 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700272 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200273 "Natural Language :: English",
274 "Operating System :: MacOS :: MacOS X",
275 "Operating System :: POSIX",
276 "Operating System :: POSIX :: BSD",
277 "Operating System :: POSIX :: Linux",
278 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200279 "Programming Language :: Python",
280 "Programming Language :: Python :: 2",
281 "Programming Language :: Python :: 2.6",
282 "Programming Language :: Python :: 2.7",
283 "Programming Language :: Python :: 3",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200284 "Programming Language :: Python :: 3.3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700285 "Programming Language :: Python :: 3.4",
Alex Gaynor1d5805b2015-09-17 20:57:44 -0400286 "Programming Language :: Python :: 3.5",
Alex Gaynor31b5d782016-12-23 12:20:36 -0500287 "Programming Language :: Python :: 3.6",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200288 "Programming Language :: Python :: Implementation :: CPython",
289 "Programming Language :: Python :: Implementation :: PyPy",
290 "Topic :: Security :: Cryptography",
291 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400292
Donald Stufftc62a78c2014-11-07 19:17:08 -0500293 package_dir={"": "src"},
Frazer McLean542ea8e2016-03-26 23:10:23 +0100294 packages=find_packages(where="src", exclude=["_cffi_src", "_cffi_src.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700295 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400296
Alex Gaynor91f119e2014-01-02 13:12:59 -0800297 install_requires=requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100298 tests_require=test_requirements,
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500299 extras_require={
300 "test": test_requirements,
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500301 "docstest": [
Alex Gaynor94176082016-01-17 23:28:05 -0500302 "doc8",
303 "pyenchant",
Paul Kehrerf2691892016-12-09 16:31:15 -0600304 "readme_renderer >= 16.0",
Alex Gaynorc1f91692017-05-17 09:47:36 -0400305 "sphinx != 1.6.1",
Alex Gaynor94176082016-01-17 23:28:05 -0500306 "sphinx_rtd_theme",
307 "sphinxcontrib-spelling",
308 ],
Paul Kehrerc6c3b6d2016-05-29 00:17:08 -0500309 "pep8test": [
Alex Gaynor94176082016-01-17 23:28:05 -0500310 "flake8",
311 "flake8-import-order",
312 "pep8-naming",
313 ],
Alex Gaynor22f7ec52016-01-17 11:41:40 -0500314 },
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400315
316 # for cffi
317 zip_safe=False,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700318 ext_package="cryptography.hazmat.bindings",
Terry Chiada5dca82014-07-27 12:27:52 +0800319 entry_points={
Terry Chia361545d2014-07-28 12:06:54 +0800320 "cryptography.backends": backends,
Peter Oddingc9b83f72014-07-12 00:52:58 +0200321 },
Peter Odding51ec05f2014-07-12 01:18:35 +0200322 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700323)