Exemplo n.º 1
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.  May raise
        SyntaxError or other errors generated by the compiler. """

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

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(compiledExtensions[0])
        try:
            f = open(pycFilename.toOsSpecific(), "wb")
        except IOError:
            pass
        else:
            f.write("\0\0\0\0")
            f.write(
                chr(self.timestamp & 0xFF)
                + chr((self.timestamp >> 8) & 0xFF)
                + chr((self.timestamp >> 16) & 0xFF)
                + chr((self.timestamp >> 24) & 0xFF)
            )
            f.write(marshal.dumps(code))
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()

        return code
Exemplo n.º 2
0
    def _compile(self, filename, source):
        if source and source[-1] != '\n':
            source = source + '\n'
        code = __builtin__.compile(source, filename.toOsSpecific(), 'exec')
        pycFilename = Filename(filename)
        pycFilename.setExtension(compiledExtensions[0])
        try:
            f = open(pycFilename.toOsSpecific(), 'wb')
        except IOError:
            pass
        else:
            f.write(imp.get_magic())
            if sys.version_info >= (3, 0):
                f.write((self.timestamp & 4294967295L).to_bytes(4, 'little'))
                f.write('\x00\x00\x00\x00')
            else:
                f.write(
                    chr(self.timestamp & 255) +
                    chr(self.timestamp >> 8 & 255) +
                    chr(self.timestamp >> 16 & 255) +
                    chr(self.timestamp >> 24 & 255))
            f.write(marshal.dumps(code))
            f.close()

        return code
Exemplo n.º 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.  May raise
        SyntaxError or other errors generated by the compiler. """

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

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(compiledExtensions[0])
        try:
            f = open(pycFilename.toOsSpecific(), 'wb')
        except IOError:
            pass
        else:
            f.write(imp.get_magic())
            if sys.version_info >= (3, 0):
                f.write((self.timestamp & 0xffffffff).to_bytes(4, 'little'))
                f.write(b'\0\0\0\0')
            else:
                f.write(chr(self.timestamp & 0xff) +
                        chr((self.timestamp >> 8) & 0xff) +
                        chr((self.timestamp >> 16) & 0xff) +
                        chr((self.timestamp >> 24) & 0xff))
            f.write(marshal.dumps(code))
            f.close()

        return code
Exemplo n.º 4
0
    def getLoadedDirname(self, mod):
        """ Returns the directory name that the indicated
        conventionally-loaded module must have been loaded from. """

        if not getattr(mod, '__file__', None):
            return None

        fullname = mod.__name__
        dirname = Filename.fromOsSpecific(mod.__file__).getDirname()

        parentname = None
        basename = fullname
        if '.' in fullname:
            parentname, basename = fullname.rsplit('.', 1)

        path = None
        if parentname:
            parent = sys.modules[parentname]
            path = parent.__path__
        if path is None:
            path = sys.path

        for dir in path:
            pdir = str(Filename.fromOsSpecific(dir))
            if pdir + '/' + basename == dirname:
                # We found it!
                return dir

        # Couldn't figure it out.
        return None
Exemplo n.º 5
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.  May raise
        SyntaxError or other errors generated by the compiler. """

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

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(compiledExtensions[0])
        try:
            f = open(pycFilename.toOsSpecific(), 'wb')
        except IOError:
            pass
        else:
            f.write(imp.get_magic())
            if sys.version_info >= (3, 0):
                f.write((self.timestamp & 0xffffffff).to_bytes(4, 'little'))
                f.write(b'\0\0\0\0')
            else:
                f.write(
                    chr(self.timestamp & 0xff) +
                    chr((self.timestamp >> 8) & 0xff) +
                    chr((self.timestamp >> 16) & 0xff) +
                    chr((self.timestamp >> 24) & 0xff))
            f.write(marshal.dumps(code))
            f.close()

        return code
Exemplo n.º 6
0
    def getLoadedDirname(self, mod):
        """ Returns the directory name that the indicated
        conventionally-loaded module must have been loaded from. """

        if not getattr(mod, '__file__', None):
            return None

        fullname = mod.__name__
        dirname = Filename.fromOsSpecific(mod.__file__).getDirname()

        parentname = None
        basename = fullname
        if '.' in fullname:
            parentname, basename = fullname.rsplit('.', 1)

        path = None
        if parentname:
            parent = sys.modules[parentname]
            path = parent.__path__
        if path is None:
            path = sys.path

        for dir in path:
            pdir = str(Filename.fromOsSpecific(dir))
            if pdir + '/' + basename == dirname:
                # We found it!
                return dir

        # Couldn't figure it out.
        return None
Exemplo n.º 7
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not.  May raise IOError. """

        if self.fileType == FTPythonCompiled or self.fileType == FTExtensionModule:
            return None

        filename = Filename(self.filename)
        filename.setExtension("py")
        filename.setText()
        vfile = vfs.getFile(filename)
        if not vfile:
            raise IOError
        return vfile.readFile(True)
