1 | n/a | #./python |
---|
2 | n/a | """Run Python tests with multiple installations of OpenSSL |
---|
3 | n/a | |
---|
4 | n/a | The script |
---|
5 | n/a | |
---|
6 | n/a | (1) downloads OpenSSL tar bundle |
---|
7 | n/a | (2) extracts it to ../openssl/src/openssl-VERSION/ |
---|
8 | n/a | (3) compiles OpenSSL |
---|
9 | n/a | (4) installs OpenSSL into ../openssl/VERSION/ |
---|
10 | n/a | (5) forces a recompilation of Python modules using the |
---|
11 | n/a | header and library files from ../openssl/VERSION/ |
---|
12 | n/a | (6) runs Python's test suite |
---|
13 | n/a | |
---|
14 | n/a | The script must be run with Python's build directory as current working |
---|
15 | n/a | directory: |
---|
16 | n/a | |
---|
17 | n/a | ./python Tools/ssl/test_multiple_versions.py |
---|
18 | n/a | |
---|
19 | n/a | The script uses LD_RUN_PATH, LD_LIBRARY_PATH, CPPFLAGS and LDFLAGS to bend |
---|
20 | n/a | search paths for header files and shared libraries. It's known to work on |
---|
21 | n/a | Linux with GCC 4.x. |
---|
22 | n/a | |
---|
23 | n/a | (c) 2013 Christian Heimes <christian@python.org> |
---|
24 | n/a | """ |
---|
25 | n/a | import logging |
---|
26 | n/a | import os |
---|
27 | n/a | import tarfile |
---|
28 | n/a | import shutil |
---|
29 | n/a | import subprocess |
---|
30 | n/a | import sys |
---|
31 | n/a | from urllib.request import urlopen |
---|
32 | n/a | |
---|
33 | n/a | log = logging.getLogger("multissl") |
---|
34 | n/a | |
---|
35 | n/a | OPENSSL_VERSIONS = [ |
---|
36 | n/a | "0.9.7m", "0.9.8i", "0.9.8l", "0.9.8m", "0.9.8y", "1.0.0k", "1.0.1e" |
---|
37 | n/a | ] |
---|
38 | n/a | FULL_TESTS = [ |
---|
39 | n/a | "test_asyncio", "test_ftplib", "test_hashlib", "test_httplib", |
---|
40 | n/a | "test_imaplib", "test_nntplib", "test_poplib", "test_smtplib", |
---|
41 | n/a | "test_smtpnet", "test_urllib2_localnet", "test_venv" |
---|
42 | n/a | ] |
---|
43 | n/a | MINIMAL_TESTS = ["test_ssl", "test_hashlib"] |
---|
44 | n/a | CADEFAULT = True |
---|
45 | n/a | HERE = os.path.abspath(os.getcwd()) |
---|
46 | n/a | DEST_DIR = os.path.abspath(os.path.join(HERE, os.pardir, "openssl")) |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | class BuildSSL: |
---|
50 | n/a | url_template = "https://www.openssl.org/source/openssl-{}.tar.gz" |
---|
51 | n/a | |
---|
52 | n/a | module_files = ["Modules/_ssl.c", |
---|
53 | n/a | "Modules/socketmodule.c", |
---|
54 | n/a | "Modules/_hashopenssl.c"] |
---|
55 | n/a | |
---|
56 | n/a | def __init__(self, version, openssl_compile_args=(), destdir=DEST_DIR): |
---|
57 | n/a | self._check_python_builddir() |
---|
58 | n/a | self.version = version |
---|
59 | n/a | self.openssl_compile_args = openssl_compile_args |
---|
60 | n/a | # installation directory |
---|
61 | n/a | self.install_dir = os.path.join(destdir, version) |
---|
62 | n/a | # source file |
---|
63 | n/a | self.src_file = os.path.join(destdir, "src", |
---|
64 | n/a | "openssl-{}.tar.gz".format(version)) |
---|
65 | n/a | # build directory (removed after install) |
---|
66 | n/a | self.build_dir = os.path.join(destdir, "src", |
---|
67 | n/a | "openssl-{}".format(version)) |
---|
68 | n/a | |
---|
69 | n/a | @property |
---|
70 | n/a | def openssl_cli(self): |
---|
71 | n/a | """openssl CLI binary""" |
---|
72 | n/a | return os.path.join(self.install_dir, "bin", "openssl") |
---|
73 | n/a | |
---|
74 | n/a | @property |
---|
75 | n/a | def openssl_version(self): |
---|
76 | n/a | """output of 'bin/openssl version'""" |
---|
77 | n/a | env = os.environ.copy() |
---|
78 | n/a | env["LD_LIBRARY_PATH"] = self.lib_dir |
---|
79 | n/a | cmd = [self.openssl_cli, "version"] |
---|
80 | n/a | return self._subprocess_output(cmd, env=env) |
---|
81 | n/a | |
---|
82 | n/a | @property |
---|
83 | n/a | def pyssl_version(self): |
---|
84 | n/a | """Value of ssl.OPENSSL_VERSION""" |
---|
85 | n/a | env = os.environ.copy() |
---|
86 | n/a | env["LD_LIBRARY_PATH"] = self.lib_dir |
---|
87 | n/a | cmd = ["./python", "-c", "import ssl; print(ssl.OPENSSL_VERSION)"] |
---|
88 | n/a | return self._subprocess_output(cmd, env=env) |
---|
89 | n/a | |
---|
90 | n/a | @property |
---|
91 | n/a | def include_dir(self): |
---|
92 | n/a | return os.path.join(self.install_dir, "include") |
---|
93 | n/a | |
---|
94 | n/a | @property |
---|
95 | n/a | def lib_dir(self): |
---|
96 | n/a | return os.path.join(self.install_dir, "lib") |
---|
97 | n/a | |
---|
98 | n/a | @property |
---|
99 | n/a | def has_openssl(self): |
---|
100 | n/a | return os.path.isfile(self.openssl_cli) |
---|
101 | n/a | |
---|
102 | n/a | @property |
---|
103 | n/a | def has_src(self): |
---|
104 | n/a | return os.path.isfile(self.src_file) |
---|
105 | n/a | |
---|
106 | n/a | def _subprocess_call(self, cmd, stdout=subprocess.DEVNULL, env=None, |
---|
107 | n/a | **kwargs): |
---|
108 | n/a | log.debug("Call '%s'", " ".join(cmd)) |
---|
109 | n/a | return subprocess.check_call(cmd, stdout=stdout, env=env, **kwargs) |
---|
110 | n/a | |
---|
111 | n/a | def _subprocess_output(self, cmd, env=None, **kwargs): |
---|
112 | n/a | log.debug("Call '%s'", " ".join(cmd)) |
---|
113 | n/a | out = subprocess.check_output(cmd, env=env) |
---|
114 | n/a | return out.strip().decode("utf-8") |
---|
115 | n/a | |
---|
116 | n/a | def _check_python_builddir(self): |
---|
117 | n/a | if not os.path.isfile("python") or not os.path.isfile("setup.py"): |
---|
118 | n/a | raise ValueError("Script must be run in Python build directory") |
---|
119 | n/a | |
---|
120 | n/a | def _download_openssl(self): |
---|
121 | n/a | """Download OpenSSL source dist""" |
---|
122 | n/a | src_dir = os.path.dirname(self.src_file) |
---|
123 | n/a | if not os.path.isdir(src_dir): |
---|
124 | n/a | os.makedirs(src_dir) |
---|
125 | n/a | url = self.url_template.format(self.version) |
---|
126 | n/a | log.info("Downloading OpenSSL from {}".format(url)) |
---|
127 | n/a | req = urlopen(url, cadefault=CADEFAULT) |
---|
128 | n/a | # KISS, read all, write all |
---|
129 | n/a | data = req.read() |
---|
130 | n/a | log.info("Storing {}".format(self.src_file)) |
---|
131 | n/a | with open(self.src_file, "wb") as f: |
---|
132 | n/a | f.write(data) |
---|
133 | n/a | |
---|
134 | n/a | def _unpack_openssl(self): |
---|
135 | n/a | """Unpack tar.gz bundle""" |
---|
136 | n/a | # cleanup |
---|
137 | n/a | if os.path.isdir(self.build_dir): |
---|
138 | n/a | shutil.rmtree(self.build_dir) |
---|
139 | n/a | os.makedirs(self.build_dir) |
---|
140 | n/a | |
---|
141 | n/a | tf = tarfile.open(self.src_file) |
---|
142 | n/a | base = "openssl-{}/".format(self.version) |
---|
143 | n/a | # force extraction into build dir |
---|
144 | n/a | members = tf.getmembers() |
---|
145 | n/a | for member in members: |
---|
146 | n/a | if not member.name.startswith(base): |
---|
147 | n/a | raise ValueError(member.name) |
---|
148 | n/a | member.name = member.name[len(base):] |
---|
149 | n/a | log.info("Unpacking files to {}".format(self.build_dir)) |
---|
150 | n/a | tf.extractall(self.build_dir, members) |
---|
151 | n/a | |
---|
152 | n/a | def _build_openssl(self): |
---|
153 | n/a | """Now build openssl""" |
---|
154 | n/a | log.info("Running build in {}".format(self.install_dir)) |
---|
155 | n/a | cwd = self.build_dir |
---|
156 | n/a | cmd = ["./config", "shared", "--prefix={}".format(self.install_dir)] |
---|
157 | n/a | cmd.extend(self.openssl_compile_args) |
---|
158 | n/a | self._subprocess_call(cmd, cwd=cwd) |
---|
159 | n/a | self._subprocess_call(["make"], cwd=cwd) |
---|
160 | n/a | |
---|
161 | n/a | def _install_openssl(self, remove=True): |
---|
162 | n/a | self._subprocess_call(["make", "install"], cwd=self.build_dir) |
---|
163 | n/a | if remove: |
---|
164 | n/a | shutil.rmtree(self.build_dir) |
---|
165 | n/a | |
---|
166 | n/a | def install_openssl(self): |
---|
167 | n/a | if not self.has_openssl: |
---|
168 | n/a | if not self.has_src: |
---|
169 | n/a | self._download_openssl() |
---|
170 | n/a | else: |
---|
171 | n/a | log.debug("Already has src %s", self.src_file) |
---|
172 | n/a | self._unpack_openssl() |
---|
173 | n/a | self._build_openssl() |
---|
174 | n/a | self._install_openssl() |
---|
175 | n/a | else: |
---|
176 | n/a | log.info("Already has installation {}".format(self.install_dir)) |
---|
177 | n/a | # validate installation |
---|
178 | n/a | version = self.openssl_version |
---|
179 | n/a | if self.version not in version: |
---|
180 | n/a | raise ValueError(version) |
---|
181 | n/a | |
---|
182 | n/a | def touch_pymods(self): |
---|
183 | n/a | # force a rebuild of all modules that use OpenSSL APIs |
---|
184 | n/a | for fname in self.module_files: |
---|
185 | n/a | os.utime(fname) |
---|
186 | n/a | |
---|
187 | n/a | def recompile_pymods(self): |
---|
188 | n/a | log.info("Using OpenSSL build from {}".format(self.build_dir)) |
---|
189 | n/a | # overwrite header and library search paths |
---|
190 | n/a | env = os.environ.copy() |
---|
191 | n/a | env["CPPFLAGS"] = "-I{}".format(self.include_dir) |
---|
192 | n/a | env["LDFLAGS"] = "-L{}".format(self.lib_dir) |
---|
193 | n/a | # set rpath |
---|
194 | n/a | env["LD_RUN_PATH"] = self.lib_dir |
---|
195 | n/a | |
---|
196 | n/a | log.info("Rebuilding Python modules") |
---|
197 | n/a | self.touch_pymods() |
---|
198 | n/a | cmd = ["./python", "setup.py", "build"] |
---|
199 | n/a | self._subprocess_call(cmd, env=env) |
---|
200 | n/a | |
---|
201 | n/a | def check_pyssl(self): |
---|
202 | n/a | version = self.pyssl_version |
---|
203 | n/a | if self.version not in version: |
---|
204 | n/a | raise ValueError(version) |
---|
205 | n/a | |
---|
206 | n/a | def run_pytests(self, *args): |
---|
207 | n/a | cmd = ["./python", "-m", "test"] |
---|
208 | n/a | cmd.extend(args) |
---|
209 | n/a | self._subprocess_call(cmd, stdout=None) |
---|
210 | n/a | |
---|
211 | n/a | def run_python_tests(self, *args): |
---|
212 | n/a | self.recompile_pymods() |
---|
213 | n/a | self.check_pyssl() |
---|
214 | n/a | self.run_pytests(*args) |
---|
215 | n/a | |
---|
216 | n/a | |
---|
217 | n/a | def main(*args): |
---|
218 | n/a | builders = [] |
---|
219 | n/a | for version in OPENSSL_VERSIONS: |
---|
220 | n/a | if version in ("0.9.8i", "0.9.8l"): |
---|
221 | n/a | openssl_compile_args = ("no-asm",) |
---|
222 | n/a | else: |
---|
223 | n/a | openssl_compile_args = () |
---|
224 | n/a | builder = BuildSSL(version, openssl_compile_args) |
---|
225 | n/a | builder.install_openssl() |
---|
226 | n/a | builders.append(builder) |
---|
227 | n/a | |
---|
228 | n/a | for builder in builders: |
---|
229 | n/a | builder.run_python_tests(*args) |
---|
230 | n/a | # final touch |
---|
231 | n/a | builder.touch_pymods() |
---|
232 | n/a | |
---|
233 | n/a | |
---|
234 | n/a | if __name__ == "__main__": |
---|
235 | n/a | logging.basicConfig(level=logging.INFO, |
---|
236 | n/a | format="*** %(levelname)s %(message)s") |
---|
237 | n/a | args = sys.argv[1:] |
---|
238 | n/a | if not args: |
---|
239 | n/a | args = ["-unetwork", "-v"] |
---|
240 | n/a | args.extend(FULL_TESTS) |
---|
241 | n/a | main(*args) |
---|