OILS / opy / misc / stdlib_compile.py View on Github | oilshell.org

56 lines, 24 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4stdlib_compile.py
5"""
6
7import compiler as stdlib_comp
8import imp
9#import os
10import marshal
11import struct
12import sys
13
14#import hashlib
15
16
17def 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
28def 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
48def main(argv):
49 in_path = argv[1]
50 out_path = argv[2]
51
52 compileAndWrite(in_path, out_path, stdlib_comp.compile)
53
54
55if __name__ == '__main__':
56 main(sys.argv)