Esempio n. 1
0
def _fs_import(dir, modname, fqname):
    "Fetch a module from the filesystem."

    pathname = os.path.join(dir, modname)
    if os.path.isdir(pathname):
        values = { '__pkgdir__' : pathname, '__path__' : [ pathname ] }
        ispkg = 1
        pathname = os.path.join(pathname, '__init__')
    else:
        values = { }
        ispkg = 0

        # look for dynload modules
        for desc in _c_suffixes:
            file = pathname + desc[0]
            try:
                fp = open(file, desc[1])
            except IOError:
                pass
            else:
                module = imp.load_module(fqname, fp, file, desc)
                values['__file__'] = file
                return 0, module, values

    t_py = _timestamp(pathname + '.py')
    t_pyc = _timestamp(pathname + _suffix)
    if t_py is None and t_pyc is None:
        return None
    code = None
    # XXX TODO - read .pyc from platform-specific locations...
    if False: # t_py is None or (t_pyc is not None and t_pyc >= t_py):
        file = pathname + _suffix
        f = open(file, 'rb')
        if f.read(4) == imp.get_magic():
            t = struct.unpack('<I', f.read(4))[0]
            if t == t_py:
                code = marshal.load(f)
        f.close()
    if code is None:
        file = pathname + '.py'
        mod, file = parser.parseModule(modname, file)
        code = Module(mod, file)
        code.compile()
        code = code.getCode()
        
        #code = imputil._compile(file, t_py)

    values['__file__'] = file
    return ispkg, code, values
