blob: 14a044d0cd274300fce93c55a6124a37be1b6ba3 [file] [log] [blame]
Brett Cannonfd074152012-04-14 14:10:13 -04001#! /usr/bin/env python
2"""Freeze importlib for use as the implementation of import."""
3import marshal
4
5
6header = """/* Auto-generated by Python/freeze_importlib.py */"""
7
8
9def main(input_path, output_path):
10 with open(input_path, 'r', encoding='utf-8') as input_file:
11 source = input_file.read()
12
13 code = compile(source, '<frozen importlib._bootstrap>', 'exec')
14
15 lines = [header]
16 lines.append('unsigned char _Py_M__importlib[] = {')
17 data = marshal.dumps(code)
18 # Code from Tools/freeze/makefreeze.py:writecode()
19 for i in range(0, len(data), 16):
20 line = [' ']
21 for c in data[i:i+16]:
22 line.append('%d,' % c)
23 lines.append(''.join(line))
24 lines.append('};\n')
Antoine Pitroua938c742012-04-16 18:30:54 +020025 with open(output_path, 'w', encoding='utf-8') as output_file:
Brett Cannonfd074152012-04-14 14:10:13 -040026 output_file.write('\n'.join(lines))
Antoine Pitroub84bc7a2012-05-16 12:58:04 +020027 # Avoid a compiler warning for lack of EOL
28 output_file.write('\n')
Brett Cannonfd074152012-04-14 14:10:13 -040029
30
31if __name__ == '__main__':
32 import sys
33
34 args = sys.argv[1:]
35 if len(args) != 2:
36 print('Need to specify input and output file paths', file=sys.stderr)
37 sys.exit(1)
38
39 main(*args)