Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Freeze importlib for use as the implementation of import.""" |
| 3 | import marshal |
| 4 | |
| 5 | |
| 6 | header = """/* Auto-generated by Python/freeze_importlib.py */""" |
| 7 | |
| 8 | |
| 9 | def 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 Pitrou | a938c74 | 2012-04-16 18:30:54 +0200 | [diff] [blame] | 25 | with open(output_path, 'w', encoding='utf-8') as output_file: |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 26 | output_file.write('\n'.join(lines)) |
Antoine Pitrou | a938c74 | 2012-04-16 18:30:54 +0200 | [diff] [blame] | 27 | output_file.write('/* Mercurial binary marker: \x00 */') |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 28 | |
| 29 | |
| 30 | if __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) |