Beispiel #1
0
def main(code_path,
         jy_exe="jython",
         testfile=False,
         print_diff=True,
         print_fail=False,
         print_success=False,
         print_diff_lines=False):
    from pprint import pprint
    from popen2 import popen2
    from StringIO import StringIO
    from difflib import Differ

    if testfile:
        pyfiles = [f.rstrip() for f in file(code_path)]
    elif os.path.isdir(code_path):
        pyfiles = globwalk.GlobDirectoryWalker(code_path, "*.py")
    else:
        pyfiles = [code_path]

    for pyfile in pyfiles:
        ast = compile(open(pyfile).read(), pyfile, "exec", _ast.PyCF_ONLY_AST)
        lispified = lispify_ast(ast)
        sio = StringIO()
        pprint(lispified, stream=sio)

        fin, fout = popen2("%s jastlib.py %s" % (jy_exe, pyfile))

        sio.seek(0)
        pstr = sio.readlines()
        jstr = fin.readlines()

        differs = False
        diffstr = []
        difflines = 0
        diff = Differ()
        results = diff.compare(pstr, jstr)
        for d in results:
            diffstr.append(d)
            if d[0] in ['+', '-']:
                differs = True
                difflines += 1

        if print_success and not differs:
            print "SUCCESS: %s" % pyfile

        if print_fail and differs:
            print "FAIL: %s" % pyfile

        if print_diff_lines:
            print "%s diff lines in %s" % (difflines, pyfile)

        if print_diff and differs:
            print "---------- ouput -------------"
            print "py: %s" % sio.getvalue()
            print "jy: %s" % "".join(jstr)
            print "---------- DIFF -------------"
            print "".join(diffstr)
Beispiel #2
0
def main(code_path, output_dir, testfile=False):
    if os.path.exists(output_dir):
        print "%s already exists, exiting" % output_dir
        sys.exit(1)
    os.mkdir(output_dir)
    if testfile:
        pyfiles = [f.rstrip() for f in file(code_path)]
    elif os.path.isdir(code_path):
        pyfiles = globwalk.GlobDirectoryWalker(code_path, "*.py")
    else:
        pyfiles = [code_path]

    for pyfile in pyfiles:
        import pprint
        path = pyfile.split(os.path.sep)
        print "%s to %s: %s" % (pyfile, output_dir,
                                os.path.join(output_dir, *path))
        fh = open(makepath(os.path.join(output_dir, *path)), 'w')
        print fh
        pprint.pprint(astview.tree(pyfile), fh)