blob: e0b57380d78e1ce867817ca83f4154a8555eb7c1 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Alex Gaynorf51f2c12014-01-03 07:33:01 -08007import os
Terry Chia361545d2014-07-28 12:06:54 +08008import platform
Alex Stapletona39a3192014-03-14 20:03:12 +00009import subprocess
Alex Stapleton707b0082014-04-20 22:24:41 +010010import sys
Alex Stapleton4b610cc2014-03-22 08:49:35 +000011from distutils.command.build import build
Alex Stapletona39a3192014-03-14 20:03:12 +000012
13import pkg_resources
Alex Gaynor9a00f052014-01-02 13:09:34 -080014
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040015from setuptools import find_packages, setup
Sascha Peilickec5492052014-03-31 17:59:37 +020016from setuptools.command.install import install
Alex Gaynoracac6a62014-03-04 15:24:03 -080017from setuptools.command.test import test
Donald Stufft446a4572013-08-11 17:38:13 -040018
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040019
Alex Gaynor7630d6c2014-01-03 07:34:43 -080020base_dir = os.path.dirname(__file__)
Donald Stufftc62a78c2014-11-07 19:17:08 -050021src_dir = os.path.join(base_dir, "src")
22
23# When executing the setup.py, we need to be able to import ourselves, this
24# means that we need to add the src/ directory to the sys.path.
25sys.path.insert(0, src_dir)
Alex Gaynor7630d6c2014-01-03 07:34:43 -080026
Donald Stufft5f12a1b2013-08-11 16:37:43 -040027about = {}
Donald Stufftc62a78c2014-11-07 19:17:08 -050028with open(os.path.join(src_dir, "cryptography", "__about__.py")) as f:
Alex Gaynor7630d6c2014-01-03 07:34:43 -080029 exec(f.read(), about)
Donald Stufft5f12a1b2013-08-11 16:37:43 -040030
31
Alex Stapletona39a3192014-03-14 20:03:12 +000032VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
Donald Stufft5f12a1b2013-08-11 16:37:43 -040033
Alex Gaynor91f119e2014-01-02 13:12:59 -080034requirements = [
Paul Kehrer40f83382015-04-20 15:00:16 -050035 "idna",
Paul Kehrera98f95a2014-11-27 09:55:17 -100036 "pyasn1",
Alex Gaynor2119f742015-04-13 09:50:31 -040037 "six>=1.4.1",
38 "setuptools"
Donald Stufft5f12a1b2013-08-11 16:37:43 -040039]
40
Paul Kehrer5bea5ca2014-12-18 07:42:27 -060041if sys.version_info < (3, 4):
42 requirements.append("enum34")
43
Paul Kehrer31bdf792015-03-25 14:11:00 -050044if sys.version_info < (3, 3):
45 requirements.append("ipaddress")
46
Julian Berman4cf38112015-02-24 10:51:53 -050047if platform.python_implementation() != "PyPy":
Alex Gaynor2119f742015-04-13 09:50:31 -040048 requirements.append("cffi>=0.8")
Julian Berman4cf38112015-02-24 10:51:53 -050049
Paul Kehrer7ad18bc2014-03-26 13:13:38 -060050# If you add a new dep here you probably need to add it in the tox.ini as well
koobsff0dd1e2014-02-24 21:55:04 +110051test_requirements = [
52 "pytest",
53 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000054 "iso8601",
koobsff0dd1e2014-02-24 21:55:04 +110055]
56
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
Terry Chia361545d2014-07-28 12:06:54 +080063def cc_is_available():
64 return sys.platform == "darwin" and list(map(
65 int, platform.mac_ver()[0].split("."))) >= [10, 8, 0]
66
67
68backends = [
69 "openssl = cryptography.hazmat.backends.openssl:backend"
70]
71
72if cc_is_available():
73 backends.append(
74 "commoncrypto = cryptography.hazmat.backends.commoncrypto:backend",
75 )
76
77
Sascha Peilickec5492052014-03-31 17:59:37 +020078def get_ext_modules():
79 from cryptography.hazmat.bindings.commoncrypto.binding import (
80 Binding as CommonCryptoBinding
81 )
82 from cryptography.hazmat.bindings.openssl.binding import (
83 Binding as OpenSSLBinding
84 )
85 from cryptography.hazmat.primitives import constant_time, padding
86
87 ext_modules = [
Donald Stufftd1b70f32014-11-07 17:48:49 -050088 OpenSSLBinding.ffi.verifier.get_extension(),
Sascha Peilickec5492052014-03-31 17:59:37 +020089 constant_time._ffi.verifier.get_extension(),
90 padding._ffi.verifier.get_extension()
91 ]
Terry Chia361545d2014-07-28 12:06:54 +080092 if cc_is_available():
Donald Stufftd1b70f32014-11-07 17:48:49 -050093 ext_modules.append(CommonCryptoBinding.ffi.verifier.get_extension())
Sascha Peilickec5492052014-03-31 17:59:37 +020094 return ext_modules
95
96
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060097class CFFIBuild(build):
Alex Gaynor49697512014-01-03 15:08:45 -080098 """
99 This class exists, instead of just providing ``ext_modules=[...]`` directly
100 in ``setup()`` because importing cryptography requires we have several
101 packages installed first.
102
103 By doing the imports here we ensure that packages listed in
104 ``setup_requires`` are already installed.
105 """
106
Alex Gaynor9a00f052014-01-02 13:09:34 -0800107 def finalize_options(self):
Sascha Peilickec5492052014-03-31 17:59:37 +0200108 self.distribution.ext_modules = get_ext_modules()
Alex Gaynor9a00f052014-01-02 13:09:34 -0800109 build.finalize_options(self)
110
koobs92a4cdb2014-02-24 22:13:17 +1100111
Sascha Peilickec5492052014-03-31 17:59:37 +0200112class CFFIInstall(install):
113 """
114 As a consequence of CFFIBuild and it's late addition of ext_modules, we
115 need the equivalent for the ``install`` command to install into platlib
116 install-dir rather than purelib.
117 """
118
119 def finalize_options(self):
120 self.distribution.ext_modules = get_ext_modules()
121 install.finalize_options(self)
122
123
Alex Gaynoracac6a62014-03-04 15:24:03 -0800124class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +1100125 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -0800126 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +1100127 self.test_args = []
128 self.test_suite = True
koobs06671802014-02-24 22:33:07 +1100129
Alex Stapletona39a3192014-03-14 20:03:12 +0000130 # This means there's a vectors/ folder with the package in here.
131 # cd into it, install the vectors package and then refresh sys.path
132 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -0700133 subprocess.check_call(
134 [sys.executable, "setup.py", "install"], cwd="vectors"
135 )
Alex Stapletona39a3192014-03-14 20:03:12 +0000136 pkg_resources.get_distribution("cryptography_vectors").activate()
137
koobsff0dd1e2014-02-24 21:55:04 +1100138 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +1100139 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +1100140 import pytest
Alex Gaynor8a58e692015-02-20 07:58:39 -0800141 test_args = [os.path.join(base_dir, "tests")]
142 errno = pytest.main(test_args)
koobsff0dd1e2014-02-24 21:55:04 +1100143 sys.exit(errno)
144
Alex Gaynor9a00f052014-01-02 13:09:34 -0800145
Peter Odding51ec05f2014-07-12 01:18:35 +0200146def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +0200147 """
148 Get a dictionary with setup keywords that (can) have side effects.
149
Peter Odding51ec05f2014-07-12 01:18:35 +0200150 :param argv: A list of strings with command line arguments.
151 :returns: A dictionary with keyword arguments for the ``setup()`` function.
152
Peter Oddingc9b83f72014-07-12 00:52:58 +0200153 This setup.py script uses the setuptools 'setup_requires' feature because
154 this is required by the cffi package to compile extension modules. The
155 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200156 build process as a result of setup.py invocations that don't need the cffi
157 module to be built (setup.py serves the dual purpose of exposing package
158 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200159
Peter Oddinge9144562014-07-12 03:14:55 +0200160 All of the options listed by ``python setup.py --help`` that print
161 information should be recognized here. The commands ``clean``,
162 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
163 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200164
Peter Oddinge9144562014-07-12 03:14:55 +0200165 This function was originally based on the `setup.py script`_ of SciPy (see
166 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200167
168 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200169 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200170 """
Peter Oddinge9144562014-07-12 03:14:55 +0200171 no_setup_requires_arguments = (
172 '-h', '--help',
173 '-n', '--dry-run',
174 '-q', '--quiet',
175 '-v', '--verbose',
176 '-V', '--version',
177 '--author',
178 '--author-email',
179 '--classifiers',
180 '--contact',
181 '--contact-email',
182 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200183 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200184 '--fullname',
185 '--help-commands',
186 '--keywords',
187 '--licence',
188 '--license',
189 '--long-description',
190 '--maintainer',
191 '--maintainer-email',
192 '--name',
193 '--no-user-cfg',
194 '--obsoletes',
195 '--platforms',
196 '--provides',
197 '--requires',
198 '--url',
199 'clean',
200 'egg_info',
201 'register',
202 'sdist',
203 'upload',
204 )
Peter Odding97f45302014-07-14 21:40:35 +0200205
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200206 def is_short_option(argument):
207 """Check whether a command line argument is a short option."""
208 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200209
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200210 def expand_short_options(argument):
211 """Expand combined short options into canonical short options."""
212 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200213
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200214 def argument_without_setup_requirements(argv, i):
215 """Check whether a command line argument needs setup requirements."""
216 if argv[i] in no_setup_requires_arguments:
217 # Simple case: An argument which is either an option or a command
218 # which doesn't need setup requirements.
219 return True
Peter Odding97f45302014-07-14 21:40:35 +0200220 elif (is_short_option(argv[i]) and
221 all(option in no_setup_requires_arguments
222 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200223 # Not so simple case: Combined short options none of which need
224 # setup requirements.
225 return True
Peter Odding97f45302014-07-14 21:40:35 +0200226 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200227 # Tricky case: --egg-info takes an argument which should not make
228 # us use setup_requires (defeating the purpose of this code).
229 return True
230 else:
231 return False
Peter Odding97f45302014-07-14 21:40:35 +0200232
233 if all(argument_without_setup_requirements(argv, i)
234 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200235 return {
236 "cmdclass": {
237 "build": DummyCFFIBuild,
238 "install": DummyCFFIInstall,
239 "test": DummyPyTest,
240 }
241 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200242 else:
Peter Oddinge327cf12014-07-12 01:18:50 +0200243 return {
244 "setup_requires": requirements,
245 "cmdclass": {
246 "build": CFFIBuild,
247 "install": CFFIInstall,
248 "test": PyTest,
249 }
250 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200251
252
Peter Odding3ae89a52014-07-12 02:06:56 +0200253setup_requires_error = ("Requested setup command that needs 'setup_requires' "
254 "while command line arguments implied a side effect "
255 "free command or option.")
256
257
Peter Oddingc9861f92014-07-13 04:17:20 +0200258class DummyCFFIBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200259 """
260 This class makes it very obvious when ``keywords_with_side_effects()`` has
261 incorrectly interpreted the command line arguments to ``setup.py build`` as
262 one of the 'side effect free' commands or options.
263 """
264
Peter Oddingdcce0802014-07-12 02:28:13 +0200265 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200266 raise RuntimeError(setup_requires_error)
267
268
Peter Oddingc9861f92014-07-13 04:17:20 +0200269class DummyCFFIInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200270 """
271 This class makes it very obvious when ``keywords_with_side_effects()`` has
272 incorrectly interpreted the command line arguments to ``setup.py install``
273 as one of the 'side effect free' commands or options.
274 """
275
Peter Oddingdcce0802014-07-12 02:28:13 +0200276 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200277 raise RuntimeError(setup_requires_error)
278
279
Peter Oddingc9861f92014-07-13 04:17:20 +0200280class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200281 """
282 This class makes it very obvious when ``keywords_with_side_effects()`` has
283 incorrectly interpreted the command line arguments to ``setup.py test`` as
284 one of the 'side effect free' commands or options.
285 """
286
Peter Odding3ae89a52014-07-12 02:06:56 +0200287 def run_tests(self):
288 raise RuntimeError(setup_requires_error)
289
290
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800291with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800292 long_description = f.read()
293
294
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700295setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400296 name=about["__title__"],
297 version=about["__version__"],
298
299 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800300 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400301 license=about["__license__"],
302 url=about["__uri__"],
303
304 author=about["__author__"],
305 author_email=about["__email__"],
306
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200307 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200308 "Intended Audience :: Developers",
309 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700310 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200311 "Natural Language :: English",
312 "Operating System :: MacOS :: MacOS X",
313 "Operating System :: POSIX",
314 "Operating System :: POSIX :: BSD",
315 "Operating System :: POSIX :: Linux",
316 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200317 "Programming Language :: Python",
318 "Programming Language :: Python :: 2",
319 "Programming Language :: Python :: 2.6",
320 "Programming Language :: Python :: 2.7",
321 "Programming Language :: Python :: 3",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200322 "Programming Language :: Python :: 3.3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700323 "Programming Language :: Python :: 3.4",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200324 "Programming Language :: Python :: Implementation :: CPython",
325 "Programming Language :: Python :: Implementation :: PyPy",
326 "Topic :: Security :: Cryptography",
327 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400328
Donald Stufftc62a78c2014-11-07 19:17:08 -0500329 package_dir={"": "src"},
330 packages=find_packages(where="src", exclude=["tests", "tests.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700331 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400332
Alex Gaynor91f119e2014-01-02 13:12:59 -0800333 install_requires=requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100334 tests_require=test_requirements,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400335
336 # for cffi
337 zip_safe=False,
Alex Gaynor9a00f052014-01-02 13:09:34 -0800338 ext_package="cryptography",
Terry Chiada5dca82014-07-27 12:27:52 +0800339 entry_points={
Terry Chia361545d2014-07-28 12:06:54 +0800340 "cryptography.backends": backends,
Peter Oddingc9b83f72014-07-12 00:52:58 +0200341 },
Peter Odding51ec05f2014-07-12 01:18:35 +0200342 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700343)