예제 #1
0
    def __init__(self, app):
        self.app = app
        self.current_max_id = 0
        self.heapy = hpy()
        self.modules = sys.modules


        for path, subdirs, files in os.walk(os.getcwd()):
            for filename in files:
                if filename.endswith(".py") and "env" not in path:
                    full_filename = os.path.join(path, filename)
                    with open(full_filename) as f:
                        code_one = f.read()

                    ast_tree = ast.parse(code_one)
                    ast_tree = StringTransformer().visit(ast_tree)
                    ast.fix_missing_locations(ast_tree)
                    co = compile(ast_tree, "<ast>", "exec")

                    with open(full_filename + 'c', 'wb') as fc:
                        fc.write('\0\0\0\0')
                        py_compile.wr_long(fc, long(time.time()))
                        marshal.dump(co, fc)
                        fc.flush()
                        fc.seek(0, 0)
                        fc.write(py_compile.MAGIC)
예제 #2
0
파일: bytecode.py 프로젝트: dagss/Bento
def bcompile(source):
    """Return the compiled bytecode from the given filename as a string ."""
    f = open(source, 'U')
    try:
        try:
            timestamp = long(os.fstat(f.fileno()).st_mtime)
        except AttributeError:
            timestamp = long(os.stat(file).st_mtime)
        codestring = f.read()
        f.close()
        if codestring and codestring[-1] != '\n':
            codestring = codestring + '\n'
        try:
            codeobject = __builtin__.compile(codestring, source, 'exec')
        except Exception,err:
            raise PyCompileError(err.__class__, err.args, source)
        fc = StringIO()
        try:
            fc.write('\0\0\0\0')
            wr_long(fc, timestamp)
            fc.write(marshal.dumps(codeobject))
            fc.flush()
            fc.seek(0, 0)
            fc.write(MAGIC)
            return fc.getvalue()
        finally:
            fc.close()
예제 #3
0
파일: importer.py 프로젝트: RobotDisco/hy
def write_hy_as_pyc(fname):
    with open(fname, 'U') as f:
        try:
            st = os.fstat(f.fileno())
        except AttributeError:
            st = os.stat(fname)
        timestamp = long_type(st.st_mtime)

    _ast = import_file_to_ast(fname,
                              os.path.basename(os.path.splitext(fname)[0]))
    code = ast_compile(_ast, fname, "exec")
    cfile = "%s.pyc" % fname[:-len(".hy")]

    if sys.version_info[0] >= 3:
        open_ = open
    else:
        open_ = __builtin__.open

    with open_(cfile, 'wb') as fc:
        if sys.version_info[0] >= 3:
            fc.write(b'\0\0\0\0')
        else:
            fc.write('\0\0\0\0')
        wr_long(fc, timestamp)
        if (sys.version_info[0] >= 3 and sys.version_info[1] >= 3):
            wr_long(fc, st.st_size)
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(MAGIC)
예제 #4
0
def bcompile(source):
    """Return the compiled bytecode from the given filename as a string ."""
    f = open(source, 'U')
    try:
        try:
            timestamp = long(os.fstat(f.fileno()).st_mtime)
        except AttributeError:
            timestamp = long(os.stat(file).st_mtime)
        codestring = f.read()
        f.close()
        if codestring and codestring[-1] != '\n':
            codestring = codestring + '\n'
        try:
            codeobject = __builtin__.compile(codestring, source, 'exec')
        except Exception, err:
            raise PyCompileError(err.__class__, err.args, source)
        fc = StringIO()
        try:
            fc.write('\0\0\0\0')
            wr_long(fc, timestamp)
            fc.write(marshal.dumps(codeobject))
            fc.flush()
            fc.seek(0, 0)
            fc.write(MAGIC)
            return fc.getvalue()
        finally:
            fc.close()
예제 #5
0
파일: ddl.py 프로젝트: zzl200012/railgun
def p_file_input(p):
    '''file_input : ENDMARKER
			| file_content ENDMARKER'''

    if len(p) == 3:
        item = p[1]
        while isinstance(item, list):
            item = item[0]

        p[0] = ast.Module(p[1], lineno=item.lineno, col_offset=item.col_offset)
        print ast.dump(p[0])
    codeobject = compile(p[0], '<string>', 'exec')
    print exec_file
    with open(exec_file + 'b', 'wb') as fc:
        print exec_file
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(codeobject, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)

    if not _debug:
        co = compile(p[0], exec_file, 'exec')
        #exec co

    return
