1 | n/a | import marshal |
---|
2 | n/a | import bkfile |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | # Write a file containing frozen code for the modules in the dictionary. |
---|
6 | n/a | |
---|
7 | n/a | header = """ |
---|
8 | n/a | #include "Python.h" |
---|
9 | n/a | |
---|
10 | n/a | static struct _frozen _PyImport_FrozenModules[] = { |
---|
11 | n/a | """ |
---|
12 | n/a | trailer = """\ |
---|
13 | n/a | {0, 0, 0} /* sentinel */ |
---|
14 | n/a | }; |
---|
15 | n/a | """ |
---|
16 | n/a | |
---|
17 | n/a | # if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app. |
---|
18 | n/a | default_entry_point = """ |
---|
19 | n/a | int |
---|
20 | n/a | main(int argc, char **argv) |
---|
21 | n/a | { |
---|
22 | n/a | extern int Py_FrozenMain(int, char **); |
---|
23 | n/a | """ + ((not __debug__ and """ |
---|
24 | n/a | Py_OptimizeFlag++; |
---|
25 | n/a | """) or "") + """ |
---|
26 | n/a | PyImport_FrozenModules = _PyImport_FrozenModules; |
---|
27 | n/a | return Py_FrozenMain(argc, argv); |
---|
28 | n/a | } |
---|
29 | n/a | |
---|
30 | n/a | """ |
---|
31 | n/a | |
---|
32 | n/a | def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()): |
---|
33 | n/a | if entry_point is None: entry_point = default_entry_point |
---|
34 | n/a | done = [] |
---|
35 | n/a | files = [] |
---|
36 | n/a | mods = sorted(dict.keys()) |
---|
37 | n/a | for mod in mods: |
---|
38 | n/a | m = dict[mod] |
---|
39 | n/a | mangled = "__".join(mod.split(".")) |
---|
40 | n/a | if m.__code__: |
---|
41 | n/a | file = 'M_' + mangled + '.c' |
---|
42 | n/a | with bkfile.open(base + file, 'w') as outfp: |
---|
43 | n/a | files.append(file) |
---|
44 | n/a | if debug: |
---|
45 | n/a | print("freezing", mod, "...") |
---|
46 | n/a | str = marshal.dumps(m.__code__) |
---|
47 | n/a | size = len(str) |
---|
48 | n/a | if m.__path__: |
---|
49 | n/a | # Indicate package by negative size |
---|
50 | n/a | size = -size |
---|
51 | n/a | done.append((mod, mangled, size)) |
---|
52 | n/a | writecode(outfp, mangled, str) |
---|
53 | n/a | if debug: |
---|
54 | n/a | print("generating table of frozen modules") |
---|
55 | n/a | with bkfile.open(base + 'frozen.c', 'w') as outfp: |
---|
56 | n/a | for mod, mangled, size in done: |
---|
57 | n/a | outfp.write('extern unsigned char M_%s[];\n' % mangled) |
---|
58 | n/a | outfp.write(header) |
---|
59 | n/a | for mod, mangled, size in done: |
---|
60 | n/a | outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) |
---|
61 | n/a | outfp.write('\n') |
---|
62 | n/a | # The following modules have a NULL code pointer, indicating |
---|
63 | n/a | # that the frozen program should not search for them on the host |
---|
64 | n/a | # system. Importing them will *always* raise an ImportError. |
---|
65 | n/a | # The zero value size is never used. |
---|
66 | n/a | for mod in fail_import: |
---|
67 | n/a | outfp.write('\t{"%s", NULL, 0},\n' % (mod,)) |
---|
68 | n/a | outfp.write(trailer) |
---|
69 | n/a | outfp.write(entry_point) |
---|
70 | n/a | return files |
---|
71 | n/a | |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | # Write a C initializer for a module containing the frozen python code. |
---|
75 | n/a | # The array is called M_<mod>. |
---|
76 | n/a | |
---|
77 | n/a | def writecode(outfp, mod, str): |
---|
78 | n/a | outfp.write('unsigned char M_%s[] = {' % mod) |
---|
79 | n/a | for i in range(0, len(str), 16): |
---|
80 | n/a | outfp.write('\n\t') |
---|
81 | n/a | for c in bytes(str[i:i+16]): |
---|
82 | n/a | outfp.write('%d,' % c) |
---|
83 | n/a | outfp.write('\n};\n') |
---|
84 | n/a | |
---|
85 | n/a | ## def writecode(outfp, mod, str): |
---|
86 | n/a | ## outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str), |
---|
87 | n/a | ## '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"'))))) |
---|