def dump_object_to_string(object): try: return pickle.dumps(object) except Exception as e: raise Error('Cannot dump Python object to string', [NativeError(e, sys.exc_traceback)]) pass
def execute_file(self, file): assert isinstance(file, VFSFile), file assert file.parent() is not None assert isinstance(file.parent(), VFSDirectory) chdirbackto = None try: if file.is_persistent(): dir_to_change_back_to = os.getcwd() # change to the directory that contains the file. note # that file.parent() is pointless in the days of union # filesystem. dir_to_change_to = os.path.dirname(os.sep.join(file.abspath())) os.chdir(dir_to_change_to) execfile(file.name(), self.__context) os.chdir(dir_to_change_back_to) return else: exec '\n'.join(file.lines()) in self.__context return pass except Exception, e: if chdirbackto is not None: os.chdir(chdirbackto) pass raise Error('Error executing ' + '/'.join(file.abspath()), [NativeError(e, sys.exc_traceback)])
def sync(self): """ (VFSEntry implementation) """ assert self.filesystem() is not None if self.__state == FileState.NEW: filename = os.sep.join(self.abspath()) try: libconfix.core.utils.helper.write_lines_to_file_if_changed( filename=filename, lines=self.__lines) except Error as err: raise Error('Could not write file '+filename, [err]) if self.mode() is not None: try: os.chmod(filename, self.mode()) except OSError as err: raise Error('Could not change mode of file '+filename, [NativeError(err, sys.exc_traceback)]) pass if FileSystem.CLEAR_ON_SYNC in self.filesystem().flags(): self.__lines = None self.__state = FileState.SYNC_CLEAR else: self.__state = FileState.SYNC_INMEM pass return if self.__state == FileState.SYNC_CLEAR: return if self.__state == FileState.SYNC_INMEM: if FileSystem.CLEAR_ON_SYNC in self.filesystem().flags(): self.__lines = None self.__state = FileState.SYNC_CLEAR pass return if self.__state == FileState.DIRTY: filename = os.sep.join(self.abspath()) try: libconfix.core.utils.helper.write_lines_to_file( filename=filename, lines=self.__lines) except Error as err: raise Error('Could not write file '+filename, [err]) if FileSystem.CLEAR_ON_SYNC in self.filesystem().flags(): self.__lines = None self.__state = FileState.SYNC_CLEAR else: self.__state = FileState.SYNC_INMEM pass return if self.__state == FileState.VIRTUAL: return assert 0 pass
def load_object_from_string(string): try: object = pickle.loads(string) except Exception as e: raise Error('Cannot read Python object from string', [NativeError(e, sys.exc_traceback)]) return object
def execute_pieces(self, pieces): for p in pieces: try: exec '\n'.join(p.lines()) in self.__context except Exception, e: raise Error( 'Error in code piece starting at line ' + str(p.start_lineno()) + ' (' + p.lines()[0] + ')', [NativeError(e, sys.exc_traceback)]) pass
def dump_object_to_file(object, filename): try: file = open(filename, 'w') except IOError as e: raise Error('Cannot open file ' + filename + ' for writing', [e]) try: pickle.dump(object, file) except Exception as e: raise Error( 'Cannot dump Python object "' + str(object) + '" to file ' + filename, [NativeError(e, sys.exc_traceback)]) pass
def load_object_from_file(filename): try: file = open(filename, 'r') except IOError as e: raise Error('Cannot open file ' + filename + ' for reading', [e]) try: object = pickle.load(file) except Exception as e: raise Error('Cannot read Python object from file ' + filename, [NativeError(e, sys.exc_traceback)]) return object
def sync(self): """ (VFSEntry implementation) """ if self.__state == DirectoryState.SYNC: pass elif self.__state == DirectoryState.INMEMORY: try: path = os.sep.join(self.abspath()) if self.mode() is None: os.mkdir(path) else: os.mkdir(path, self.mode()) pass self.__state = DirectoryState.SYNC pass except OSError, err: raise Error('Could not create directory ' + path, [NativeError(err, sys.exc_traceback)]) pass
def sync(self): """ (VFSEntry implementation) """ assert self.filesystem() is not None if self.__state == FileState.NEW: filename = os.sep.join(self.abspath()) try: libconfix.core.utils.helper.write_lines_to_file_if_changed( filename=filename, lines=self.__lines) except Error, err: raise Error('Could not write file '+filename, [err]) if self.mode() is not None: try: os.chmod(filename, self.mode()) except OSError, err: raise Error('Could not change mode of file '+filename, [NativeError(err, sys.exc_traceback)]) pass
def sync(self): """ (VFSEntry implementation) """ if self.__state == DirectoryState.SYNC: pass elif self.__state == DirectoryState.INMEMORY: try: path = os.sep.join(self.abspath()) if self.mode() is None: os.mkdir(path) else: os.mkdir(path, self.mode()) pass self.__state = DirectoryState.SYNC pass except OSError as err: raise Error('Could not create directory ' + path, [NativeError(err, sys.exc_traceback)]) pass else: assert 0 pass # recursively sync child entries child_errors = [] for name, entry in self.entries(): try: entry.sync() pass except Error as err: child_errors.append(err) pass pass if len(child_errors): raise Error('Could not sync child nodes of directory ' + path, child_errors) pass
def __init__(self, file): self.context_ = {} # remember filename for subsequent error messages self.filename_ = file.abspath() try: chdirbackto = None if file.is_persistent(): chdirbackto = os.getcwd() os.chdir(os.sep.join(file.parent().abspath())) execfile(file.name(), self.context_) os.chdir(chdirbackto) return else: exec '\n'.join(file.lines()) in self.context_ return pass except Exception, e: if chdirbackto is not None: os.chdir(chdirbackto) pass raise Error('Error in ' + '/'.join(file.abspath()), [NativeError(e, sys.exc_traceback)])
import cPickle mypickle = cPickle pass def load_object_from_file(filename): try: file = open(filename, 'r') except IOError, e: raise Error('Cannot open file ' + filename + ' for reading', [e]) try: object = mypickle.load(file) except Exception, e: raise Error('Cannot read Python object from file ' + filename, [NativeError(e, sys.exc_traceback)]) return object def dump_object_to_file(object, filename): try: file = open(filename, 'w') except IOError, e: raise Error('Cannot open file ' + filename + ' for writing', [e]) try: mypickle.dump(object, file) except Exception, e: raise Error( 'Cannot dump Python object "' + str(object) + '" to file ' + filename, [NativeError(e, sys.exc_traceback)])