1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """
|
4 | stdlib_compile.py
|
5 | """
|
6 |
|
7 | import compiler as stdlib_comp
|
8 | import imp
|
9 | #import os
|
10 | import marshal
|
11 | import struct
|
12 | import sys
|
13 |
|
14 | #import hashlib
|
15 |
|
16 |
|
17 | def getPycHeader(filename):
|
18 | # compile.c uses marshal to write a long directly, with
|
19 | # calling the interface that would also generate a 1-byte code
|
20 | # to indicate the type of the value. simplest way to get the
|
21 | # same effect is to call marshal and then skip the code.
|
22 | #mtime = os.path.getmtime(filename)
|
23 | mtime = 0 # to make it deterministic for now
|
24 | mtime = struct.pack('<i', int(mtime))
|
25 | return imp.get_magic() + mtime
|
26 |
|
27 |
|
28 | def compileAndWrite(in_path, out_path, compile_func):
|
29 | #print(stdlib_comp, file=sys.stderr)
|
30 |
|
31 | with open(in_path) as f:
|
32 | co = compile_func(f.read(), in_path, 'exec')
|
33 |
|
34 | #print(co, file=sys.stderr)
|
35 |
|
36 | h = getPycHeader(in_path)
|
37 | with open(out_path, 'w') as out_f:
|
38 | out_f.write(h)
|
39 |
|
40 | s = marshal.dumps(co)
|
41 | #m = hashlib.md5()
|
42 | #m.update(s)
|
43 | #print(m.hexdigest(), file=sys.stderr)
|
44 |
|
45 | out_f.write(s)
|
46 |
|
47 |
|
48 | def main(argv):
|
49 | in_path = argv[1]
|
50 | out_path = argv[2]
|
51 |
|
52 | compileAndWrite(in_path, out_path, stdlib_comp.compile)
|
53 |
|
54 |
|
55 | if __name__ == '__main__':
|
56 | main(sys.argv)
|