ยปCore Development>Code coverage>PC/VC6/build_ssl.py

Python code coverage for PC/VC6/build_ssl.py

#countcontent
1n/a# Script for building the _ssl module for Windows.
2n/a# Uses Perl to setup the OpenSSL environment correctly
3n/a# and build OpenSSL, then invokes a simple nmake session
4n/a# for _ssl.pyd itself.
5n/a
6n/a# THEORETICALLY, you can:
7n/a# * Unpack the latest SSL release one level above your main Python source
8n/a# directory. It is likely you will already find the zlib library and
9n/a# any other external packages there.
10n/a# * Install ActivePerl and ensure it is somewhere on your path.
11n/a# * Run this script from the PC/VC6 directory.
12n/a#
13n/a# it should configure and build SSL, then build the ssl Python extension
14n/a# without intervention.
15n/a
16n/a# Modified by Christian Heimes
17n/a# Now this script supports pre-generated makefiles and assembly files.
18n/a# Developers don't need an installation of Perl anymore to build Python. A svn
19n/a# checkout from our svn repository is enough.
20n/a
21n/aimport os, sys, re, shutil
22n/a
23n/a# Find all "foo.exe" files on the PATH.
24n/adef find_all_on_path(filename, extras = None):
25n/a entries = os.environ["PATH"].split(os.pathsep)
26n/a ret = []
27n/a for p in entries:
28n/a fname = os.path.abspath(os.path.join(p, filename))
29n/a if os.path.isfile(fname) and fname not in ret:
30n/a ret.append(fname)
31n/a if extras:
32n/a for p in extras:
33n/a fname = os.path.abspath(os.path.join(p, filename))
34n/a if os.path.isfile(fname) and fname not in ret:
35n/a ret.append(fname)
36n/a return ret
37n/a
38n/a# Find a suitable Perl installation for OpenSSL.
39n/a# cygwin perl does *not* work. ActivePerl does.
40n/a# Being a Perl dummy, the simplest way I can check is if the "Win32" package
41n/a# is available.
42n/adef find_working_perl(perls):
43n/a for perl in perls:
44n/a fh = os.popen('"%s" -e "use Win32;"' % perl)
45n/a fh.read()
46n/a rc = fh.close()
47n/a if rc:
48n/a continue
49n/a return perl
50n/a print("Can not find a suitable PERL:")
51n/a if perls:
52n/a print(" the following perl interpreters were found:")
53n/a for p in perls:
54n/a print(" ", p)
55n/a print(" None of these versions appear suitable for building OpenSSL")
56n/a else:
57n/a print(" NO perl interpreters were found on this machine at all!")
58n/a print(" Please install ActivePerl and ensure it appears on your path")
59n/a return None
60n/a
61n/a# Locate the best SSL directory given a few roots to look into.
62n/adef find_best_ssl_dir(sources):
63n/a candidates = []
64n/a for s in sources:
65n/a try:
66n/a # note: do not abspath s; the build will fail if any
67n/a # higher up directory name has spaces in it.
68n/a fnames = os.listdir(s)
69n/a except OSError:
70n/a fnames = []
71n/a for fname in fnames:
72n/a fqn = os.path.join(s, fname)
73n/a if os.path.isdir(fqn) and fname.startswith("openssl-"):
74n/a candidates.append(fqn)
75n/a # Now we have all the candidates, locate the best.
76n/a best_parts = []
77n/a best_name = None
78n/a for c in candidates:
79n/a parts = re.split("[.-]", os.path.basename(c))[1:]
80n/a # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers
81n/a if len(parts) >= 4:
82n/a continue
83n/a if parts > best_parts:
84n/a best_parts = parts
85n/a best_name = c
86n/a if best_name is not None:
87n/a print("Found an SSL directory at '%s'" % (best_name,))
88n/a else:
89n/a print("Could not find an SSL directory in '%s'" % (sources,))
90n/a sys.stdout.flush()
91n/a return best_name
92n/a
93n/adef fix_makefile(makefile):
94n/a """Fix some stuff in all makefiles
95n/a """
96n/a if not os.path.isfile(makefile):
97n/a return
98n/a with open(makefile) as fin:
99n/a lines = fin.readlines()
100n/a with open(makefile, 'w') as fout:
101n/a for line in lines:
102n/a if line.startswith("PERL="):
103n/a continue
104n/a if line.startswith("CP="):
105n/a line = "CP=copy\n"
106n/a if line.startswith("MKDIR="):
107n/a line = "MKDIR=mkdir\n"
108n/a if line.startswith("CFLAG="):
109n/a line = line.strip()
110n/a for algo in ("RC5", "MDC2", "IDEA"):
111n/a noalgo = " -DOPENSSL_NO_%s" % algo
112n/a if noalgo not in line:
113n/a line = line + noalgo
114n/a line = line + '\n'
115n/a fout.write(line)
116n/a
117n/adef run_configure(configure, do_script):
118n/a print("perl Configure "+configure)
119n/a os.system("perl Configure "+configure)
120n/a print(do_script)
121n/a os.system(do_script)
122n/a
123n/adef cmp(f1, f2):
124n/a bufsize = 1024 * 8
125n/a with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
126n/a while True:
127n/a b1 = fp1.read(bufsize)
128n/a b2 = fp2.read(bufsize)
129n/a if b1 != b2:
130n/a return False
131n/a if not b1:
132n/a return True
133n/a
134n/adef copy(src, dst):
135n/a if os.path.isfile(dst) and cmp(src, dst):
136n/a return
137n/a shutil.copy(src, dst)
138n/a
139n/adef main():
140n/a debug = "-d" in sys.argv
141n/a build_all = "-a" in sys.argv
142n/a if 1: # Win32
143n/a arch = "x86"
144n/a configure = "VC-WIN32"
145n/a do_script = "ms\\do_nasm"
146n/a makefile="ms\\nt.mak"
147n/a m32 = makefile
148n/a dirsuffix = "32"
149n/a configure += " no-idea no-rc5 no-mdc2"
150n/a make_flags = ""
151n/a if build_all:
152n/a make_flags = "-a"
153n/a # perl should be on the path, but we also look in "\perl" and "c:\\perl"
154n/a # as "well known" locations
155n/a perls = find_all_on_path("perl.exe", ["\\perl\\bin", "C:\\perl\\bin"])
156n/a perl = find_working_perl(perls)
157n/a if perl:
158n/a print("Found a working perl at '%s'" % (perl,))
159n/a else:
160n/a print("No Perl installation was found. Existing Makefiles are used.")
161n/a sys.stdout.flush()
162n/a # Look for SSL 3 levels up from PC/VC6 - ie, same place zlib etc all live.
163n/a ssl_dir = find_best_ssl_dir(("..\\..\\..",))
164n/a if ssl_dir is None:
165n/a sys.exit(1)
166n/a
167n/a old_cd = os.getcwd()
168n/a try:
169n/a os.chdir(ssl_dir)
170n/a # If the ssl makefiles do not exist, we invoke Perl to generate them.
171n/a # Due to a bug in this script, the makefile sometimes ended up empty
172n/a # Force a regeneration if it is.
173n/a if not os.path.isfile(makefile) or os.path.getsize(makefile)==0:
174n/a if perl is None:
175n/a print("Perl is required to build the makefiles!")
176n/a sys.exit(1)
177n/a
178n/a print("Creating the makefiles...")
179n/a sys.stdout.flush()
180n/a # Put our working Perl at the front of our path
181n/a os.environ["PATH"] = os.path.dirname(perl) + \
182n/a os.pathsep + \
183n/a os.environ["PATH"]
184n/a run_configure(configure, do_script)
185n/a if debug:
186n/a print("OpenSSL debug builds aren't supported.")
187n/a #if arch=="x86" and debug:
188n/a # # the do_masm script in openssl doesn't generate a debug
189n/a # # build makefile so we generate it here:
190n/a # os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile)
191n/a
192n/a fix_makefile(makefile)
193n/a copy(r"crypto\buildinf.h", r"crypto\buildinf_%s.h" % arch)
194n/a copy(r"crypto\opensslconf.h", r"crypto\opensslconf_%s.h" % arch)
195n/a
196n/a # If the assembler files don't exist in tmpXX, copy them there
197n/a if perl is None and os.path.exists("asm"+dirsuffix):
198n/a if not os.path.exists("tmp"+dirsuffix):
199n/a os.mkdir("tmp"+dirsuffix)
200n/a for f in os.listdir("asm"+dirsuffix):
201n/a if not f.endswith(".asm"): continue
202n/a if os.path.isfile(r"tmp%s\%s" % (dirsuffix, f)): continue
203n/a shutil.copy(r"asm%s\%s" % (dirsuffix, f), "tmp"+dirsuffix)
204n/a
205n/a # Now run make.
206n/a copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h")
207n/a copy(r"crypto\opensslconf_%s.h" % arch, r"crypto\opensslconf.h")
208n/a
209n/a #makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile)
210n/a makeCommand = "nmake /nologo -f \"%s\"" % makefile
211n/a print("Executing ssl makefiles:", makeCommand)
212n/a sys.stdout.flush()
213n/a rc = os.system(makeCommand)
214n/a if rc:
215n/a print("Executing "+makefile+" failed")
216n/a print(rc)
217n/a sys.exit(rc)
218n/a finally:
219n/a os.chdir(old_cd)
220n/a # And finally, we can build the _ssl module itself for Python.
221n/a defs = "SSL_DIR=%s" % (ssl_dir,)
222n/a if debug:
223n/a defs = defs + " " + "DEBUG=1"
224n/a rc = os.system('nmake /nologo -f _ssl.mak ' + defs + " " + make_flags)
225n/a sys.exit(rc)
226n/a
227n/aif __name__=='__main__':
228n/a main()