blob: 5db847739f05daa864c4eeaa05021afc95ec2ca0 [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 Kehrer71a16212015-05-18 18:59:25 -070035 "idna>=2.0",
Alex Gaynor02fe5a82015-06-22 20:45:41 -040036 "pyasn1>=0.1.8",
Alex Gaynor2119f742015-04-13 09:50:31 -040037 "six>=1.4.1",
Alex Gaynor02fe5a82015-06-22 20:45:41 -040038 "setuptools",
Donald Stufft5f12a1b2013-08-11 16:37:43 -040039]
Paul Kehrer4c287d72015-06-08 00:05:53 -050040setup_requirements = []
Donald Stufft5f12a1b2013-08-11 16:37:43 -040041
Paul Kehrer5bea5ca2014-12-18 07:42:27 -060042if sys.version_info < (3, 4):
43 requirements.append("enum34")
44
Paul Kehrer31bdf792015-03-25 14:11:00 -050045if sys.version_info < (3, 3):
46 requirements.append("ipaddress")
47
Paul Kehrer7d17cbb2015-08-20 13:25:15 -050048if platform.python_implementation() == "PyPy":
49 if sys.pypy_version_info < (2, 6):
50 raise RuntimeError(
51 "cryptography 1.0 is not compatible with PyPy < 2.6. Please "
52 "upgrade PyPy to use this library."
53 )
54else:
Paul Kehrer68b3b1e2015-05-19 13:05:21 -070055 requirements.append("cffi>=1.1.0")
Paul Kehrer4c287d72015-06-08 00:05:53 -050056 setup_requirements.append("cffi>=1.1.0")
Julian Berman4cf38112015-02-24 10:51:53 -050057
Paul Kehrer7ad18bc2014-03-26 13:13:38 -060058# 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 +110059test_requirements = [
Alex Gaynor959403b2015-09-19 13:19:43 -040060 "pytest<2.8",
koobsff0dd1e2014-02-24 21:55:04 +110061 "pretend",
Alex Stapleton0bd20e22014-03-14 19:58:07 +000062 "iso8601",
Terry Chia36a787f2015-09-30 19:13:14 +080063 "hypothesis",
koobsff0dd1e2014-02-24 21:55:04 +110064]
65
Alex Stapletona39a3192014-03-14 20:03:12 +000066# If there's no vectors locally that probably means we are in a tarball and
67# need to go and get the matching vectors package from PyPi
68if not os.path.exists(os.path.join(base_dir, "vectors/setup.py")):
69 test_requirements.append(VECTORS_DEPENDENCY)
70
Alex Gaynor9a00f052014-01-02 13:09:34 -080071
Terry Chia361545d2014-07-28 12:06:54 +080072def cc_is_available():
73 return sys.platform == "darwin" and list(map(
74 int, platform.mac_ver()[0].split("."))) >= [10, 8, 0]
75
76
77backends = [
78 "openssl = cryptography.hazmat.backends.openssl:backend"
79]
80
81if cc_is_available():
82 backends.append(
83 "commoncrypto = cryptography.hazmat.backends.commoncrypto:backend",
84 )
85
86
Alex Gaynoracac6a62014-03-04 15:24:03 -080087class PyTest(test):
koobsff0dd1e2014-02-24 21:55:04 +110088 def finalize_options(self):
Alex Gaynor6858cd42014-03-04 15:33:13 -080089 test.finalize_options(self)
koobsff0dd1e2014-02-24 21:55:04 +110090 self.test_args = []
91 self.test_suite = True
koobs06671802014-02-24 22:33:07 +110092
Alex Stapletona39a3192014-03-14 20:03:12 +000093 # This means there's a vectors/ folder with the package in here.
94 # cd into it, install the vectors package and then refresh sys.path
95 if VECTORS_DEPENDENCY not in test_requirements:
Alex Gaynord9f9b752014-07-11 10:18:24 -070096 subprocess.check_call(
97 [sys.executable, "setup.py", "install"], cwd="vectors"
98 )
Alex Stapletona39a3192014-03-14 20:03:12 +000099 pkg_resources.get_distribution("cryptography_vectors").activate()
100
koobsff0dd1e2014-02-24 21:55:04 +1100101 def run_tests(self):
koobs92a4cdb2014-02-24 22:13:17 +1100102 # Import here because in module scope the eggs are not loaded.
koobsff0dd1e2014-02-24 21:55:04 +1100103 import pytest
Alex Gaynor8a58e692015-02-20 07:58:39 -0800104 test_args = [os.path.join(base_dir, "tests")]
105 errno = pytest.main(test_args)
koobsff0dd1e2014-02-24 21:55:04 +1100106 sys.exit(errno)
107
Alex Gaynor9a00f052014-01-02 13:09:34 -0800108
Peter Odding51ec05f2014-07-12 01:18:35 +0200109def keywords_with_side_effects(argv):
Peter Oddingc9b83f72014-07-12 00:52:58 +0200110 """
111 Get a dictionary with setup keywords that (can) have side effects.
112
Peter Odding51ec05f2014-07-12 01:18:35 +0200113 :param argv: A list of strings with command line arguments.
114 :returns: A dictionary with keyword arguments for the ``setup()`` function.
115
Peter Oddingc9b83f72014-07-12 00:52:58 +0200116 This setup.py script uses the setuptools 'setup_requires' feature because
117 this is required by the cffi package to compile extension modules. The
118 purpose of ``keywords_with_side_effects()`` is to avoid triggering the cffi
Peter Oddinge9144562014-07-12 03:14:55 +0200119 build process as a result of setup.py invocations that don't need the cffi
120 module to be built (setup.py serves the dual purpose of exposing package
121 metadata).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200122
Peter Oddinge9144562014-07-12 03:14:55 +0200123 All of the options listed by ``python setup.py --help`` that print
124 information should be recognized here. The commands ``clean``,
125 ``egg_info``, ``register``, ``sdist`` and ``upload`` are also recognized.
126 Any combination of these options and commands is also supported.
Peter Oddingc9b83f72014-07-12 00:52:58 +0200127
Peter Oddinge9144562014-07-12 03:14:55 +0200128 This function was originally based on the `setup.py script`_ of SciPy (see
129 also the discussion in `pip issue #25`_).
Peter Oddingc9b83f72014-07-12 00:52:58 +0200130
131 .. _pip issue #25: https://github.com/pypa/pip/issues/25
Peter Odding63ce5df2014-07-12 01:56:37 +0200132 .. _setup.py script: https://github.com/scipy/scipy/blob/master/setup.py
Peter Oddingc9b83f72014-07-12 00:52:58 +0200133 """
Peter Oddinge9144562014-07-12 03:14:55 +0200134 no_setup_requires_arguments = (
135 '-h', '--help',
136 '-n', '--dry-run',
137 '-q', '--quiet',
138 '-v', '--verbose',
139 '-V', '--version',
140 '--author',
141 '--author-email',
142 '--classifiers',
143 '--contact',
144 '--contact-email',
145 '--description',
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200146 '--egg-base',
Peter Oddinge9144562014-07-12 03:14:55 +0200147 '--fullname',
148 '--help-commands',
149 '--keywords',
150 '--licence',
151 '--license',
152 '--long-description',
153 '--maintainer',
154 '--maintainer-email',
155 '--name',
156 '--no-user-cfg',
157 '--obsoletes',
158 '--platforms',
159 '--provides',
160 '--requires',
161 '--url',
162 'clean',
163 'egg_info',
164 'register',
165 'sdist',
166 'upload',
167 )
Peter Odding97f45302014-07-14 21:40:35 +0200168
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200169 def is_short_option(argument):
170 """Check whether a command line argument is a short option."""
171 return len(argument) >= 2 and argument[0] == '-' and argument[1] != '-'
Peter Odding97f45302014-07-14 21:40:35 +0200172
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200173 def expand_short_options(argument):
174 """Expand combined short options into canonical short options."""
175 return ('-' + char for char in argument[1:])
Peter Odding97f45302014-07-14 21:40:35 +0200176
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200177 def argument_without_setup_requirements(argv, i):
178 """Check whether a command line argument needs setup requirements."""
179 if argv[i] in no_setup_requires_arguments:
180 # Simple case: An argument which is either an option or a command
181 # which doesn't need setup requirements.
182 return True
Peter Odding97f45302014-07-14 21:40:35 +0200183 elif (is_short_option(argv[i]) and
184 all(option in no_setup_requires_arguments
185 for option in expand_short_options(argv[i]))):
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200186 # Not so simple case: Combined short options none of which need
187 # setup requirements.
188 return True
Peter Odding97f45302014-07-14 21:40:35 +0200189 elif argv[i - 1:i] == ['--egg-base']:
Peter Odding6c1e9ef2014-07-14 15:50:31 +0200190 # Tricky case: --egg-info takes an argument which should not make
191 # us use setup_requires (defeating the purpose of this code).
192 return True
193 else:
194 return False
Peter Odding97f45302014-07-14 21:40:35 +0200195
196 if all(argument_without_setup_requirements(argv, i)
197 for i in range(1, len(argv))):
Peter Odding3ae89a52014-07-12 02:06:56 +0200198 return {
199 "cmdclass": {
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700200 "build": DummyBuild,
201 "install": DummyInstall,
Peter Odding3ae89a52014-07-12 02:06:56 +0200202 "test": DummyPyTest,
203 }
204 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200205 else:
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700206 cffi_modules = [
207 "src/_cffi_src/build_openssl.py:ffi",
208 "src/_cffi_src/build_constant_time.py:ffi",
209 "src/_cffi_src/build_padding.py:ffi",
210 ]
211 if cc_is_available():
212 cffi_modules.append("src/_cffi_src/build_commoncrypto.py:ffi")
213
Peter Oddinge327cf12014-07-12 01:18:50 +0200214 return {
Paul Kehrer4c287d72015-06-08 00:05:53 -0500215 "setup_requires": setup_requirements,
Peter Oddinge327cf12014-07-12 01:18:50 +0200216 "cmdclass": {
Peter Oddinge327cf12014-07-12 01:18:50 +0200217 "test": PyTest,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700218 },
219 "cffi_modules": cffi_modules
Peter Oddinge327cf12014-07-12 01:18:50 +0200220 }
Peter Oddingc9b83f72014-07-12 00:52:58 +0200221
222
Peter Odding3ae89a52014-07-12 02:06:56 +0200223setup_requires_error = ("Requested setup command that needs 'setup_requires' "
224 "while command line arguments implied a side effect "
225 "free command or option.")
226
227
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700228class DummyBuild(build):
Peter Odding3ae89a52014-07-12 02:06:56 +0200229 """
230 This class makes it very obvious when ``keywords_with_side_effects()`` has
231 incorrectly interpreted the command line arguments to ``setup.py build`` as
232 one of the 'side effect free' commands or options.
233 """
234
Peter Oddingdcce0802014-07-12 02:28:13 +0200235 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200236 raise RuntimeError(setup_requires_error)
237
238
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700239class DummyInstall(install):
Peter Odding3ae89a52014-07-12 02:06:56 +0200240 """
241 This class makes it very obvious when ``keywords_with_side_effects()`` has
242 incorrectly interpreted the command line arguments to ``setup.py install``
243 as one of the 'side effect free' commands or options.
244 """
245
Peter Oddingdcce0802014-07-12 02:28:13 +0200246 def run(self):
Peter Odding3ae89a52014-07-12 02:06:56 +0200247 raise RuntimeError(setup_requires_error)
248
249
Peter Oddingc9861f92014-07-13 04:17:20 +0200250class DummyPyTest(test):
Peter Odding3ae89a52014-07-12 02:06:56 +0200251 """
252 This class makes it very obvious when ``keywords_with_side_effects()`` has
253 incorrectly interpreted the command line arguments to ``setup.py test`` as
254 one of the 'side effect free' commands or options.
255 """
256
Peter Odding3ae89a52014-07-12 02:06:56 +0200257 def run_tests(self):
258 raise RuntimeError(setup_requires_error)
259
260
Alex Gaynor7630d6c2014-01-03 07:34:43 -0800261with open(os.path.join(base_dir, "README.rst")) as f:
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800262 long_description = f.read()
263
264
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700265setup(
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400266 name=about["__title__"],
267 version=about["__version__"],
268
269 description=about["__summary__"],
Alex Gaynorf51f2c12014-01-03 07:33:01 -0800270 long_description=long_description,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400271 license=about["__license__"],
272 url=about["__uri__"],
273
274 author=about["__author__"],
275 author_email=about["__email__"],
276
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200277 classifiers=[
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200278 "Intended Audience :: Developers",
279 "License :: OSI Approved :: Apache Software License",
Alex Gaynorabe8bc92014-10-31 19:28:57 -0700280 "License :: OSI Approved :: BSD License",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200281 "Natural Language :: English",
282 "Operating System :: MacOS :: MacOS X",
283 "Operating System :: POSIX",
284 "Operating System :: POSIX :: BSD",
285 "Operating System :: POSIX :: Linux",
286 "Operating System :: Microsoft :: Windows",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200287 "Programming Language :: Python",
288 "Programming Language :: Python :: 2",
289 "Programming Language :: Python :: 2.6",
290 "Programming Language :: Python :: 2.7",
291 "Programming Language :: Python :: 3",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200292 "Programming Language :: Python :: 3.3",
Alex Gaynor7f8b2772014-03-17 10:22:41 -0700293 "Programming Language :: Python :: 3.4",
Alex Gaynor1d5805b2015-09-17 20:57:44 -0400294 "Programming Language :: Python :: 3.5",
Christian Heimesf83ed1d2013-08-10 23:28:29 +0200295 "Programming Language :: Python :: Implementation :: CPython",
296 "Programming Language :: Python :: Implementation :: PyPy",
297 "Topic :: Security :: Cryptography",
298 ],
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400299
Donald Stufftc62a78c2014-11-07 19:17:08 -0500300 package_dir={"": "src"},
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700301 packages=find_packages(
302 where="src", exclude=["_cffi_src", "_cffi_src.*", "tests", "tests.*"]
303 ),
Alex Gaynore23dd3a2014-08-11 13:51:54 -0700304 include_package_data=True,
Donald Stufft9ebb8ff2013-08-11 17:05:03 -0400305
Alex Gaynor91f119e2014-01-02 13:12:59 -0800306 install_requires=requirements,
koobsff0dd1e2014-02-24 21:55:04 +1100307 tests_require=test_requirements,
Donald Stufft5f12a1b2013-08-11 16:37:43 -0400308
309 # for cffi
310 zip_safe=False,
Paul Kehrer68b3b1e2015-05-19 13:05:21 -0700311 ext_package="cryptography.hazmat.bindings",
Terry Chiada5dca82014-07-27 12:27:52 +0800312 entry_points={
Terry Chia361545d2014-07-28 12:06:54 +0800313 "cryptography.backends": backends,
Peter Oddingc9b83f72014-07-12 00:52:58 +0200314 },
Peter Odding51ec05f2014-07-12 01:18:35 +0200315 **keywords_with_side_effects(sys.argv)
Alex Gaynorc62e91f2013-08-06 19:25:52 -0700316)