| 1 | import marshal
|
|---|
| 2 | import bkfile
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | # Write a file containing frozen code for the modules in the dictionary.
|
|---|
| 6 |
|
|---|
| 7 | header = """
|
|---|
| 8 | #include "Python.h"
|
|---|
| 9 |
|
|---|
| 10 | static struct _frozen _PyImport_FrozenModules[] = {
|
|---|
| 11 | """
|
|---|
| 12 | trailer = """\
|
|---|
| 13 | {0, 0, 0} /* sentinel */
|
|---|
| 14 | };
|
|---|
| 15 | """
|
|---|
| 16 |
|
|---|
| 17 | # if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app.
|
|---|
| 18 | default_entry_point = """
|
|---|
| 19 | int
|
|---|
| 20 | main(int argc, char **argv)
|
|---|
| 21 | {
|
|---|
| 22 | extern int Py_FrozenMain(int, char **);
|
|---|
| 23 | """ + ((not __debug__ and """
|
|---|
| 24 | Py_OptimizeFlag++;
|
|---|
| 25 | """) or "") + """
|
|---|
| 26 | PyImport_FrozenModules = _PyImport_FrozenModules;
|
|---|
| 27 | return Py_FrozenMain(argc, argv);
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | """
|
|---|
| 31 |
|
|---|
| 32 | def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
|
|---|
| 33 | if entry_point is None: entry_point = default_entry_point
|
|---|
| 34 | done = []
|
|---|
| 35 | files = []
|
|---|
| 36 | mods = dict.keys()
|
|---|
| 37 | mods.sort()
|
|---|
| 38 | for mod in mods:
|
|---|
| 39 | m = dict[mod]
|
|---|
| 40 | mangled = "__".join(mod.split("."))
|
|---|
| 41 | if m.__code__:
|
|---|
| 42 | file = 'M_' + mangled + '.c'
|
|---|
| 43 | outfp = bkfile.open(base + file, 'w')
|
|---|
| 44 | files.append(file)
|
|---|
| 45 | if debug:
|
|---|
| 46 | print "freezing", mod, "..."
|
|---|
| 47 | str = marshal.dumps(m.__code__)
|
|---|
|
|---|