"""Generate correct output from the test cases

Puts program output in the output directory.  Puts generated bytecode
in the bytecode directory.
"""

import os

outputDir = 'output'
bytecodeDir = 'bytecode'

def remove_pyc(dir):
    for file in os.listdir(dir):
        base, ext = os.path.splitext(file)
        if ext == '.pyc':
            os.unlink(os.path.join(dir, file))

def list_tests(dir):
    tests = []
    for file in os.listdir(dir):
        base, ext = os.path.splitext(file)
        if ext == '.py' and base[:5] == 'test_':
            tests.append(file)
    return tests

def main(dir="."):
    remove_pyc(dir)
    for test in list_tests(dir):
        base, ext = os.path.splitext(test)
        print base
        try:
            __import__(base)
        except Exception, err:
            print "%s raised exception: %s" % (base, err)
        os.system("cp %sc %s/" % (test, bytecodeDir))
        os.system("python %s > %s/%s.txt 2>&1" % (test, outputDir, base))
        
if __name__ == "__main__":
    main()
