|
| 1 | +# Convert project from Python 2 to Python 3 and test that JSON traces are |
| 2 | +# unchanged for all examples. |
| 3 | +# |
| 4 | +# python3 convert_2to3.py |
| 5 | +# |
| 6 | +# Runs under Python 2.7 and Python 3.x without 2to3 conversion |
| 7 | +# |
| 8 | +# Created by John DeNero |
| 9 | + |
| 10 | +import generate_json_trace |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import json |
| 14 | + |
| 15 | + |
| 16 | +def write_trace(python, example_path, output_path): |
| 17 | + '''Use system call to generate a JSON trace using some python binary.''' |
| 18 | + example = os.path.split(example_path)[1] |
| 19 | + print('Generating JSON for "{0}" with {1}'.format(example, python)) |
| 20 | + cmd = '{0} generate_json_trace.py {1} > {2}' |
| 21 | + os.system(cmd.format(python, example_path, output_path)) |
| 22 | + |
| 23 | + |
| 24 | +def write_py2_traces(examples_dir, traces_dir): |
| 25 | + '''Write JSON traces for all examples using Python 2.7.''' |
| 26 | + for path, _, filenames in os.walk(examples_dir): |
| 27 | + for example in filenames: |
| 28 | + example_path = os.path.join(path, example) |
| 29 | + outfile = path.replace(os.path.sep, '_') + '_' + example |
| 30 | + output_path = os.path.join(traces_dir, outfile) |
| 31 | + write_trace('python2.7', example_path, output_path) |
| 32 | + |
| 33 | + |
| 34 | +def verify_py3_traces(examples_dir, traces_dir): |
| 35 | + '''Write and compare JSON traces for all examples using Python 3.''' |
| 36 | + diffs = [] |
| 37 | + for path, _, filenames in os.walk(examples_dir): |
| 38 | + for example in filenames: |
| 39 | + example_path = os.path.join(path, example) |
| 40 | + outfile = path.replace(os.path.sep, '_') + '_' + example |
| 41 | + output_path = os.path.join(traces_dir, outfile + '.py3k') |
| 42 | + write_trace('python3', example_path, output_path) |
| 43 | + |
| 44 | + py2_path = os.path.join(traces_dir, outfile) |
| 45 | + py2_result = json.load(open(py2_path)) |
| 46 | + py3_result = json.load(open(output_path)) |
| 47 | + if py2_result['trace'] != py3_result['trace']: |
| 48 | + diffs.append(example) |
| 49 | + return diffs |
| 50 | + |
| 51 | + |
| 52 | +known_differences = """Known differences include: |
| 53 | +
|
| 54 | +fib.txt: "while True:" is evaluated once in Python 3, but repeatedly in Python. |
| 55 | +
|
| 56 | +map.txt: 2to3 converts call to map() to a list comprehension. |
| 57 | +
|
| 58 | +OOP*.txt, ll2.txt: __init__ functions orphan a __locals__ dict on the heap in |
| 59 | + Python 3, but it is not rendered in the front end. |
| 60 | +
|
| 61 | +wentworth_try_finally.txt: Python 3 integer division is true, not floor. |
| 62 | +""" |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + examples_dir = 'example-code' |
| 66 | + if not os.path.exists(examples_dir): |
| 67 | + print('Examples directory {0} does not exist.'.format(examples_dir)) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + traces_dir = examples_dir + '-traces' |
| 71 | + if os.path.exists(traces_dir): |
| 72 | + print('Testing directory {0} already exists.'.format(traces_dir)) |
| 73 | + sys.exit(1) |
| 74 | + os.mkdir(traces_dir) |
| 75 | + |
| 76 | + write_py2_traces(examples_dir, traces_dir) |
| 77 | + |
| 78 | + # Convert examples to Python 3 |
| 79 | + os.system('2to3 -w -n --no-diffs {0}/*.txt {0}/*/*.txt'.format(examples_dir)) |
| 80 | + |
| 81 | + diffs = verify_py3_traces(examples_dir, traces_dir) |
| 82 | + |
| 83 | + if diffs: |
| 84 | + print('Trace differences for: {0}'.format(diffs)) |
| 85 | + print('See {0} for traces.'.format(traces_dir)) |
| 86 | + print(known_differences) |
| 87 | + else: |
| 88 | + print('Traces are identical; cleaning up') |
| 89 | + shutil.rm(traces_dir) |
| 90 | + |
0 commit comments