blob: 3dd2bd6eaaa1a53873d08d588fb2f16bba711c82 [file] [log] [blame]
Alex Gaynorbfc06bc2013-08-06 19:36:19 -07001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
Alex Gaynorc37feed2014-03-08 08:32:56 -080013
14from __future__ import absolute_import, division, print_function
15
Alex Gaynorf51f2c12014-01-03 07:33:01 -080016import os
Terry Chia361545d2014-07-28 12:06:54 +080017import platform
Alex Stapletona39a3192014-03-14 20:03:12 +000018import subprocess
Alex Stapleton707b0082014-04-20 22:24:41 +010019import sys
Alex Stapleton4b610cc2014-03-22 08:49:35 +000020from distutils.command.build import build
Alex Stapletona39a3192014-03-14 20:03:12 +000021
22import pkg_resources
Alex Gaynor9a00f052014-01-02 13:09:34 -080023
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040024from setuptools import find_packages, setup
Sascha Peilickec5492052014-03-31 17:59:37 +020025from setuptools.command.install import install
Alex Gaynoracac6a62014-03-04 15:24:03 -080026from setuptools.command.test import test
Donald Stufft446a4572013-08-11 17:38:13 -040027
Paul Kehrerafc1ccd2014-03-19 11:49:32 -040028
Alex Gaynor7630d6c2014-01-03 07:34:43 -080029base_dir = os.path.dirname(__file__)
30
Donald Stufft5f12a1b2013-08-11 16:37:43 -040031about = {}
Alex Gaynor7630d6c2014-01-03 07:34:43 -080032with open(os.path.join(base_dir, "cryptography", "__about__.py")) as f:
33 exec(f.read(), about)
Donald Stufft5f12a1b2013-08-11 16:37:43 -040034
35
Terry Chiada5dca82014-07-27 12:27:52 +080036SETUPTOOLS_DEPENDENCY = "setuptools"
Paul Kehrer7fcaa372014-01-10 23:39:58 -060037CFFI_DEPENDENCY = "cffi>=0.8"
Paul Kehrerc0242552013-09-10 18:54:13 -050038SIX_DEPENDENCY = "six>=1.4.1"
Alex Stapletona39a3192014-03-14 20:03:12 +000039VECTORS_DEPENDENCY = "cryptography_vectors=={0}".format(about['__version__'])
Donald Stufft5f12a1b2013-08-11 16:37:43 -040040
Alex Gaynor91f119e2014-01-02 13:12:59 -080041requirements = [
Donald Stufft5f12a1b2013-08-11 16:37:43 -040042 CFFI_DEPENDENCY,
Terry Chiada5dca82014-07-27 12:27:52 +080043 SIX_DEPENDENCY,
44 SETUPTOOLS_DEPENDENCY
Donald Stufft5f12a1b2013-08-11 16:37:43 -040045]
46
Paul Kehrer7ad18bc2014-03-26 13:13:38 -060047# 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 +110048test_requirements = [
49 "pytest",
Paul Kehrerd3e3df92014-04-30 11:13:17 -050050 "pyasn1",
koobsff0dd1e2014-02-24 21:55:04 +110051 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000052 "iso8601",
koobsff0dd1e2014-02-24 21:55:04 +110053]
54
Alex Stapletona39a3192014-03-14 20:03:12 +000055# If there's no vectors locally that probably means we are in a tarball and
56# need to go and get the matching vectors package from PyPi
57if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
58 test_requirements.append(VECTORS_DEPENDENCY)
59
Alex Gaynor9a00f052014-01-02 13:09:34 -080060
Terry Chia361545d2014-07-28 12:06:54 +080061def cc_is_available():
62 return sys.platform == "darwin" and list(map(
63 int, platform.mac_ver()[0].split("."))) >= [10, 8, 0]
64
65
66backends = [
67 "openssl = cryptography.hazmat.backends.openssl:backend"
68]
69
70if cc_is_available():
71 backends.append(
72 "commoncrypto = cryptography.hazmat.backends.commoncrypto:backend",
73 )
74
75
Sascha Peilickec5492052014-03-31 17:59:37 +020076def get_ext_modules():
77 from cryptography.hazmat.bindings.commoncrypto.binding import (
78 Binding as CommonCryptoBinding
79 )
80 from cryptography.hazmat.bindings.openssl.binding import (
81 Binding as OpenSSLBinding
82 )
83 from cryptography.hazmat.primitives import constant_time, padding
84
85 ext_modules = [
86 OpenSSLBinding().ffi.verifier.get_extension(),
87 constant_time._ffi.verifier.get_extension(),
88 padding._ffi.verifier.get_extension()
89 ]
Terry Chia361545d2014-07-28 12:06:54 +080090 if cc_is_available():
Sascha Peilickec5492052014-03-31 17:59:37 +020091 ext_modules.append(CommonCryptoBinding().ffi.verifier.get_extension())
92 return ext_modules
93
94
Paul Kehrer5b6ce2a2014-02-24 20:16:10 -060095class CFFIBuild(build):
Alex Gaynor49697512014-01-03 15:08:45 -080096 """
97 This class exists, instead of just providing ``ext_modules=[...]`` directly
98 in ``setup()`` because importing cryptography requires we have several
99 packages installed first.
100
101 By doing the imports here we ensure that packages listed in
102 ``setup_requires`` are already installed.
103 """
104
Alex Gaynor9a00f052014-01-02 13:09:34 -0800105 def finalize_options(self):
Sascha Peilickec5492052014-03-31 17:59:37 +0200106 self.distribution.ext_modules = get_ext_modules()
Alex Gaynor9a00f052014-01-02 13:09:34 -0800107 build.finalize_options(self)
108
koobs92a4cdb2014-02-24 22:13:17 +1100109
Sascha Peilickec5492052014-03-31 17:59:37 +0200110class CFFIInstall(install):
111 """
112 As a consequence of CFFIBuild and it's late addition of ext_modules, we
113 need the equivalent for the ``install`` command to install into platlib
114 install-dir rather than purelib.
115 """
116
117 def finalize_options(self):
118 self.distribution.ext_modules = get_ext_modules()
119 install.finalize_options(self)
120
121
Alex Gaynoracac6a62014-03-04 15:24:03 -0800122class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +1100123 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -0800124 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +1100125 self.test_args = []
126 self.test_suite = True
koobs06671802014-02-24 22:33:07 +1100127
Alex Stapletona39a3192014-03-14 20:03:12 +0000128 # This means there's a vectors/ folder with the package in here.
129 # cd into it, install the vectors package and then refresh sys.path
130 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -0700131 subprocess.check_call(
132 [sys.executable, "setup.py", "install"], cwd="vectors"
133 )
Alex Stapletona39a3192014-03-14 20:03:12 +0000134 pkg_resources.get_distribution("cryptography_vectors").activate()
135
koobsff0dd1e2014-02-24 21:55:04 +1100136 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +1100137 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +1100138 import pytest
139 errno = pytest.main(self.test_args)
140 sys.exit(errno)
141
Alex Gaynor9a00f052014-01-02 13:09:34 -0800142
Peter Odding51ec05f2014-07-12 01:18:35 +0200143def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +0200144 """
145 Get a dictionary with setup keywords that (can) have side effects.
146
Peter Odding51ec05f2014-07-12 01:18:35 +0200147 :param argv: A list of strings with command line arguments.
148 :returns: A dictionary with keyword arguments for the ``setup()`` function.
149
Peter Oddingc9b83f72014-07-12 00:52:58 +0200150 This setup.py script uses the setuptools 'setup_requires' feature because
151 this is required by the cffi package to compile extension modules. The
152 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200153 build process as a result of setup.py invocations that don't need the cffi
154 module to be built (setup.py serves the dual purpose of exposing package
155 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200156
Peter Oddinge9144562014-07-12 03:14:55 +0200157 All of the options listed by ``python setup.py --help`` that print
158 information should be recognized here. The commands ``clean``,
159 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
160 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200161
Peter Oddinge9144562014-07-12 03:14:55 +0200162 This function was originally based on the `setup.py script`_ of SciPy (see
163 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200164
165 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200166 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200167 """
Peter Oddinge9144562014-07-12 03:14:55 +0200168 no_setup_requires_arguments = (
169 '-h', '--help',
170 '-n', '--dry-run',
171 '-q', '--quiet',
172 '-v', '--verbose',
173 '-V', '--version',
174 '--author',
175 '--author-email',
176 '--classifiers',
177 '--contact',
178 '--contact-email',
179 '--description',
180 '--fullname',
181 '--help-commands',
182 '--keywords',
183 '--licence',
184 '--license',
185 '--long-description',
186 '--maintainer',
187 '--maintainer-email',
188 '--name',
189 '--no-user-cfg',
190 '--obsoletes',
191 '--platforms',
192 '--provides',
193 '--requires',
194 '--url',
195 'clean',
196 'egg_info',
197 'register',
198 'sdist',
199 'upload',
200 )
201 if all((arg in no_setup_requires_arguments) or
202 all(('-' + char) in no_setup_requires_arguments for char in arg[1:])
203 for arg in argv[1:]):
Peter Odding3ae89a52014-07-12 02:06:56 +0200204 return {
205 "cmdclass": {
206 "build": DummyCFFIBuild,
207 "install": DummyCFFIInstall,
208 "test": DummyPyTest,
209 }
210 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200211 else:
Peter Oddinge327cf12014-07-12 01:18:50 +0200212 return {
213 "setup_requires": requirements,
214 "cmdclass": {
215 "build": CFFIBuild,
216 "install": CFFIInstall,
217 "test": PyTest,
218 }
219 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200220
221
Peter Odding3ae89a52014-07-12 02:06:56 +0200222setup_requires_error = ("Requested setup command that needs 'setup_requires' "
223 "while command line arguments implied a side effect "
224 "free command or option.")
225
226
Peter Oddingc9861f92014-07-13 04:17:20 +0200227class DummyCFFIBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200228 """
229 This class makes it very obvious when ``keywords_with_side_effects()`` has
230 incorrectly interpreted the command line arguments to ``setup.py build`` as
231 one of the 'side effect free' commands or options.
232 """
233
Peter Oddingdcce0802014-07-12 02:28:13 +0200234 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200235 raise RuntimeError(setup_requires_error)
236
237
Peter Oddingc9861f92014-07-13 04:17:20 +0200238class DummyCFFIInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200239 """
240 This class makes it very obvious when ``keywords_with_side_effects()`` has
241 incorrectly interpreted the command line arguments to ``setup.py install``
242 as one of the 'side effect free' commands or options.
243 """
244
Peter Oddingdcce0802014-07-12 02:28:13 +0200245 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200246 raise RuntimeError(setup_requires_error)
247
248
Peter Oddingc9861f92014-07-13 04:17:20 +0200249class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200250 """
251 This class makes it very obvious when ``keywords_with_side_effects()`` has
252 incorrectly interpreted the command line arguments to ``setup.py test`` as
253 one of the 'side effect free' commands or options.
254 """
255
Peter Odding3ae89a52014-07-12 02:06:56 +0200256 def run_tests(self):
257 raise RuntimeError(setup_requires_error)
258
259
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800260with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800261 long_description = f.read()
262
263
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700264setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400265 name=about["__title__"],
266 version=about["__version__"],
267
268 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800269 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400270 license=about["__license__"],
271 url=about["__uri__"],
272
273 author=about["__author__"],
274 author_email=about["__email__"],
275
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200276 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200277 "Intended Audience :: Developers",
278 "License :: OSI Approved :: Apache Software License",
279 "Natural Language :: English",
280 "Operating System :: MacOS :: MacOS X",
281 "Operating System :: POSIX",
282 "Operating System :: POSIX :: BSD",
283 "Operating System :: POSIX :: Linux",
284 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200285 "Programming Language :: Python",
286 "Programming Language :: Python :: 2",
287 "Programming Language :: Python :: 2.6",
288 "Programming Language :: Python :: 2.7",
289 "Programming Language :: Python :: 3",
290 "Programming Language :: Python :: 3.2",
291 "Programming Language :: Python :: 3.3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700292 "Programming Language :: Python :: 3.4",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200293 "Programming Language :: Python :: Implementation :: CPython",
294 "Programming Language :: Python :: Implementation :: PyPy",
295 "Topic :: Security :: Cryptography",
296 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400297
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400298 packages=find_packages(exclude=["tests", "tests.*"]),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700299 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400300
Alex Gaynor91f119e2014-01-02 13:12:59 -0800301 install_requires=requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100302 tests_require=test_requirements,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400303
304 # for cffi
305 zip_safe=False,
Alex Gaynor9a00f052014-01-02 13:09:34 -0800306 ext_package="cryptography",
Terry Chiada5dca82014-07-27 12:27:52 +0800307 entry_points={
Terry Chia361545d2014-07-28 12:06:54 +0800308 "cryptography.backends": backends,
Peter Oddingc9b83f72014-07-12 00:52:58 +0200309 },
Peter Odding51ec05f2014-07-12 01:18:35 +0200310 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700311)