Jean-Paul Calderone | cf20dd6 | 2008-06-12 16:38:38 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jean-Paul Calderone | 0db6cdb | 2008-04-11 11:52:15 -0400 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | be1e9d8 | 2010-10-07 22:32:00 -0400 | [diff] [blame] | 5 | # Copyright (C) Jean-Paul Calderone 2008-2010, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | # |
Jean-Paul Calderone | e53ccf7 | 2008-04-11 11:40:39 -0400 | [diff] [blame] | 7 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 8 | """ |
| 9 | Installation script for the OpenSSL module |
| 10 | """ |
| 11 | |
Jean-Paul Calderone | ddbc28f | 2008-12-31 16:33:17 -0500 | [diff] [blame] | 12 | import sys, os |
Jean-Paul Calderone | 5a1bf38 | 2009-02-08 16:39:15 -0500 | [diff] [blame] | 13 | from distutils.core import Extension, setup |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 14 | from distutils.errors import DistutilsFileError |
| 15 | from distutils.command.build_ext import build_ext |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 16 | |
Jean-Paul Calderone | ba4308e | 2010-07-27 20:56:32 -0400 | [diff] [blame] | 17 | # XXX Deduplicate this |
Jean-Paul Calderone | 5e99f44 | 2011-09-02 11:08:10 -0400 | [diff] [blame] | 18 | __version__ = '0.13' |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 19 | |
Jean-Paul Calderone | 084b752 | 2013-02-09 09:53:45 -0800 | [diff] [blame] | 20 | xcrypto_src = ['OpenSSL/crypto/crypto.c', 'OpenSSL/crypto/x509.c', |
Jean-Paul Calderone | 024375a | 2010-07-27 20:37:50 -0400 | [diff] [blame] | 21 | 'OpenSSL/crypto/x509name.c', 'OpenSSL/crypto/pkey.c', |
| 22 | 'OpenSSL/crypto/x509store.c', 'OpenSSL/crypto/x509req.c', |
| 23 | 'OpenSSL/crypto/x509ext.c', 'OpenSSL/crypto/pkcs7.c', |
| 24 | 'OpenSSL/crypto/pkcs12.c', 'OpenSSL/crypto/netscape_spki.c', |
| 25 | 'OpenSSL/crypto/revoked.c', 'OpenSSL/crypto/crl.c', |
| 26 | 'OpenSSL/util.c'] |
Jean-Paul Calderone | 084b752 | 2013-02-09 09:53:45 -0800 | [diff] [blame] | 27 | xcrypto_dep = ['OpenSSL/crypto/crypto.h', 'OpenSSL/crypto/x509.h', |
Jean-Paul Calderone | 024375a | 2010-07-27 20:37:50 -0400 | [diff] [blame] | 28 | 'OpenSSL/crypto/x509name.h', 'OpenSSL/crypto/pkey.h', |
| 29 | 'OpenSSL/crypto/x509store.h', 'OpenSSL/crypto/x509req.h', |
| 30 | 'OpenSSL/crypto/x509ext.h', 'OpenSSL/crypto/pkcs7.h', |
| 31 | 'OpenSSL/crypto/pkcs12.h', 'OpenSSL/crypto/netscape_spki.h', |
| 32 | 'OpenSSL/crypto/revoked.h', 'OpenSSL/crypto/crl.h', |
| 33 | 'OpenSSL/util.h'] |
Jean-Paul Calderone | 024375a | 2010-07-27 20:37:50 -0400 | [diff] [blame] | 34 | ssl_src = ['OpenSSL/ssl/connection.c', 'OpenSSL/ssl/context.c', 'OpenSSL/ssl/ssl.c', |
Jean-Paul Calderone | e0fcf51 | 2012-02-13 09:10:15 -0500 | [diff] [blame] | 35 | 'OpenSSL/ssl/session.c', 'OpenSSL/util.c'] |
Jean-Paul Calderone | 024375a | 2010-07-27 20:37:50 -0400 | [diff] [blame] | 36 | ssl_dep = ['OpenSSL/ssl/connection.h', 'OpenSSL/ssl/context.h', 'OpenSSL/ssl/ssl.h', |
Jean-Paul Calderone | e0fcf51 | 2012-02-13 09:10:15 -0500 | [diff] [blame] | 37 | 'OpenSSL/ssl/session.h', 'OpenSSL/util.h'] |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 38 | |
| 39 | IncludeDirs = None |
| 40 | LibraryDirs = None |
| 41 | |
| 42 | # Add more platforms here when needed |
| 43 | if os.name == 'nt' or sys.platform == 'win32': |
Jean-Paul Calderone | 61b0c43 | 2009-07-20 16:55:35 -0400 | [diff] [blame] | 44 | |
| 45 | Libraries = ['Ws2_32'] |
Jean-Paul Calderone | 61b0c43 | 2009-07-20 16:55:35 -0400 | [diff] [blame] | 46 | |
Jean-Paul Calderone | 61b0c43 | 2009-07-20 16:55:35 -0400 | [diff] [blame] | 47 | |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 48 | |
| 49 | class BuildExtension(build_ext): |
| 50 | """ |
| 51 | A custom command that semiautomatically finds dependencies required by |
| 52 | PyOpenSSL. |
| 53 | """ |
| 54 | |
| 55 | user_options = (build_ext.user_options + |
| 56 | [("with-openssl=", None, |
| 57 | "directory where OpenSSL is installed")]) |
| 58 | with_openssl = None |
| 59 | openssl_dlls = () |
| 60 | openssl_mingw = False |
| 61 | |
| 62 | |
| 63 | def finalize_options(self): |
| 64 | """ |
| 65 | Update build options with details about OpenSSL. |
| 66 | """ |
| 67 | build_ext.finalize_options(self) |
| 68 | if self.with_openssl is None: |
| 69 | self.find_openssl() |
| 70 | self.find_openssl_dlls() |
| 71 | self.add_openssl_compile_info() |
| 72 | |
| 73 | |
| 74 | def find_openssl(self): |
| 75 | """ |
| 76 | Find OpenSSL's install directory. |
| 77 | """ |
Jean-Paul Calderone | 6ef6588 | 2009-11-11 10:36:04 -0500 | [diff] [blame] | 78 | potentials = [] |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 79 | dirs = os.environ.get("PATH").split(os.pathsep) |
| 80 | for d in dirs: |
| 81 | if os.path.exists(os.path.join(d, "openssl.exe")): |
| 82 | ssldir, bin = os.path.split(d) |
| 83 | if not bin: |
| 84 | ssldir, bin = os.path.split(ssldir) |
Jean-Paul Calderone | 6ef6588 | 2009-11-11 10:36:04 -0500 | [diff] [blame] | 85 | potentials.append(ssldir) |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 86 | childdirs = os.listdir(ssldir) |
Jean-Paul Calderone | 6ef6588 | 2009-11-11 10:36:04 -0500 | [diff] [blame] | 87 | if "lib" in childdirs and "include" in childdirs: |
| 88 | self.with_openssl = ssldir |
| 89 | return |
| 90 | if potentials: |
| 91 | raise DistutilsFileError( |
| 92 | "Only found improper OpenSSL directories: %r" % ( |
| 93 | potentials,)) |
| 94 | else: |
| 95 | raise DistutilsFileError("Could not find 'openssl.exe'") |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def find_openssl_dlls(self): |
| 99 | """ |
| 100 | Find OpenSSL's shared libraries. |
| 101 | """ |
| 102 | self.openssl_dlls = [] |
| 103 | self.find_openssl_dll("libssl32.dll", False) |
| 104 | if self.openssl_dlls: |
| 105 | self.openssl_mingw = True |
| 106 | else: |
| 107 | self.find_openssl_dll("ssleay32.dll", True) |
| 108 | self.find_openssl_dll("libeay32.dll", True) |
| 109 | # add zlib to the mix if it looks like OpenSSL |
| 110 | # was linked with a private copy of it |
| 111 | self.find_openssl_dll("zlib1.dll", False) |
| 112 | |
| 113 | |
| 114 | def find_openssl_dll(self, name, required): |
| 115 | """ |
| 116 | Find OpenSSL's shared library and its path after installation. |
| 117 | """ |
| 118 | dllpath = os.path.join(self.with_openssl, "bin", name) |
| 119 | if not os.path.exists(dllpath): |
| 120 | if required: |
| 121 | raise DistutilsFileError("could not find '%s'" % name) |
| 122 | else: |
| 123 | return |
| 124 | newpath = os.path.join(self.build_lib, "OpenSSL", name) |
| 125 | self.openssl_dlls.append((dllpath, newpath)) |
| 126 | |
| 127 | |
| 128 | def add_openssl_compile_info(self): |
| 129 | """ |
| 130 | Set up various compile and link parameters. |
| 131 | """ |
| 132 | if self.compiler == "mingw32": |
| 133 | if self.openssl_mingw: |
| 134 | # Library path and library names are sane when OpenSSL is |
| 135 | # built with MinGW . |
| 136 | libdir = "lib" |
| 137 | libs = ["eay32", "ssl32"] |
| 138 | else: |
| 139 | libdir = "" |
| 140 | libs = [] |
| 141 | # Unlike when using the binary installer, which creates |
| 142 | # an atypical shared library name 'ssleay32', so we have |
| 143 | # to use this workaround. |
| 144 | if self.link_objects is None: |
| 145 | self.link_objects = [] |
| 146 | for dllpath, _ in self.openssl_dlls: |
| 147 | dllname = os.path.basename(dllpath) |
| 148 | libname = os.path.splitext(dllname)[0] + ".a" |
| 149 | libpath = os.path.join(self.with_openssl, |
| 150 | "lib", "MinGW", libname) |
| 151 | self.link_objects.append(libpath) |
| 152 | else: |
| 153 | libdir = "lib" |
| 154 | libs = ["libeay32", "ssleay32"] |
| 155 | self.include_dirs.append(os.path.join(self.with_openssl, "include")) |
| 156 | self.library_dirs.append(os.path.join(self.with_openssl, libdir)) |
| 157 | self.libraries.extend(libs) |
| 158 | |
| 159 | |
| 160 | def run(self): |
| 161 | """ |
| 162 | Build extension modules and copy shared libraries. |
| 163 | """ |
| 164 | build_ext.run(self) |
| 165 | for dllpath, newpath in self.openssl_dlls: |
| 166 | self.copy_file(dllpath, newpath) |
| 167 | |
| 168 | |
| 169 | def get_outputs(self): |
| 170 | """ |
| 171 | Return a list of file paths built by this comand. |
| 172 | """ |
| 173 | output = [pathpair[1] for pathpair in self.openssl_dlls] |
| 174 | output.extend(build_ext.get_outputs(self)) |
| 175 | return output |
| 176 | |
| 177 | |
| 178 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 179 | else: |
| 180 | Libraries = ['ssl', 'crypto'] |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 181 | BuildExtension = build_ext |
| 182 | |
Jean-Paul Calderone | 5a1bf38 | 2009-02-08 16:39:15 -0500 | [diff] [blame] | 183 | |
U-YOUR-FA38FA253F\Zooko Brillnonywonx | d78922f | 2008-11-10 06:19:16 -0700 | [diff] [blame] | 184 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 185 | def mkExtension(name): |
Jean-Paul Calderone | e940437 | 2008-03-04 22:19:18 -0500 | [diff] [blame] | 186 | modname = 'OpenSSL.' + name |
| 187 | src = globals()[name.lower() + '_src'] |
| 188 | dep = globals()[name.lower() + '_dep'] |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 189 | return Extension(modname, src, libraries=Libraries, depends=dep, |
Jean-Paul Calderone | 2d79b30 | 2009-07-20 14:49:53 -0400 | [diff] [blame] | 190 | include_dirs=IncludeDirs, library_dirs=LibraryDirs) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 191 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 192 | |
| 193 | setup(name='pyOpenSSL', version=__version__, |
Jean-Paul Calderone | e0d94c8 | 2009-07-21 11:12:52 -0400 | [diff] [blame] | 194 | packages = ['OpenSSL'], |
Jean-Paul Calderone | 024375a | 2010-07-27 20:37:50 -0400 | [diff] [blame] | 195 | package_dir = {'OpenSSL': 'OpenSSL'}, |
Jean-Paul Calderone | 084b752 | 2013-02-09 09:53:45 -0800 | [diff] [blame] | 196 | ext_modules = [mkExtension('xcrypto'), mkExtension('SSL')], |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 197 | py_modules = ['OpenSSL.__init__', 'OpenSSL.tsafe', |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 198 | 'OpenSSL.rand', |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 199 | 'OpenSSL.version', 'OpenSSL.test.__init__', |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 200 | 'OpenSSL.test.util', |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 201 | 'OpenSSL.test.test_crypto', |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 202 | 'OpenSSL.test.test_rand', |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 203 | 'OpenSSL.test.test_ssl'], |
Jean-Paul Calderone | 71d62c0 | 2009-07-21 11:30:22 -0400 | [diff] [blame] | 204 | zip_safe = False, |
Jean-Paul Calderone | bcd4545 | 2009-11-11 10:10:57 -0500 | [diff] [blame] | 205 | cmdclass = {"build_ext": BuildExtension}, |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 206 | description = 'Python wrapper module around the OpenSSL library', |
Jean-Paul Calderone | e53ccf7 | 2008-04-11 11:40:39 -0400 | [diff] [blame] | 207 | author = 'Martin Sjögren, AB Strakt', |
| 208 | author_email = 'msjogren@gmail.com', |
| 209 | maintainer = 'Jean-Paul Calderone', |
| 210 | maintainer_email = 'exarkun@twistedmatrix.com', |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 211 | url = 'http://pyopenssl.sourceforge.net/', |
Jean-Paul Calderone | 9820bba | 2011-03-02 19:02:33 -0500 | [diff] [blame] | 212 | license = 'APL2', |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 213 | long_description = """\ |
| 214 | High-level wrapper around a subset of the OpenSSL library, includes |
| 215 | * SSL.Connection objects, wrapping the methods of Python's portable |
| 216 | sockets |
| 217 | * Callbacks written in Python |
| 218 | * Extensive error-handling mechanism, mirroring OpenSSL's error codes |
Jean-Paul Calderone | d59d3bc | 2011-12-19 13:32:29 -0500 | [diff] [blame] | 219 | ... and much more ;)""", |
| 220 | classifiers = [ |
| 221 | 'Development Status :: 6 - Mature', |
| 222 | 'Intended Audience :: Developers', |
| 223 | 'License :: OSI Approved :: Apache Software License', |
| 224 | 'Operating System :: MacOS :: MacOS X', |
| 225 | 'Operating System :: Microsoft :: Windows', |
| 226 | 'Operating System :: POSIX', |
| 227 | 'Programming Language :: C', |
| 228 | 'Programming Language :: Python :: 2.4', |
| 229 | 'Programming Language :: Python :: 2.5', |
| 230 | 'Programming Language :: Python :: 2.6', |
| 231 | 'Programming Language :: Python :: 2.7', |
| 232 | 'Programming Language :: Python :: 3.2', |
| 233 | 'Programming Language :: Python :: Implementation :: CPython', |
| 234 | 'Programming Language :: Python :: Implementation :: PyPy', |
| 235 | 'Topic :: Security :: Cryptography', |
| 236 | 'Topic :: Software Development :: Libraries :: Python Modules', |
| 237 | 'Topic :: System :: Networking', |
| 238 | ]) |