예제 #6
0
    def generate_pyc(cls, py_path):
        pyc_name = None

        try:
            # 将源码编译成pycode对象
            with open(py_path) as source_fp:
                source = source_fp.read()
                code = compile(source, py_path, 'exec')

            # 将pycode对象保存为pyc格式
            pyc_name = os.path.splitext(py_path)[0] + '.pyc'
            with open(pyc_name, "wb") as code_fp:
                # 按照ceval.c 写入方式
                code_fp.write(imp.get_magic())
                py_compile.wr_long(code_fp, 0)
                marshal.dump(code, code_fp)
                code_fp.flush()
                code_fp.seek(4, 0)
                py_compile.wr_long(code_fp, int(time.time()))

        except IOError as error:
            showerror("生成PYC失败", "文件处理失败!{}".format(error))
        except Exception as error:
            showerror("生成PYC失败", "{}".format(error))

        return pyc_name
예제 #7
0
def compile_py(file: PyFile):
    with tokenize.open(file.file_path) as f:
        try:
            timestamp = int(os.fstat(f.fileno()).st_mtime)
        except AttributeError:
            timestamp = int(os.stat(file.file_path).st_mtime)
        codestring = f.read()
    try:
        codeobject = builtins.compile(codestring,
                                      file.base,
                                      'exec',
                                      dont_inherit=True)
    except Exception as err:
        py_exc = PyCompileError(err.__class__, err, file)
        sys.stderr.write(py_exc.msg + '\n')
        return
    try:
        dirname = os.path.dirname(file.pyc_path)
        if dirname:
            os.makedirs(dirname)
    except OSError as error:
        if error.errno != errno.EEXIST:
            raise
    with open(file.pyc_path, 'wb') as fc:
        fc.write(b'\0\0\0\0')
        py_compile.wr_long(fc, timestamp)
        marshal.dump(codeobject, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(MAGIC)
    return file.pyc_path
예제 #8
0
def _byte_compile(name, path, timestamp):
    """
    Byte compiles a Python file and returns the bytes.

    This is copied from py_compile and is risky, but py_compile
    doesn't support a version that writes to a file object.
    """
    text = open(path, 'U').read()
    try:
        if ispython3:
            code = compile(text, path, 'exec', optimize=1)
        else:
            if sys.flags.optimize != 1:
                raise DistutilsError('Optimize is set to {}.  Run python with -O flag to set optimize to 1.'.format(sys.flags.optimize))

            code = compile(text, path, 'exec')

    except Exception as details:
        raise DistutilsError("compiling '%s' failed\n    %s: %s" % (path, details.__class__.__name__, details))

    b = compat.BytesIO()
    b.write(py_compile.MAGIC)
    py_compile.wr_long(b, timestamp)
    b.write(marshal.dumps(code))
    return b.getvalue()
예제 #9
0
 def _bcompile(file, cfile=None, dfile=None, doraise=False):
     encoding = py_compile.read_encoding(file, "utf-8")
     f = open(file, 'U', encoding=encoding)
     try:
         timestamp = int(os.fstat(f.fileno()).st_mtime)
     except AttributeError:
         timestamp = int(os.stat(file).st_mtime)
     codestring = f.read()
     f.close()
     if codestring and codestring[-1] != '\n':
         codestring = codestring + '\n'
     try:
         codeobject = builtins.compile(codestring, dfile or file, 'exec')
     except Exception as err:
         py_exc = py_compile.PyCompileError(err.__class__, err, dfile
                                            or file)
         if doraise:
             raise py_exc
         else:
             sys.stderr.write(py_exc.msg + '\n')
             return
     fc = io.BytesIO()
     try:
         fc.write(b'\0\0\0\0')
         py_compile.wr_long(fc, timestamp)
         marshal.dump(codeobject, fc)
         fc.flush()
         fc.seek(0, 0)
         fc.write(py_compile.MAGIC)
         return fc.getvalue()
     finally:
         fc.close()
예제 #10
0
def write_hy_as_pyc(fname):
    with open(fname, 'U') as f:
        try:
            st = os.fstat(f.fileno())
        except AttributeError:
            st = os.stat(fname)
        timestamp = long_type(st.st_mtime)

    _ast = import_file_to_ast(fname,
                              os.path.basename(os.path.splitext(fname)[0]))
    code = ast_compile(_ast, fname, "exec")
    cfile = "%s.pyc" % fname[:-len(".hy")]

    if sys.version_info[0] >= 3:
        open_ = open
    else:
        open_ = __builtin__.open

    with open_(cfile, 'wb') as fc:
        if sys.version_info[0] >= 3:
            fc.write(b'\0\0\0\0')
        else:
            fc.write('\0\0\0\0')
        wr_long(fc, timestamp)
        if (sys.version_info[0] >= 3 and sys.version_info[1] >= 3):
            wr_long(fc, st.st_size)
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(MAGIC)
예제 #11
0
def mydecrypt(code,decrypted_name,decompyled_name):
	with open(decrypted_name, 'wb') as fc:
		fc.write(py_compile.MAGIC)
		py_compile.wr_long(fc, long(time.time()))
		marshal.dump(code, fc)
		fc.flush()
		fc.seek(0, 0)
		mydecomp(decrypted_name,decompyled_name)
예제 #12
0
파일: env.py 프로젝트: Algy/tempy
def _write_code(filename, codeobject):
    with open(filename, "wb") as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(codeobject, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
예제 #13
0
def create_pyc_file(func, code):
    with open(PYC_FILENAME.format(func), "wb") as f:
        f.write("\0\0\0\0")
        py_compile.wr_long(f, long(time.time()))
        marshal.dump(code, f)
        f.flush()
        f.seek(0, 0)
        f.write(py_compile.MAGIC)
예제 #14
0
def mydecrypt(code, decrypted_name, decompyled_name):
    with open(decrypted_name, 'wb') as fc:
        fc.write(py_compile.MAGIC)
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        mydecomp(decrypted_name, decompyled_name)
예제 #15
0
파일: env.py 프로젝트: Algy/tempy
def _write_code(filename, codeobject):
    with open(filename, "wb") as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(codeobject, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
예제 #16
0
def writeCodeToPyc(codeobj, outputFilename):
	with open(outputFilename, 'wb') as fc:
		fc.write('\0\0\0\0')
		py_compile.wr_long(fc, long(time.time()))
		marshal.dump(codeobj, fc)
		fc.flush()
		fc.seek(0, 0)
		fc.write(py_compile.MAGIC)
예제 #17
0
 def export_transformed(self, code, tree, module_name, file_name):
     f = open(file_name + suffix , 'wb')
     f.write('\0\0\0\0')
     timestamp = long(os.fstat(f.fileno()).st_mtime)
     wr_long(f, timestamp)
     marshal.dump(code, f)
     f.flush()
     f.seek(0, 0)
     f.write(imp.get_magic())
예제 #18
0
def PyCodeObject_to_pyc(py_code_obj, pyc_file):
    with open(pyc_file, 'wb') as pyc:
        pyc_magic = imp.get_magic()
        pyc.write(pyc_magic)
        mtime = long(os.fstat(pyc.fileno()).st_mtime)
        py_compile.wr_long(pyc, mtime)
        marshal.dump(py_code_obj, pyc)
        pyc.flush()
        pyc.close()
예제 #19
0
def compile_to_file(filename, compiled_code):
    delete_file_if_exists(filename)
    with open(filename, 'wb') as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(compiled_code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
예제 #20
0
 def export_transformed(self, code, tree, module_name, file_name):
     f = open(file_name + suffix, 'wb')
     f.write('\0\0\0\0')
     timestamp = long(os.fstat(f.fileno()).st_mtime)
     wr_long(f, timestamp)
     marshal.dump(code, f)
     f.flush()
     f.seek(0, 0)
     f.write(imp.get_magic())
예제 #21
0
def PyCodeObject_to_pyc(py_code_obj, pyc_file):
    with open(pyc_file, 'wb') as pyc:
        pyc_magic = imp.get_magic()
        pyc.write(pyc_magic)
        mtime = long(os.fstat(pyc.fileno()).st_mtime)
        py_compile.wr_long(pyc, mtime)
        marshal.dump(py_code_obj, pyc)
        pyc.flush()
        pyc.close()
예제 #22
0
    def executeModule(self, code):
        code.append((RETURN_VALUE, None))
        c = Code(code, [], [], False, False, False, str(symbol(self.getNS().__name__, "<string>")), self.filename, 0, None)

        dis.dis(c)
        codeobject = c.to_code()

        with open('output.pyc', 'wb') as fc:
            fc.write(py_compile.MAGIC)
            py_compile.wr_long(fc, long(time.time()))
            marshal.dump(c, fc)
예제 #23
0
파일: utils.py 프로젝트: deefex/mutate
def compile_to_file(filename, compiled_code):
    """ Compile the file_under_test.py to its .pyc form - ensuring consistency before and between mutations """
    # TODO - Investigate whether
    delete_file_if_exists(filename)
    with open(filename, 'wb') as fc:
            fc.write('\0\0\0\0')
            py_compile.wr_long(fc, long(time.time()))
            marshal.dump(compiled_code, fc)
            fc.flush()
            fc.seek(0, 0)
            fc.write(py_compile.MAGIC)
예제 #24
0
파일: lib.py 프로젝트: vangu02/pyintercept
    def save(self, outfile):
        """ Save compiled patched file in outfile (.pyc)
        """
        assert self.new_code_object, 'Not patched'

        with open(outfile, 'wb') as fd:
            fd.write(py_compile.MAGIC)
            py_compile.wr_long(fd, long(time.time()))
            marshal.dump(self.new_code_object, fd)
            fd.flush()
            fd.seek(0, 0)
            fd.write(py_compile.MAGIC)
예제 #25
0
    def save(self, outfile):
        """ Save compiled patched file in outfile (.pyc)
        """
        assert self.new_code_object, 'Not patched'

        with open(outfile, 'wb') as fd:
            fd.write(py_compile.MAGIC)
            py_compile.wr_long(fd, long(time.time()))
            marshal.dump(self.new_code_object, fd)
            fd.flush()
            fd.seek(0, 0)
            fd.write(py_compile.MAGIC)
예제 #26
0
def test_bytecode():
    """This function does the same as test_codegen, but it generates a
    .pyc, avoiding the use of codegen.

    """
    code = compile(ast.fix_missing_locations(rewrite_tree()), '<string>', 'exec')
    with open('output.pyc', 'wb') as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
예제 #27
0
파일: ast_export.py 프로젝트: pannous/kast
def emit_pyc(code,fileName='output.pyc'):
    import marshal
    import py_compile
    import time
    with open(fileName, 'wb') as fc:
        fc.write(b'\0\0\0\0')
        # py_compile.wr_long(fc, long(time.time()))
        py_compile.wr_long(fc, time.time())
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
        print("WRITTEN TO "+fileName)
예제 #28
0
파일: ast_export.py 프로젝트: cppxaxa/kast
def emit_pyc(code, fileName='output.pyc'):
    import marshal
    import py_compile
    import time
    with open(fileName, 'wb') as fc:
        fc.write(b'\0\0\0\0')
        # py_compile.wr_long(fc, long(time.time()))
        py_compile.wr_long(fc, time.time())
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
        print("WRITTEN TO " + fileName)
예제 #29
0
    def executeModule(self, code):
        code.append((RETURN_VALUE, None))
        c = Code(code, [], [], False, False, False,
                 str(Symbol(self.getNS().__name__, "<string>")), self.filename,
                 0, None)

        dis.dis(c)
        codeobject = c.to_code()

        with open('output.pyc', 'wb') as fc:
            fc.write(py_compile.MAGIC)
            py_compile.wr_long(fc, long(time.time()))
            marshal.dump(c, fc)
예제 #30
0
def test_bytecode():
    """This function does the same as test_codegen, but it generates a
    .pyc, avoiding the use of codegen.

    """
    code = compile(ast.fix_missing_locations(rewrite_tree()), '<string>',
                   'exec')
    with open('output.pyc', 'wb') as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, long(time.time()))
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
예제 #31
0
    def executeModule(self, code):
        code.append((RETURN_VALUE, None))
        c = Code(code, [], [], False, False, False, str(Symbol.intern(self.getNS().__name__, "<string>")), "./clj/clojure/core.clj", 0, None)
        import marshal
        import pickle
        import py_compile
        import time
        import dis

        dis.dis(c)
        codeobject = c.to_code()
        print codeobject.__class__ is compileDef.__class__

        with open('output.pyc', 'wb') as fc:
            fc.write(py_compile.MAGIC)
            py_compile.wr_long(fc, long(time.time()))
            marshal.dump(c, fc)
예제 #32
0
파일: rt.py 프로젝트: pombredanne/psil
def _import(fn, globals):
    try:
        return __import__(fn, globals=globals)
    except ImportError:
        psilname = fn + ".psil"
        pycname = fn + ".pyc"
        try:
            psilstat = os.stat(psilname)
        except OSError:
            raise ImportError("no such module")
        timestamp = int(psilstat.st_mtime)
        try:
            pycstat = os.stat(pycname)
        except OSError:
            pycstat = None
        if pycstat is None or pycstat.st_mtime < timestamp:
            f = open(psilname)
            code = f.read()
            f.close()
            body = []
            t = interpreter.tokenise(code)
            while True:
                p = interpreter.parse(t)
                if p is None:
                    break
                p = interpreter.macroexpand_r(p)
                if p is None:
                    continue
                tree = psilc(p)
                body.append(tree)
            tree = ast.Module(body)
            ast.fix_missing_locations(tree)
            codeobject = compile(tree, psilname, "exec")

            fc = open(pycname, "wb")
            fc.write(b"\0\0\0\0")
            py_compile.wr_long(fc, timestamp)
            marshal.dump(codeobject, fc)
            fc.flush()
            fc.seek(0, 0)
            fc.write(py_compile.MAGIC)
            fc.close()
            py_compile.set_creator_type(pycname)

            return builtins.__import__(fn, globals=globals)
예제 #33
0
파일: rt.py 프로젝트: shigehiro-fukase/psil
def _import(fn, globals):
    try:
        return __import__(fn, globals=globals)
    except ImportError:
        psilname = fn + ".psil"
        pycname = fn + ".pyc"
        try:
            psilstat = os.stat(psilname)
        except OSError:
            raise ImportError("no such module")
        timestamp = int(psilstat.st_mtime)
        try:
            pycstat = os.stat(pycname)
        except OSError:
            pycstat = None
        if pycstat is None or pycstat.st_mtime < timestamp:
            f = open(psilname)
            code = f.read()
            f.close()
            body = []
            t = interpreter.tokenise(code)
            while True:
                p = interpreter.parse(t)
                if p is None:
                    break
                p = interpreter.macroexpand_r(p)
                if p is None:
                    continue
                tree = psilc(p)
                body.append(tree)
            tree = ast.Module(body)
            ast.fix_missing_locations(tree)
            codeobject = compile(tree, psilname, 'exec')

            fc = open(pycname, 'wb')
            fc.write(b'\0\0\0\0')
            py_compile.wr_long(fc, timestamp)
            marshal.dump(codeobject, fc)
            fc.flush()
            fc.seek(0, 0)
            fc.write(py_compile.MAGIC)
            fc.close()
            py_compile.set_creator_type(pycname)

            return builtins.__import__(fn, globals=globals)
예제 #34
0
파일: compile.py 프로젝트: rougeth/pycc
def make_pyc(module, destination):

    ast.fix_missing_locations(module.node)
    codeobject = compile(module.node, module.location, 'exec')

    destination_dir = os.path.split(destination)[0]

    if not os.path.exists(destination_dir):

        os.makedirs(destination_dir)

    with open(destination, 'wb') as pyc:
        pyc.write('\0\0\0\0')
        py_compile.wr_long(pyc, long(time.time()))
        marshal.dump(codeobject, pyc)
        pyc.flush()
        pyc.seek(0, 0)
        pyc.write(py_compile.MAGIC)
예제 #35
0
파일: importer.py 프로젝트: cwebber/hy
def write_hy_as_pyc(fname):
    with open(fname, 'U') as f:
        try:
            timestamp = long(os.fstat(f.fileno()).st_mtime)
        except AttributeError:
            timestamp = long(os.stat(fname).st_mtime)

    _ast = import_file_to_ast(fname)
    code = compile(_ast, fname, "exec")
    cfile = "%s.pyc" % fname[:-len(".hy")]

    with open(cfile, 'wb') as fc:
        fc.write('\0\0\0\0')
        wr_long(fc, timestamp)
        marshal.dump(code, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(MAGIC)
예제 #36
0
def _code_to_bytecode_py3(code):
    """Get bytecode for PY3.

    This implementation borrows heavily from the standard lib implementation
    in the py_compile.compile function.
    """
    # Importing in this function because it only contains useful data when
    # loaded in a PY# environment.
    import py_compile
    import marshal

    bytecode = io.BytesIO()
    bytecode.write(str('\0\0\0\0'))
    py_compile.wr_long(bytecode, pycompat.long(time.time()))
    py_compile.wr_long(bytecode, pycompat.long(0))
    bytecode.write(marshal.dumps(code))
    bytecode.seek(0, 0)
    bytecode.write(py_compile.MAGIC)
    return bytecode.getvalue()
예제 #37
0
def _code_to_bytecode_py3(code):
    """Get bytecode for PY3.

    This implementation borrows heavily from the standard lib implementation
    in the py_compile.compile function.
    """
    # Importing in this function because it only contains useful data when
    # loaded in a PY# environment.
    import py_compile
    import marshal

    bytecode = io.BytesIO()
    bytecode.write(str('\0\0\0\0'))
    py_compile.wr_long(bytecode, pycompat.long(time.time()))
    py_compile.wr_long(bytecode, pycompat.long(0))
    bytecode.write(marshal.dumps(code))
    bytecode.seek(0, 0)
    bytecode.write(py_compile.MAGIC)
    return bytecode.getvalue()
예제 #38
0
 def _bcompile(file, cfile=None, dfile=None, doraise=False, optimize=-1):
     with tokenize.open(file) as f:
         try:
             timestamp = int(os.fstat(f.fileno()).st_mtime)
         except AttributeError:
             timestamp = int(os.stat(file).st_mtime)
         codestring = f.read()
     try:
         codeobject = builtins.compile(codestring,
                                       dfile or file,
                                       'exec',
                                       optimize=optimize)
     except Exception as err:
         py_exc = py_compile.PyCompileError(err.__class__, err, dfile
                                            or file)
         if doraise:
             raise py_exc
         else:
             sys.stderr.write(py_exc.msg + '\n')
             return
     if cfile is None:
         if optimize >= 0:
             cfile = imp.cache_from_source(file,
                                           debug_override=not optimize)
         else:
             cfile = imp.cache_from_source(file)
     try:
         os.makedirs(os.path.dirname(cfile))
     except OSError as error:
         if error.errno != errno.EEXIST:
             raise
     fc = io.BytesIO()
     try:
         fc.write(b'\0\0\0\0')
         py_compile.wr_long(fc, timestamp)
         marshal.dump(codeobject, fc)
         fc.flush()
         fc.seek(0, 0)
         fc.write(py_compile.MAGIC)
         return fc.getvalue()
     finally:
         fc.close()
예제 #39
0
def _code_to_bytecode_py2(code):
    """Get bytecode for PY2.

    This implementation borrows heavily from the standard lib implementation
    in the py_compile.compile function.
    """
    # Importing in this function because it only contains useful data when
    # loaded in a PY2 environment.
    import py_compile
    import marshal

    bytecode = io.BytesIO()
    # NOTE: Intentionally not using the BytesIO initializer to write the inital
    # bytes. Initializing the objects with '\0\0\0\0' resulted in no bytes
    # being written to the buffer. Instead, use the write method explicitly to
    # write the placeholder for the magic number.
    bytecode.write(str('\0\0\0\0'))
    py_compile.wr_long(bytecode, pycompat.long(time.time()))
    bytecode.write(marshal.dumps(code))
    bytecode.seek(0, 0)
    bytecode.write(py_compile.MAGIC)
    return bytecode.getvalue()
예제 #40
0
def _code_to_bytecode_py2(code):
    """Get bytecode for PY2.

    This implementation borrows heavily from the standard lib implementation
    in the py_compile.compile function.
    """
    # Importing in this function because it only contains useful data when
    # loaded in a PY2 environment.
    import py_compile
    import marshal

    bytecode = io.BytesIO()
    # NOTE: Intentionally not using the BytesIO initializer to write the inital
    # bytes. Initializing the objects with '\0\0\0\0' resulted in no bytes
    # being written to the buffer. Instead, use the write method explicitly to
    # write the placeholder for the magic number.
    bytecode.write(str('\0\0\0\0'))
    py_compile.wr_long(bytecode, pycompat.long(time.time()))
    bytecode.write(marshal.dumps(code))
    bytecode.seek(0, 0)
    bytecode.write(py_compile.MAGIC)
    return bytecode.getvalue()
예제 #41
0
파일: compiler.py 프로젝트: Mause/tyrian
    def write_code_to_file(self,
                           codeobject: types.CodeType,
                           filehandler=None,
                           filename: str=None):
        """
        Write a code object to the specified filehandler
        """

        st = os.stat(filename or __file__)
        size = st.st_size & 0xFFFFFFFF
        timestamp = int(st.st_mtime)

        # write a placeholder for the MAGIC
        filehandler.write(b'\0\0\0\0')

        wr_long(filehandler, timestamp)
        wr_long(filehandler, size)
        marshal.dump(codeobject, filehandler)
        filehandler.flush()

        # write the magic to the start
        filehandler.seek(0, 0)
        filehandler.write(MAGIC)
예제 #42
0
            codestring, {'%(file_compile_date)s': time.strftime('%d.%m.%Y', time.localtime(maxTS)),
                         '%(mod_ID)s': multireplace(os.path.basename(modName), {'.py': '', 'mod_': ''})})
    try:
        codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
    except Exception, err:
        py_exc = py_compile.PyCompileError(err.__class__, err, dfile or file)
        if doraise:
            raise py_exc
        else:
            sys.stderr.write(py_exc.msg + '\n')
            return
    if cfile is None:
        cfile = file + (__debug__ and 'c' or 'o')
    with open(cfile, 'wb') as fc:
        fc.write('\0\0\0\0')
        py_compile.wr_long(fc, timestamp)
        marshal.dump(codeobject, fc)
        fc.flush()
        fc.seek(0, 0)
        fc.write(py_compile.MAGIC)
    if timeStr:
        os.utime(cfile, (time.time(), timestamp))


def multireplace(string, replacements):
    """
    Given a string and a replacement map, it returns the replaced string.
    :param str string: string to execute replacements on
    :param dict replacements: replacement dictionary {value to find: value to replace}
    :rtype: str
    """
예제 #43
0
import marshal
import py_compile
import time
import ast

#codeobject = compile("body=[ClassDef(name='A', bases=[Name(id='object', ctx=Load())], body=[FunctionDef(name='__init__', args=arguments(args=[Name(id='self', ctx=Param()), Name(id='value', ctx=Param())], vararg=None, kwarg=None, defaults=[]), body=[Assign(targets=[Attribute(value=Name(id='self', ctx=Load()), attr='v', ctx=Store())], value=Name(id='value', ctx=Load()))], decorator_list=[]), FunctionDef(name='p', args=arguments(args=[Name(id='self', ctx=Param())], vararg=None, kwarg=None, defaults=[]), body=[Print(dest=None, values=[Attribute(value=Name(id='self', ctx=Load()), attr='v', ctx=Load())], nl=True), Return()], decorator_list=[])], decorator_list=[]), Assign(targets=[Name(id='a', ctx=Store())], value=Call(func=Name(id='A', ctx=Load()), args=[Num(n=1)], keywords=[], starargs=None, kwargs=None)), Expr(value=Call(func=Attribute(value=Name(id='a', ctx=Load()), attr='p', ctx=Load()), args=[], keywords=[], starargs=None, kwargs=None))]", '<string>', 'exec')
codeobject = compile(ast.parse('print 3+3*2'), '<string>', 'exec')
print codeobject
with open('output.pyc', 'wb') as fc:
    fc.write('\0\0\0\0')
    py_compile.wr_long(fc, long(time.time()))
    marshal.dump(codeobject, fc)
    fc.flush()
    fc.seek(0, 0)
    fc.write(py_compile.MAGIC)
예제 #44
0
파일: pysight.py 프로젝트: nomoral/pySight
def write_codeobj(out, code):
	with open(out, 'wb') as fc:
		fc.write(py_compile.MAGIC)
		py_compile.wr_long(fc, long(time.time()))
		marshal.dump(code, fc)
		fc.flush()
예제 #45
0
 def dump_code_to_file(code, file):
     file.write(MAGIC)
     wr_long(file, long(time.time()))  # noqa
     marshal.dump(code, file)
     file.flush()
예제 #46
0
import base64
import marshal
import py_compile
import time
import zlib

with open('needpy.pyc', 'wb') as fc:
    fc.write('\0\0\0\0')
    codeobject = marshal.loads(zlib.decompress(base64.b64decode(
        'eJxtVP9r21YQvyd/ieWm66Cd03QM1B8C3pggUuzYCSWstHSFQijyoJBhhGq9OXJl2ZFeqAMOK6Q/94f9Ofvn1s+d7Lgtk/3O997du/vc584a0eqpYP2GVfwDEeOrKCU6g2LRRyiK4oooFsVVUSqkqxTX6J1F+SfSNYrrdKPorC76luhbpOEGCZNFZw2KG3Rmk26QtuXi3xTb7ND6/aVu0g2RuvhEcZNut5lAGbTvAFbyH57TkYLKy8J6xpDvQxiiiaIlcdqJxVcHbXY6bXNlZgviPCrO0+StqfKd88gzNh/qRZyMdWHE29TZZvIkG7eZFRGGRcBmsXJaUoKCQ9fWKHwSqNeKFnsM5PnwJ7q2aKk4AFhcWtQCh+ChB5+Lu/RmyYUxmtOEYxas7i/2iuR7Ti14OEOSmU0RADd4+dQzbM1FJhukAUeQ+kZROuLyioagrau76kc1slY1NNaY/y3LAxDQBrAICJisV2hMdF2lxQcyFuMoqcX3+TCl6xotqzSpkqmxYVmjXVjAXiwBsEfBrd1VvTvLCj2EXRnhoryAKdpxcIgJcowUB68yAx/tlCAuPHqDuZo0CN3CUGHwkPhGMA7aXMfphjbmQLhLhJcHa0a+mpgB191c1U1lnHJQbgkHx+WGxeJbejnpkzSavo2jkxZ7i725npGAaTc8FXmUjbUETHUmkxXN5zqL5WiWxwE7Bc11yyYzNJpN02jerq+DzNNodfxOX8kE4FcmYKscDdYD1oPGGucXYNmgs1F+NTf3GOt3Mg7b+NTVruqoQyX1hOEUacKw+AGbP38ZOq9THRXaSbL5pXGQ8bho/Z/lrzQaHxdoCrlev+t6nZ7re57r+57rHXag93Deh37k+vuw9zorO/Qj/B50cAf2oyOsvut3D+ADWxdxfN/1Drqu39mHzvcRswv/Hvz7sHeg9w8Qzy99DzuFwxhPhs6zWTbOI3OZRiaZZcVj5wVwOklx7OwVxR47PR46r/SVM8ulBJic9zku/eqY/MqJxiDj+Gd55wS3f35pbLCzHoEwzKKpDkN5i+TR+1AYCWTo5IV0Z0P9H3phDDd6lMzPdS5bbo9eJGbTsW9nbDqLL1N9Iq+rRxDbll2x67a9Lf27hw5uK1s1rZr6DOPF+FI=')))
    py_compile.wr_long(fc, long(time.time()))
    marshal.dump(codeobject, fc)
    fc.flush()
    fc.seek(0, 0)
    fc.write(py_compile.MAGIC)