"""Formats the speed tests generated by automate_test.py in a text file
bench_results.txt
"""

import os
import locale

locale.setlocale(locale.LC_ALL, '')

# take CPython test as base
cpython = [f for f in os.listdir() if f.startswith('cpython_')][0]
base = {}
for line in open(cpython).readlines():
    script, t = line.split(':')
    base[script] = float(t)

tests = [f for f in os.listdir() if f.startswith('bench_result_')]
res = {}
for test in tests:
    version = test[len('bench_result_'):-4]
    print(version)
    res[version] = {}
    for line in open(test).readlines():
        line = line.strip()
        if line:
            script, t = line.split(':')
            res[version][script] = float(t)/base[script]

out = open('bench_result.txt', 'w')
versions = list(res.keys())
versions.sort()
scripts = list(res[versions[0]].keys())
scripts.sort()
maxlen = max(len(script) for script in scripts)
fmt = '%%%ss' %maxlen

out.write(fmt %'Test'+' '+' '.join('% 6s' %v for v in versions)+'\n')

for script in scripts:
    out.write(fmt % script+' '+
        ' '.join(locale.format_string('% 6.2f', float(res[v][script]))
             for v in versions)
        +'\n')
out.close()