Exemplo n.º 8
0
    def _read_code(self):
        if self.desc[2] == imp.PY_COMPILED:
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            raise IOError('Could not read %s' % self.filename)
        else:
            if self.desc[2] == imp.C_EXTENSION:
                return
        t_pyc = None
        for ext in compiledExtensions:
            pycFilename = Filename(self.filename)
            pycFilename.setExtension(ext)
            pycVfile = vfs.getFile(pycFilename, False)
            if pycVfile:
                t_pyc = pycVfile.getTimestamp()
                break

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

        if not code:
            source = self._read_source()
            filename = Filename(self.filename)
            filename.setExtension('py')
            code = self._compile(filename, source)
        return code
Exemplo n.º 9
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not.  May raise IOError. """

        if self.desc[2] == imp.PY_COMPILED or \
           self.desc[2] == imp.C_EXTENSION:
            return None

        filename = Filename(self.filename)
        filename.setExtension('py')
        filename.setText()
        vfile = vfs.getFile(filename)
        if not vfile:
            raise IOError("Could not find '%s'" % (filename))
        return vfile.readFile(True)
Exemplo n.º 10
0
    def find_module(self, fullname, path = None):
        if path is None:
            dir_path = self.dir_path
        else:
            dir_path = path
        #print >>sys.stderr, "find_module(%s), dir_path = %s" % (fullname, dir_path)
        basename = fullname.split('.')[-1]
        path = Filename(dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(dir_path, vfile, filename,
                             desc=('.py', 'U', imp.PY_SOURCE))

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        for ext in compiledExtensions:
            filename = Filename(path)
            filename.setExtension(ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename,
                                 desc=('.'+ext, 'rb', imp.PY_COMPILED))

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

            filename = Filename(path + desc[0])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename, 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(dir_path, vfile, filename, packagePath=path,
                             desc=('.py', 'U', imp.PY_SOURCE))
        for ext in compiledExtensions:
            filename = Filename(path, '__init__.' + ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename, packagePath=path,
                                 desc=('.'+ext, 'rb', imp.PY_COMPILED))

        #print >>sys.stderr, "not found."
        return None
Exemplo n.º 11
0
 def _import_extension_module(self, fullname):
     vfile = vfs.getFile(self.filename, False)
     if hasattr(vfile, 'getMount') and isinstance(vfile.getMount(),
                                                  VirtualFileMountSystem):
         filename = self.filename
     else:
         if self.filename.exists():
             filename = self.filename
         else:
             filename = Filename.temporary(
                 '',
                 self.filename.getBasenameWoExtension(),
                 '.' + self.filename.getExtension(),
                 type=Filename.TDso)
             filename.setExtension(self.filename.getExtension())
             filename.setBinary()
             sin = vfile.openReadFile(True)
             sout = OFileStream()
             if not filename.openWrite(sout):
                 raise IOError
             if not copyStream(sin, sout):
                 raise IOError
             vfile.closeReadFile(sin)
             del sout
     module = imp.load_module(fullname, None, filename.toOsSpecific(),
                              self.desc)
     module.__file__ = self.filename.toOsSpecific()
     return module
Exemplo n.º 12
0
    def getLoadedDirname(self, mod):
        if not getattr(mod, '__file__', None):
            return
        fullname = mod.__name__
        dirname = Filename.fromOsSpecific(mod.__file__).getDirname()
        parentname = None
        basename = fullname
        if '.' in fullname:
            parentname, basename = fullname.rsplit('.', 1)
        path = None
        if parentname:
            parent = sys.modules[parentname]
            path = parent.__path__
        if path is None:
            path = sys.path
        for dir in path:
            pdir = str(Filename.fromOsSpecific(dir))
            if pdir + '/' + basename == dirname:
                return dir

        return
Exemplo n.º 13
0
 def _read_source(self):
     if self.desc[2] == imp.PY_COMPILED or self.desc[2] == imp.C_EXTENSION:
         return None
     filename = Filename(self.filename)
     filename.setExtension('py')
     filename.setText()
     vfile = vfs.getFile(filename)
     if not vfile:
         raise IOError("Could not find '%s'" % filename)
     return vfile.readFile(True)
Exemplo n.º 14
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not.  May raise IOError,
        ValueError, SyntaxError, or a number of other errors generated
        by the low-level system. """

        if self.desc[2] == imp.PY_COMPILED:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            raise IOError('Could not read %s' % (self.filename))

        elif self.desc[2] == imp.C_EXTENSION:
            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.
        t_pyc = None
        for ext in compiledExtensions:
            pycFilename = Filename(self.filename)
            pycFilename.setExtension(ext)
            pycVfile = vfs.getFile(pycFilename, False)
            if pycVfile:
                t_pyc = pycVfile.getTimestamp()
                break

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

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

        return code
