Exemple #1
0
def instrumentModule(module_filename, out_dir, is_app=False, in_dir=""):

    mod_file = os.path.join(out_dir, module_filename)

    if os.path.exists(mod_file) and os.stat(
            os.path.join(
                in_dir,
                module_filename)).st_mtime < os.stat(mod_file).st_mtime:
        return

    print("Instrumenting %s" % module_filename)

    if "se_dict.py" in module_filename:
        import_se_dict = False
    else:
        import_se_dict = True

    module_contents = file(os.path.join(in_dir, module_filename), "U").read()

    if len(module_contents.strip()) == 0:
        file(mod_file, "w").close()
        return
    root_node = ast.parse(module_contents)
    SplitBoolOpPass1().visit(root_node)
    LiftComputationFromConditionalPass2().visit(root_node)
    BranchIdentifierPass3(import_se_dict).visit(root_node)
    ast.fix_missing_locations(root_node)
    compile(root_node, module_filename,
            'exec')  # to make sure the new AST is ok
    unparse.Unparser(root_node, file(mod_file, "w"))
Exemple #2
0
 def check_roundtrip(self, code1, filename="internal"):
     ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST)
     unparse_buffer = io.StringIO()
     unparse.Unparser(ast1, unparse_buffer)
     code2 = unparse_buffer.getvalue()
     ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST)
     self.assertASTEqual(ast1, ast2)
Exemple #3
0
 def emit(self, out):
     import io
     f = io.StringIO()
     unparse.Unparser(self.params, f)
     src = f.getvalue()
     for line in src.split('\n'):
         out(line)
Exemple #4
0
    def translate(self, source):
        parsed_code = ast.parse(source)
        safe_loops = self.get_safe_loops_fn(parsed_code)
        if not safe_loops:
            print("No safe loops found")
        print(safe_loops)
        print(safe_loops[0].all_statements)
        print(ast.dump(parsed_code))
        transformer = self.Transformer(ast.increment_lineno(parsed_code, 100),
                                       safe_loops)
        transformed_tree = transformer.transform_tree()
        #        transformed_tree.lineno =1
        #       transformed_tree.col_offset = 0
        print(type(transformed_tree))
        transformed_tree = ast.fix_missing_locations(transformed_tree)

        # print(ast.dump(transformed_tree))
        # for node in ast.walk(transformed_tree):
        #     if isinstance(node, ast.expr) or isinstance(node, ast.stmt):
        #         print(node.lineno)
        #         print(node)

        unparse.Unparser(transformed_tree, sys.stdout)
        output_code = compile(transformed_tree, self.filename, 'exec')
        pmod = ParallelModule()
        try:
            with open('output.py', 'w') as f:
                unparse.Unparser(transformed_tree, f)
        #    f.write(imp.get_magic())
        #   marshal.dump(output_code, f)
        #    exec(output_code,{})

        except:
            print(sys.exc_info()[0])
            print(sys.exc_info()[1])
            print(sys.exc_info()[2])
Exemple #5
0
def reorderVariables(src, toggleAugmented):
    try:
        #filename = argv[0]
        isAugAssign = toggleAugmented  #int(argv[1])
        #print isAugAssign
        fileAST = ast.parse(src)  #get_ast_from_file(filename)
        #print fileAST
        newFileAST = RewriteAST(isAugAssign).visit(fileAST)
        #print newFileAST

        with open('temp.txt', 'w') as myf:
            unparse.Unparser(newFileAST, myf)
        with open('temp.txt', 'r') as myf:
            return myf.read()
        #print ""
    except:
        print "Usage python rewrite.py <fileName> <0 (no aug assign rewriting) or 1>"
Exemple #6
0
def instrumentModule(module_filename, out_dir, is_app=False, in_dir=""):
    (dirname, mod_name) = os.path.split(module_filename)
    if len(dirname) > 0:
        out_dir = os.path.join(out_dir, dirname)
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)

    mod_file = os.path.join(out_dir, mod_name)

    if os.path.exists(mod_file) and os.stat(
            os.path.join(
                in_dir,
                module_filename)).st_mtime < os.stat(mod_file).st_mtime:
        return
    print "Instrumenting %s" % module_filename
    if "se_dict.py" in module_filename:
        import_se_dict = False
    else:
        import_se_dict = True

    module_contents = file(os.path.join(in_dir, module_filename), "U").read()
    if len(module_contents.strip()) == 0:
        file(mod_file, "w").close()
        return
    root_node = ast.parse(module_contents)
    SplitBoolOpPass1().visit(root_node)
    MoveFunctionCallsPass2().visit(root_node)
    BranchIdentifierPass3(import_se_dict).visit(root_node)
    if is_app:
        NoxAppPass4().visit(root_node)
    NotBugfixPass().visit(root_node)
    ast.fix_missing_locations(root_node)

    compile(root_node, module_filename,
            'exec')  # to make sure the new AST is ok

    unparse.Unparser(root_node, file(mod_file, "w"))
        if type(v) is dict:
            # pprint.pprint({"Before ref": v })
            v2 = rec(v, i + 1)
            attrs[n] = v2

        elif type(v) in str:
            if n in ("ntype", "type", "scpe", "chain"):
                pass
            elif v == "":
                pass
            else:
                if "link:" in v:
                    v = v.replace("link:", "")
            attrs[n] = v
    attrs["rdf:type"] = t

    # pprint.pprint({'f':f,'i':attrs})
    r = f(**attrs)
    # debug the input and output
    # pprint.pprint({'o':r,'i':attrs})
    return r


new_ast = rec(data.body2.deep)

# now lets try and unparse it....
unparse.Unparser(new_ast, sys.stdout)

# for x in f:
#    print "def %s(**kwargs):\n                pass" % x
Exemple #8
0
 def dounparse(tree):
     output = io.StringIO()
     unparse.Unparser(tree, output)
     v = output.getvalue()
     output.close()
     return v
Exemple #9
0
 def src_dump(self, code):
     import unparse
     print "src dump:"
     unparse.Unparser(code)