def _import_compiled_module(self, fullname): """ Loads the compiled C/C++ shared object as a Python module, and returns it. """ vfile = vfs.getFile(self.filename, False) # We can only import a compiled 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 else: # It's a virtual file. Dump it. filename = Filename.temporary('', self.filename.getBasenameWoExtension(), '.' + self.filename.getExtension(), type = Filename.TDso) filename.setExtension(self.filename.getExtension()) fin = open(vfile, 'rb') fout = open(filename, 'wb') data = fin.read(4096) while data: fout.write(data) data = fin.read(4096) fin.close() fout.close() module = imp.load_module(fullname, None, filename.toOsSpecific(), self.desc) module.__file__ = self.filename.cStr() return module
def _import_compiled_module(self, fullname): """ Loads the compiled C/C++ shared object as a Python module, and returns it. """ vfile = vfs.getFile(self.filename, False) # We can only import a compiled 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 else: # It's a virtual file. Dump it. filename = Filename.temporary( '', self.filename.getBasenameWoExtension(), '.' + self.filename.getExtension(), type=Filename.TDso) filename.setExtension(self.filename.getExtension()) fin = open(vfile, 'rb') fout = open(filename, 'wb') data = fin.read(4096) while data: fout.write(data) data = fin.read(4096) fin.close() fout.close() module = imp.load_module(fullname, None, filename.toOsSpecific(), self.desc) module.__file__ = self.filename.cStr() return module
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
def _loadPyc(self, vfile, timestamp): """ Reads and returns the marshal data from a .pyc file. """ code = None f = open(vfile, 'rb') if f.read(4) == imp.get_magic(): t = struct.unpack('<I', f.read(4))[0] if not timestamp or t == timestamp: code = marshal.loads(f.read()) f.close() return code
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()
def getdata(self, path): path = Filename(self.dir_path, Filename.fromOsSpecific(path)) f = open(path, 'rb') return f.read()