Exemple #1
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not. """

        if self.fileType == FTPythonCompiled:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            return None

        elif self.fileType == FTCompiledModule:
            return None

        # It's a .py file (or an __init__.py file; same thing).  Read
        # the .pyc file if it is available and current; otherwise read
        # the .py file and compile it.
        pycFilename = Filename(self.filename)
        pycFilename.setExtension(pycExtension)
        pycVfile = vfs.getFile(pycFilename, False)
        t_pyc = None
        if pycVfile:
            t_pyc = pycVfile.getTimestamp()

        code = None
        if t_pyc and t_pyc >= self.timestamp:
            code = self._loadPyc(pycVfile, self.timestamp)

        if not code:
            source = self._read_source()
            filename = Filename(self.filename)
            filename.setExtension('py')
            code = self._compile(filename, source)

        return code
Exemple #2
0
    def _compile(self, filename, source):
        """ Compiles the Python source code to a code object and
        attempts to write it to an appropriate .pyc file. """
        
        if source and source[-1] != '\n':
            source = source + '\n'
        code = __builtin__.compile(source, filename.cStr(), 'exec')

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(pycExtension)
        try:
            f = open(pycFilename, 'wb')
        except IOError:
            pass
        else:
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', self.timestamp))
            f.write(marshal.dumps(code))
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()

        return code
Exemple #3
0
    def _compile(self, filename, source):
        """ Compiles the Python source code to a code object and
        attempts to write it to an appropriate .pyc file. """

        if source and source[-1] != '\n':
            source = source + '\n'
        code = __builtin__.compile(source, filename.cStr(), 'exec')

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(pycExtension)
        try:
            f = open(pycFilename, 'wb')
        except IOError:
            pass
        else:
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', self.timestamp))
            f.write(marshal.dumps(code))
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()

        return code
Exemple #4
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not. """

        if self.fileType == FTPythonCompiled:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            return None

        elif self.fileType == FTCompiledModule:
            return None

        # It's a .py file (or an __init__.py file; same thing).  Read
        # the .pyc file if it is available and current; otherwise read
        # the .py file and compile it.
        pycFilename = Filename(self.filename)
        pycFilename.setExtension(pycExtension)
        pycVfile = vfs.getFile(pycFilename, False)
        t_pyc = None
        if pycVfile:
            t_pyc = pycVfile.getTimestamp()

        code = None
        if t_pyc and t_pyc >= self.timestamp:
            code = self._loadPyc(pycVfile, self.timestamp)

        if not code:
            source = self._read_source()
            filename = Filename(self.filename)
            filename.setExtension('py')
            code = self._compile(filename, source)

        return code
Exemple #5
0
 def _read_source(self):
     """ Returns the Python source for this file, if it is
     available, or None if it is not. """
     
     if self.fileType == FTPythonCompiled or \
        self.fileType == FTCompiledModule:
         return None
     
     filename = Filename(self.filename)
     filename.setExtension('py')
     try:
         file = open(filename, 'rU')
     except IOError:
         return None
     return file.read()
Exemple #6
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not. """

        if self.fileType == FTPythonCompiled or \
           self.fileType == FTCompiledModule:
            return None

        filename = Filename(self.filename)
        filename.setExtension('py')
        try:
            file = open(filename, 'rU')
        except IOError:
            return None
        return file.read()
Exemple #7
0
    def find_module(self, fullname):
        basename = fullname.split('.')[-1]
        path = Filename(self.dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource)

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        filename = Filename(path)
        filename.setExtension(pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled)

        # Look for a compiled C/C++ module.
        for desc in imp.get_suffixes():
            if desc[2] != imp.C_EXTENSION:
                continue

            filename = Filename(path)
            filename.setExtension(desc[0][1:])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(self,
                                 vfile,
                                 filename,
                                 FTCompiledModule,
                                 desc=desc)

        # Finally, consider a package, i.e. a directory containing
        # __init__.py.
        filename = Filename(path, '__init__.py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self,
                             vfile,
                             filename,
                             FTPythonSource,
                             packagePath=path)
        filename = Filename(path, '__init__.' + pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self,
                             vfile,
                             filename,
                             FTPythonCompiled,
                             packagePath=path)

        return None
Exemple #8
0
    def find_module(self, fullname):
        basename = fullname.split('.')[-1]
        path = Filename(self.dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource)

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        filename = Filename(path)
        filename.setExtension(pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled)

        # Look for a compiled C/C++ module.
        for desc in imp.get_suffixes():
            if desc[2] != imp.C_EXTENSION:
                continue
            
            filename = Filename(path)
            filename.setExtension(desc[0][1:])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(self, vfile, filename, FTCompiledModule,
                                 desc = desc)
        

        # Finally, consider a package, i.e. a directory containing
        # __init__.py.
        filename = Filename(path, '__init__.py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource,
                             packagePath = path)
        filename = Filename(path, '__init__.' + pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled,
                             packagePath = path)

        return None