Exemplo n.º 15
0
    def _import_extension_module(self, fullname):
        """ Loads the binary shared object as a Python module, and
        returns it. """

        vfile = vfs.getFile(self.filename, False)

        # We can only import an extension module if it already exists on
        # disk.  This means if it's a truly virtual file that has no
        # on-disk equivalent, we have to write it to a temporary file
        # first.
        if hasattr(vfile, 'getMount') and \
           isinstance(vfile.getMount(), VirtualFileMountSystem):
            # It's a real file.
            filename = self.filename
        elif self.filename.exists():
            # It's a virtual file, but it's shadowing a real file in
            # the same directory.  Assume they're the same, and load
            # the real one.
            filename = self.filename
        else:
            # It's a virtual file with no real-world existence.  Dump
            # it to disk.  TODO: clean up this filename.
            filename = Filename.temporary(
                '',
                self.filename.getBasenameWoExtension(),
                '.' + self.filename.getExtension(),
                type=Filename.TDso)
            filename.setExtension(self.filename.getExtension())
            filename.setBinary()
            sin = vfile.openReadFile(True)
            sout = OFileStream()
            if not filename.openWrite(sout):
                raise IOError
            if not copyStream(sin, sout):
                raise IOError
            vfile.closeReadFile(sin)
            del sout

        module = imp.load_module(fullname, None, filename.toOsSpecific(),
                                 self.desc)
        module.__file__ = self.filename.toOsSpecific()
        return module
Exemplo n.º 16
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not.  May raise IOError. """

        if self.desc[2] == imp.PY_COMPILED or \
           self.desc[2] == imp.C_EXTENSION:
            return None

        filename = Filename(self.filename)
        filename.setExtension('py')
        filename.setText()
        vfile = vfs.getFile(filename)
        if not vfile:
            raise IOError("Could not find '%s'" % (filename))
        return vfile.readFile(True)
Exemplo n.º 17
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not.  May raise IOError. """

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

        filename = Filename(self.filename)
        filename.setExtension('py')
        filename.setText()
        vfile = vfs.getFile(filename)
        if not vfile:
            raise IOError
        return vfile.readFile(True)
Exemplo n.º 18
0
    def _import_extension_module(self, fullname):
        """ Loads the binary shared object as a Python module, and
        returns it. """

        vfile = vfs.getFile(self.filename, False)

        # We can only import an extension module if it already exists on
        # disk.  This means if it's a truly virtual file that has no
        # on-disk equivalent, we have to write it to a temporary file
        # first.
        if hasattr(vfile, 'getMount') and \
           isinstance(vfile.getMount(), VirtualFileMountSystem):
            # It's a real file.
            filename = self.filename
        elif self.filename.exists():
            # It's a virtual file, but it's shadowing a real file in
            # the same directory.  Assume they're the same, and load
            # the real one.
            filename = self.filename
        else:
            # It's a virtual file with no real-world existence.  Dump
            # it to disk.  TODO: clean up this filename.
            filename = Filename.temporary('', self.filename.getBasenameWoExtension(),
                                          '.' + self.filename.getExtension(),
                                          type = Filename.TDso)
            filename.setExtension(self.filename.getExtension())
            filename.setBinary()
            sin = vfile.openReadFile(True)
            sout = OFileStream()
            if not filename.openWrite(sout):
                raise IOError
            if not copyStream(sin, sout):
                raise IOError
            vfile.closeReadFile(sin)
            del sout

        module = imp.load_module(fullname, None, filename.toOsSpecific(),
                                 self.desc)
        module.__file__ = self.filename.toOsSpecific()
        return module
