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