Esempio n. 2
0
def _fs_import(dir, modname, fqname):
    "Fetch a module from the filesystem."

    pathname = os.path.join(dir, modname)
    if os.path.isdir(pathname):
        values = { '__pkgdir__' : pathname, '__path__' : [ pathname ] }
        ispkg = 1
        pathname = os.path.join(pathname, '__init__')
    else:
        values = { }
        ispkg = 0

        # look for dynload modules
        for desc in _c_suffixes:
            fname = pathname + desc[0]
            try:
                fp = open(fname, desc[1])
            except IOError:
                pass
            else:
                module = imp.load_module(fqname, fp, fname, desc)
                values['__file__'] = fname
                return 0, module, values

    filename = pathname + '.py'
    filenamec = pathname + _suffix
    t_py = _timestamp(filename)
    t_pyc = _timestamp(filenamec)
    #print "timestamp", filename, t_py, t_pyc
    if t_py is None and t_pyc is None:
        return None
    code = None
    out_t_py = t_py
    out_filename = filename
    # XXX TODO - read .pyc from platform-specific locations...
    platform_file = parser.checkOverridePlatformFile(filename)
    #print "check platform file", filename, platform_file
    if platform_file:
        platform_filec = platform_file[:-3] + _suffix
        t_p_py = _timestamp(platform_file)
        t_p_pyc = _timestamp(platform_filec)
        #print "platform file", platform_file, platform_filec, t_p_py, t_p_pyc
        if t_p_py is not None or t_p_pyc is not None:
            # platform file exists: must check that instead.
            ok = True
            if t_py is not None and t_pyc is not None and t_py > t_pyc:
                # .py exists, .pyc exists, .py is newer than pyc: nope
                ok = False
            if t_py is not None and t_p_py is not None and t_py > t_p_py:
                # .py exists, platform.py exists, .py is newer than pyc: nope
                ok = False
            if t_py is not None and t_p_pyc is not None and t_py > t_p_pyc:
                # .py exists, platform.pyc exists, .py is newer than pyc: nope
                ok = False

            #print "platform file ok", platform_file, platform_filec
            if ok and \
                (t_p_py is None or (t_p_pyc is not None and t_p_pyc >= t_p_py)):
                f = open(platform_filec, 'rb')
                magic = f.read(4)
                #print "reading platform file", platform_filec, repr(magic), repr(imp.get_magic())
                if magic == imp.get_magic():
                    t = struct.unpack('<I', f.read(4))[0]
                    #print "time?", platform_filec, t, t_p_py
                    if t == t_p_py:
                        code = marshal.load(f)
                f.close()

    if code is None and \
       (t_py is None or (t_pyc is not None and t_pyc >= t_py)):
        f = open(filenamec, 'rb')
        magic = f.read(4)
        if magic == imp.get_magic():
            t = struct.unpack('<I', f.read(4))[0]
            if t == t_py:
                code = marshal.load(f)
        f.close()

    if code is None:
        filename = pathname + '.py'
        "compiling", filename
        mod, filename = parser.parseModule(modname, filename)
        code = Module(mod, filename)
        code.compile()
        code = code.getCode()

        if platform_file and t_p_py:
            out_t_py = t_p_py
            out_filename = platform_file

        # try to cache the compiled code
        try:
            f = open(out_filename + _suffix_char, 'wb')
        except IOError, e:
            #print "write cache error", out_filename + _suffix_char, e
            pass
        else:
            #print "writing cache to ", out_filename + _suffix_char
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', out_t_py))
            marshal.dump(code, f)
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()
Esempio n. 3
0
def _fs_import(dir, modname, fqname):
    "Fetch a module from the filesystem."

    pathname = os.path.join(dir, modname)
    if os.path.isdir(pathname):
        values = { '__pkgdir__' : pathname, '__path__' : [ pathname ] }
        ispkg = 1
        pathname = os.path.join(pathname, '__init__')
    else:
        values = { }
        ispkg = 0

        # look for dynload modules
        for desc in _c_suffixes:
            fname = pathname + desc[0]
            try:
                fp = open(fname, desc[1])
            except IOError:
                pass
            else:
                module = imp.load_module(fqname, fp, fname, desc)
                values['__file__'] = fname
                return 0, module, values

    filename = pathname + '.py'
    filenamec = pathname + _suffix
    t_py = _timestamp(filename)
    t_pyc = _timestamp(filenamec)
    #print "timestamp", filename, t_py, t_pyc
    if t_py is None and t_pyc is None:
        return None
    code = None
    out_t_py = t_py
    out_filename = filename
    # XXX TODO - read .pyc from platform-specific locations...
    platform_file = parser.checkOverridePlatformFile(filename)
    #print "check platform file", filename, platform_file
    if platform_file:
        platform_filec = platform_file[:-3] + _suffix
        t_p_py = _timestamp(platform_file)
        t_p_pyc = _timestamp(platform_filec)
        #print "platform file", platform_file, platform_filec, t_p_py, t_p_pyc
        if t_p_py is not None or t_p_pyc is not None:
            # platform file exists: must check that instead.
            ok = True
            if t_py is not None and t_pyc is not None and t_py > t_pyc:
                # .py exists, .pyc exists, .py is newer than pyc: nope
                ok = False
            if t_py is not None and t_p_py is not None and t_py > t_p_py:
                # .py exists, platform.py exists, .py is newer than pyc: nope
                ok = False
            if t_py is not None and t_p_pyc is not None and t_py > t_p_pyc:
                # .py exists, platform.pyc exists, .py is newer than pyc: nope
                ok = False

            #print "platform file ok", platform_file, platform_filec
            if ok and \
                (t_p_py is None or (t_p_pyc is not None and t_p_pyc >= t_p_py)):
                f = open(platform_filec, 'rb')
                magic = f.read(4)
                #print "reading platform file", platform_filec, repr(magic), repr(imp.get_magic())
                if magic == imp.get_magic():
                    t = struct.unpack('<I', f.read(4))[0]
                    #print "time?", platform_filec, t, t_p_py
                    if t == t_p_py:
                        code = marshal.load(f)
                f.close()

    if code is None and \
       (t_py is None or (t_pyc is not None and t_pyc >= t_py)):
        f = open(filenamec, 'rb')
        magic = f.read(4)
        if magic == imp.get_magic():
            t = struct.unpack('<I', f.read(4))[0]
            if t == t_py:
                code = marshal.load(f)
        f.close()

    if code is None:
        filename = pathname + '.py'
        "compiling", filename
        mod, filename = parser.parseModule(modname, filename)
        code = Module(mod, filename)
        code.compile()
        code = code.getCode()
        
        if platform_file and t_p_py:
            out_t_py = t_p_py
            out_filename = platform_file

        # try to cache the compiled code
        try:
            f = open(out_filename + _suffix_char, 'wb')
        except IOError, e:
            #print "write cache error", out_filename + _suffix_char, e
            pass
        else:
            #print "writing cache to ", out_filename + _suffix_char
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', out_t_py))
            marshal.dump(code, f)
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()