Exemplo n.º 19
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not.  May raise IOError,
        ValueError, SyntaxError, or a number of other errors generated
        by the low-level system. """

        if self.desc[2] == imp.PY_COMPILED:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            raise IOError('Could not read %s' % (self.filename))

        elif self.desc[2] == imp.C_EXTENSION:
            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.
        t_pyc = None
        for ext in compiledExtensions:
            pycFilename = Filename(self.filename)
            pycFilename.setExtension(ext)
            pycVfile = vfs.getFile(pycFilename, False)
            if pycVfile:
                t_pyc = pycVfile.getTimestamp()
                break

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

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

        return code
Exemplo n.º 20
0
    def find_module(self, fullname, path=None):
        if path is None:
            dir_path = self.dir_path
        else:
            dir_path = path
        # print >>sys.stderr, "find_module(%s), dir_path = %s" % (fullname, dir_path)
        basename = fullname.split(".")[-1]
        path = Filename(dir_path, basename)

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

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

        # Look for a C/C++ extension 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(dir_path, vfile, filename, FTExtensionModule, 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(dir_path, vfile, filename, FTPythonSource, packagePath=path)
        for ext in compiledExtensions:
            filename = Filename(path, "__init__." + ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename, FTPythonCompiled, packagePath=path)

        # print >>sys.stderr, "not found."
        return None
Exemplo n.º 21
0
 def __init__(self, path):
     if isinstance(path, Filename):
         self.dir_path = Filename(path)
     else:
         self.dir_path = Filename.fromOsSpecific(path)
Exemplo n.º 22
0
 def __init__(self, path):
     if isinstance(path, Filename):
         self.dir_path = Filename(path)
     else:
         self.dir_path = Filename.fromOsSpecific(path)
Exemplo n.º 23
0
 def __init__(self, path):
     self.dir_path = Filename.fromOsSpecific(path)
Exemplo n.º 24
0
    def find_module(self, fullname, path=None):
        if path is None:
            dir_path = self.dir_path
        else:
            dir_path = path
        basename = fullname.split('.')[-1]
        path = Filename(dir_path, basename)
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(dir_path,
                             vfile,
                             filename,
                             desc=('.py', 'U', imp.PY_SOURCE))
        for ext in compiledExtensions:
            filename = Filename(path)
            filename.setExtension(ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path,
                                 vfile,
                                 filename,
                                 desc=('.' + ext, 'rb', imp.PY_COMPILED))

        for desc in imp.get_suffixes():
            if desc[2] != imp.C_EXTENSION:
                continue
            filename = Filename(path + desc[0])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename, desc=desc)

        filename = Filename(path, '__init__.py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(dir_path,
                             vfile,
                             filename,
                             packagePath=path,
                             desc=('.py', 'U', imp.PY_SOURCE))
        for ext in compiledExtensions:
            filename = Filename(path, '__init__.' + ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path,
                                 vfile,
                                 filename,
                                 packagePath=path,
                                 desc=('.' + ext, 'rb', imp.PY_COMPILED))

        return
Exemplo n.º 25
0
 def getdata(self, path):
     path = Filename(self.dir_path, Filename.fromOsSpecific(path))
     vfile = vfs.getFile(path)
     if not vfile:
         raise IOError("Could not find '%s'" % (path))
     return vfile.readFile(True)
Exemplo n.º 26
0
    def find_module(self, fullname, path=None):
        if path is None:
            dir_path = self.dir_path
        else:
            dir_path = path
        #print >>sys.stderr, "find_module(%s), dir_path = %s" % (fullname, dir_path)
        basename = fullname.split('.')[-1]
        path = Filename(dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(dir_path,
                             vfile,
                             filename,
                             desc=('.py', 'U', imp.PY_SOURCE))

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        for ext in compiledExtensions:
            filename = Filename(path)
            filename.setExtension(ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path,
                                 vfile,
                                 filename,
                                 desc=('.' + ext, 'rb', imp.PY_COMPILED))

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

            filename = Filename(path + desc[0])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path, vfile, filename, 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(dir_path,
                             vfile,
                             filename,
                             packagePath=path,
                             desc=('.py', 'U', imp.PY_SOURCE))
        for ext in compiledExtensions:
            filename = Filename(path, '__init__.' + ext)
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(dir_path,
                                 vfile,
                                 filename,
                                 packagePath=path,
                                 desc=('.' + ext, 'rb', imp.PY_COMPILED))

        #print >>sys.stderr, "not found."
        return None
Exemplo n.º 27
0
 def __init__(self, path):
     self.dir_path = Filename.fromOsSpecific(path)
Exemplo n.º 28
0
 def getdata(self, path):
     path = Filename(self.dir_path, Filename.fromOsSpecific(path))
     vfile = vfs.getFile(path)
     if not vfile:
         raise IOError("Could not find '%s'" % (path))
     return vfile.readFile(True)