blob: c012722a42b94fa43c4c2e02aaf4ad05f9df2365 [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 Pitroua938c742012-04-16 18:30:54 +020027 output_file.write('/* Mercurial binary marker: \x00 */')
Brett Cannonfd074152012-04-14 14:10:13 -040028
29
30if __name__ == '__main__':
31 import sys
32
33 args = sys.argv[1:]
34 if len(args) != 2:
35 print('Need to specify input and output file paths', file=sys.stderr)
36 sys.exit(1)
37
38 main(*args)