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