Exemple #1
0
def ast_tool(args):
    print("Reconstructing AST %r" % (args.input.name,), file=sys.stderr)
    
    mod_ast = deserialize(json.load(args.input))
    
    code = compile(mod_ast, args.input.name, mode='exec', dont_inherit=True)
    
    if args.output_type == 'opcode':
        print_code(code)
        return 
    elif args.output_type == 'ast':
        json.dump(serialize(mod_ast), args.output, indent=2)
        return 
    elif args.output_type == 'python':
        python_source(mod_ast, file=args.output)
        return
    elif args.output_type == 'pyc':
        
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer

        try:
            timestamp = int(os.fstat(args.input.fileno()).st_mtime)
        except AttributeError:
            timestamp = int(os.stat(args.input.name).st_mtime)
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer
        dump_pyc(code, args.output, timestamp=timestamp)
    else:
        raise  Exception("unknow output type %r" % args.output_type)

    return
Exemple #2
0
def src_tool(args):
    print("Analysing python module %r" % (args.input.name,), file=sys.stderr)
    
    source = args.input.read()
    mod_ast = ast.parse(source, args.input.name)
    code = compile(source, args.input.name, mode='exec', dont_inherit=True)
    
    if args.output_type == 'opcode':
        print_code(code)
        return 
    elif args.output_type == 'ast':
        json.dump(serialize(mod_ast), args.output, indent=2)
        return 
    elif args.output_type == 'python':
        print(source.decode(), file=args.output)
    elif args.output_type == 'pyc':
        
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer

        try:
            timestamp = int(os.fstat(args.input.fileno()).st_mtime)
        except AttributeError:
            timestamp = int(os.stat(args.input.name).st_mtime)
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer
        codeobject = compile(source, '<recompile>', 'exec')
        dump_pyc(codeobject, args.output, timestamp=timestamp)
    else:
        raise  Exception("unknow output type %r" % args.output_type)

    return
Exemple #3
0
def src_tool(args):
    print("Analysing python module %r" % (args.input.name,), file=sys.stderr)
    
    source = args.input.read()
    mod_ast = ast.parse(source, args.input.name)
    code = compile(source, args.input.name, mode='exec', dont_inherit=True)
    
    if args.output_type == 'opcode':
        print_code(code)
        return 
    elif args.output_type == 'ast':
        print_ast(mod_ast, file=args.output)
        return 
    elif args.output_type == 'python':
        print(source.decode(), file=args.output)
    elif args.output_type == 'pyc':
        
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer

        try:
            timestamp = int(os.fstat(args.input.fileno()).st_mtime)
        except AttributeError:
            timestamp = int(os.stat(args.input.name).st_mtime)
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer
        create_pyc(source, cfile=args.output, timestamp=timestamp)
    else:
        raise  Exception("unknow output type %r" % args.output_type)

    return
Exemple #4
0
def depyc(args):
    
    binary = args.input.read()
    modtime, code = extract(binary)
    
    print("Decompiling module %r compiled on %s" % (args.input.name, modtime,), file=sys.stderr)
    
    if args.output_type == 'pyc':
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer
        args.output.write(binary)
        return
            
    if args.output_type == 'opcode':
        print_code(code)
        return 
    
    mod_ast = make_module(code)
    
    if args.output_type == 'ast':
        json.dump(serialize(mod_ast), args.output, indent=2)
        return
    
    if args.output_type == 'python':
        python_source(mod_ast, file=args.output)
        return
        
    
    raise  Exception("unknow output type %r" % args.output_type)
Exemple #5
0
def depyc(args):
    
    binary = args.input.read()
    modtime, code = extract(binary)
    
    print("Decompiling module %r compiled on %s" % (args.input.name, modtime,), file=sys.stderr)
    
    if args.output_type == 'pyc':
        if py3 and args.output is sys.stdout:
            args.output = sys.stdout.buffer
        args.output.write(binary)
        return
            
    if args.output_type == 'opcode':
        print_code(code)
        return 
    
    mod_ast = make_module(code)
    
    if args.output_type == 'ast':
        print_ast(mod_ast, file=args.output)
        return 
    
    if args.output_type == 'python':
        python_source(mod_ast, file=args.output)
        return
        
    
    raise  Exception("unknow output type %r" % args.output_type)