blob: 7a76909b741be88098479494dfda5420339d5eb9 [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
Terry Chiada5dca82014-07-27 12:27:52 +080032SETUPTOOLS_DEPENDENCY = "setuptools"
Paul Kehrer7fcaa372014-01-10 23:39:58 -060033CFFI_DEPENDENCY = "cffi>=0.8"
Paul Kehrerc0242552013-09-10 18:54:13 -050034SIX_DEPENDENCY = "six>=1.4.1"
Alex Stapletona39a3192014-03-14 20:03:12 +000035VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
Donald Stufft5f12a1b2013-08-11 16:37:43 -040036
Alex Gaynor91f119e2014-01-02 13:12:59 -080037requirements = [
Donald Stufft5f12a1b2013-08-11 16:37:43 -040038 CFFI_DEPENDENCY,
Terry Chiada5dca82014-07-27 12:27:52 +080039 SIX_DEPENDENCY,
40 SETUPTOOLS_DEPENDENCY
Donald Stufft5f12a1b2013-08-11 16:37:43 -040041]
42
Paul Kehrer7ad18bc2014-03-26 13:13:38 -060043# 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 +110044test_requirements = [
45 "pytest",
Paul Kehrerd3e3df92014-04-30 11:13:17 -050046 "pyasn1",
koobsff0dd1e2014-02-24 21:55:04 +110047 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000048 "iso8601",
koobsff0dd1e2014-02-24 21:55:04 +110049]
50
Alex Stapletona39a3192014-03-14 20:03:12 +000051# If there's no vectors locally that probably means we are in a tarball and
52# need to go and get the matching vectors package from PyPi
53if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
54 test_requirements.append(VECTORS_DEPENDENCY)
55
Alex Gaynor9a00f052014-01-02 13:09:34 -080056
Terry Chia361545d2014-07-28 12:06:54 +080057def cc_is_available():
58 return sys.platform == "darwin" and list(map(
59 int, platform.mac_ver()[0].split("."))) >= [10, 8, 0]
60
61
62backends = [
63 "openssl = cryptography.hazmat.backends.openssl:backend"
64]
65
66if cc_is_available():
67 backends.append(
68 "commoncrypto = cryptography.hazmat.backends.commoncrypto:backend",
69 )
70
71
Sascha Peilickec5492052014-03-31 17:59:37 +020072def get_ext_modules():
73 from cryptography.hazmat.bindings.commoncrypto.binding import (
74 Binding as CommonCryptoBinding
75 )
76 from cryptography.hazmat.bindings.openssl.binding import (
77 Binding as OpenSSLBinding
78 )
79 from cryptography.hazmat.primitives import constant_time, padding
80
81 ext_modules = [
Donald Stufftd1b70f32014-11-07 17:48:49 -050082 OpenSSLBinding.ffi.verifier.get_extension(),
Sascha Peilickec5492052014-03-31 17:59:37 +020083 constant_time._ffi.verifier.get_extension(),
84 padding._ffi.verifier.get_extension()
85 ]
Terry Chia361545d2014-07-28 12:06:54 +080086 if cc_is_available():
Donald Stufftd1b70f32014-11-07 17:48:49 -050087 ext_modules.append(CommonCryptoBinding.ffi.verifier.get_extension())
Sascha Peilickec5492052014-03-31 17:59:37 +020088 return ext_modules
89
90
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060091class CFFIBuild(build):
Alex Gaynor49697512014-01-03 15:08:45 -080092 """
93 This class exists, instead of just providing ``ext_modules=[...]`` directly
94 in ``setup()`` because importing cryptography requires we have several
95 packages installed first.
96
97 By doing the imports here we ensure that packages listed in
98 ``setup_requires`` are already installed.
99 """
100
Alex Gaynor9a00f052014-01-02 13:09:34 -0800101 def finalize_options(self):
Sascha Peilickec5492052014-03-31 17:59:37 +0200102 self.distribution.ext_modules = get_ext_modules()
Alex Gaynor9a00f052014-01-02 13:09:34 -0800103 build.finalize_options(self)
104
koobs92a4cdb2014-02-24 22:13:17 +1100105
Sascha Peilickec5492052014-03-31 17:59:37 +0200106class CFFIInstall(install):
107 """
108 As a consequence of CFFIBuild and it's late addition of ext_modules, we
109 need the equivalent for the ``install`` command to install into platlib
110 install-dir rather than purelib.
111 """
112
113 def finalize_options(self):
114 self.distribution.ext_modules = get_ext_modules()
115 install.finalize_options(self)
116
117
Alex Gaynoracac6a62014-03-04 15:24:03 -0800118class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +1100119 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -0800120 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +1100121 self.test_args = []
122 self.test_suite = True
koobs06671802014-02-24 22:33:07 +1100123
Alex Stapletona39a3192014-03-14 20:03:12 +0000124 # This means there's a vectors/ folder with the package in here.
125 # cd into it, install the vectors package and then refresh sys.path
126 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -0700127 subprocess.check_call(
128 [sys.executable, "setup.py", "install"], cwd="vectors"
129 )
Alex Stapletona39a3192014-03-14 20:03:12 +0000130 pkg_resources.get_distribution("cryptography_vectors").activate()
131
koobsff0dd1e2014-02-24 21:55:04 +1100132 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +1100133 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +1100134 import pytest
135 errno = pytest.main(self.test_args)
136 sys.exit(errno)
137
Alex Gaynor9a00f052014-01-02 13:09:34 -0800138
Peter Odding51ec05f2014-07-12 01:18:35 +0200139def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +0200140 """
141 Get a dictionary with setup keywords that (can) have side effects.
142
Peter Odding51ec05f2014-07-12 01:18:35 +0200143 :param argv: A list of strings with command line arguments.
144 :returns: A dictionary with keyword arguments for the ``setup()`` function.
145
Peter Oddingc9b83f72014-07-12 00:52:58 +0200146 This setup.py script uses the setuptools 'setup_requires' feature because
147 this is required by the cffi package to compile extension modules. The
148 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200149 build process as a result of setup.py invocations that don't need the cffi
150 module to be built (setup.py serves the dual purpose of exposing package
151 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200152
Peter Oddinge9144562014-07-12 03:14:55 +0200153 All of the options listed by ``python setup.py --help`` that print
154 information should be recognized here. The commands ``clean``,
155 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
156 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200157
Peter Oddinge9144562014-07-12 03:14:55 +0200158 This function was originally based on the `setup.py script`_ of SciPy (see
159 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200160
161 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200162 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200163 """
Peter Oddinge9144562014-07-12 03:14:55 +0200164 no_setup_requires_arguments = (
165 '-h', '--help',
166 '-n', '--dry-run',
167 '-q', '--quiet',
168 '-v', '--verbose',
169 '-V', '--version',
170 '--author',
171 '--author-email',
172 '--classifiers',
173 '--contact',
174 '--contact-email',
175 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200176 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200177 '--fullname',
178 '--help-commands',
179 '--keywords',
180 '--licence',
181 '--license',
182 '--long-description',
183 '--maintainer',
184 '--maintainer-email',
185 '--name',
186 '--no-user-cfg',
187 '--obsoletes',
188 '--platforms',
189 '--provides',
190 '--requires',
191 '--url',
192 'clean',
193 'egg_info',
194 'register',
195 'sdist',
196 'upload',
197 )
Peter Odding97f45302014-07-14 21:40:35 +0200198
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200199 def is_short_option(argument):
200 """Check whether a command line argument is a short option."""
201 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200202
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200203 def expand_short_options(argument):
204 """Expand combined short options into canonical short options."""
205 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200206
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200207 def argument_without_setup_requirements(argv, i):
208 """Check whether a command line argument needs setup requirements."""
209 if argv[i] in no_setup_requires_arguments:
210 # Simple case: An argument which is either an option or a command
211 # which doesn't need setup requirements.
212 return True
Peter Odding97f45302014-07-14 21:40:35 +0200213 elif (is_short_option(argv[i]) and
214 all(option in no_setup_requires_arguments
215 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200216 # Not so simple case: Combined short options none of which need
217 # setup requirements.
218 return True
Peter Odding97f45302014-07-14 21:40:35 +0200219 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200220 # Tricky case: --egg-info takes an argument which should not make
221 # us use setup_requires (defeating the purpose of this code).
222 return True
223 else:
224 return False
Peter Odding97f45302014-07-14 21:40:35 +0200225
226 if all(argument_without_setup_requirements(argv, i)
227 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200228 return {
229 "cmdclass": {
230 "build": DummyCFFIBuild,
231 "install": DummyCFFIInstall,
232 "test": DummyPyTest,
233 }
234 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200235 else:
Peter Oddinge327cf12014-07-12 01:18:50 +0200236 return {
237 "setup_requires": requirements,
238 "cmdclass": {
239 "build": CFFIBuild,
240 "install": CFFIInstall,
241 "test": PyTest,
242 }
243 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200244
245
Peter Odding3ae89a52014-07-12 02:06:56 +0200246setup_requires_error = ("Requested setup command that needs 'setup_requires' "
247 "while command line arguments implied a side effect "
248 "free command or option.")
249
250
Peter Oddingc9861f92014-07-13 04:17:20 +0200251class DummyCFFIBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200252 """
253 This class makes it very obvious when ``keywords_with_side_effects()`` has
254 incorrectly interpreted the command line arguments to ``setup.py build`` as
255 one of the 'side effect free' commands or options.
256 """
257
Peter Oddingdcce0802014-07-12 02:28:13 +0200258 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200259 raise RuntimeError(setup_requires_error)
260
261
Peter Oddingc9861f92014-07-13 04:17:20 +0200262class DummyCFFIInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200263 """
264 This class makes it very obvious when ``keywords_with_side_effects()`` has
265 incorrectly interpreted the command line arguments to ``setup.py install``
266 as one of the 'side effect free' commands or options.
267 """
268
Peter Oddingdcce0802014-07-12 02:28:13 +0200269 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200270 raise RuntimeError(setup_requires_error)
271
272
Peter Oddingc9861f92014-07-13 04:17:20 +0200273class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200274 """
275 This class makes it very obvious when ``keywords_with_side_effects()`` has
276 incorrectly interpreted the command line arguments to ``setup.py test`` as
277 one of the 'side effect free' commands or options.
278 """
279
Peter Odding3ae89a52014-07-12 02:06:56 +0200280 def run_tests(self):
281 raise RuntimeError(setup_requires_error)
282
283
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800284with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800285 long_description = f.read()
286
287
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700288setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400289 name=about["__title__"],
290 version=about["__version__"],
291
292 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800293 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400294 license=about["__license__"],
295 url=about["__uri__"],
296
297 author=about["__author__"],
298 author_email=about["__email__"],
299
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200300 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200301 "Intended Audience :: Developers",
302 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700303 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200304 "Natural Language :: English",
305 "Operating System :: MacOS :: MacOS X",
306 "Operating System :: POSIX",
307 "Operating System :: POSIX :: BSD",
308 "Operating System :: POSIX :: Linux",
309 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200310 "Programming Language :: Python",
311 "Programming Language :: Python :: 2",
312 "Programming Language :: Python :: 2.6",
313 "Programming Language :: Python :: 2.7",
314 "Programming Language :: Python :: 3",
315 "Programming Language :: Python :: 3.2",
316 "Programming Language :: Python :: 3.3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700317 "Programming Language :: Python :: 3.4",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200318 "Programming Language :: Python :: Implementation :: CPython",
319 "Programming Language :: Python :: Implementation :: PyPy",
320 "Topic :: Security :: Cryptography",
321 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400322
Donald Stufftc62a78c2014-11-07 19:17:08 -0500323 package_dir={"": "src"},
324 packages=find_packages(where="src", exclude=["tests", "tests.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700325 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400326
Alex Gaynor91f119e2014-01-02 13:12:59 -0800327 install_requires=requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100328 tests_require=test_requirements,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400329
330 # for cffi
331 zip_safe=False,
Alex Gaynor9a00f052014-01-02 13:09:34 -0800332 ext_package="cryptography",
Terry Chiada5dca82014-07-27 12:27:52 +0800333 entry_points={
Terry Chia361545d2014-07-28 12:06:54 +0800334 "cryptography.backends": backends,
Peter Oddingc9b83f72014-07-12 00:52:58 +0200335 },
Peter Odding51ec05f2014-07-12 01:18:35 +0200336 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700337)