def load_hashfile(self, uri): # load hashfile files_to_add=[] self.format=self.get_hashfile_format(uri) if not self.format: log("unknown format") return log("Detected format: " + self.format.name) # load content uri = vfs_clean_uri(uri) content = gnomevfs.read_entire_file(uri) lines = content.split("\n") list = self.format.read_file(lines) root = os.path.dirname(uri) for hash, file in list: absfile = os.path.join(root, file) u = gnomevfs.escape_host_and_path_string(absfile) files_to_add.append( (u, file, hash) ) # reset hashfile #self.new_hashfile() # do add the files to list for f in files_to_add: self.files.append(File(f[0], f[1], f[2])) self.filename=uri self.update_hashfile() self.update_ui() self.update_and_check_file_list() return True
def load_hashfile(self, uri): # load hashfile files_to_add = [] self.format = self.get_hashfile_format(uri) if not self.format: log("unknown format") return log("Detected format: " + self.format.name) # load content uri = vfs_clean_uri(uri) content = gnomevfs.read_entire_file(uri) lines = content.split("\n") list = self.format.read_file(lines) root = os.path.dirname(uri) for hash, file in list: absfile = os.path.join(root, file) u = gnomevfs.escape_host_and_path_string(absfile) files_to_add.append((u, file, hash)) # reset hashfile #self.new_hashfile() # do add the files to list for f in files_to_add: self.files.append(File(f[0], f[1], f[2])) self.filename = uri self.update_hashfile() self.update_ui() self.update_and_check_file_list() return True
def read_entire_file(uri): try: uri = gnomevfs.read_entire_file(uri) except: log("Warning: Couldn't read file \"%s\"." % (uri,)) raise return uri
def file_read(file): "Reads data from a file" try: if file is None: raise IOError return gnomevfs.read_entire_file(file) except gnomevfs.Error: raise IOError
def get_hashfile_format(self, uri): for format in formats: # search in al our recognized formats regex = format.filename_regex result = regex.search(uri.lower()) if result: # this can be a valid filename, now look inside uri = vfs_clean_uri(uri) content = gnomevfs.read_entire_file(uri) content = content.split("\n") if format.detect_file(content): # yes, this is a valid hashfile \o/ return format return None
def on_filelist_drag_data_received(self, widget, drag_context, x, y, selection_data, info, timestamp): files = selection_data.data.split() drag_context.drop_finish(True, timestamp) for f in files: # remove trailing noise f = f.strip("\r\n\x00", ) uri = vfs_clean_uri(f) lines = None try: content = gnomevfs.read_entire_file(uri) except gnomevfs.IsDirectoryError: pass except gnomevfs.NotFoundError: pass else: lines = content.split("\n") if lines and self.get_hashfile_format(uri, lines): glade = os.path.join(DATADIR, "parano.glade") dialog = gtk.glade.XML(glade, "dialog_add_or_open") dialog = dialog.get_widget("dialog_add_or_open") result = dialog.run() dialog.hide_all() if result == gtk.RESPONSE_CANCEL: # abort drop return if result == gtk.RESPONSE_CLOSE: # open new hashfile self.load_hashfile(f) return # add the file or folder self.add_file(f) self.modified = True self.update_ui()
def on_filelist_drag_data_received(self, widget, drag_context, x, y, selection_data, info, timestamp): files = selection_data.data.split() drag_context.drop_finish(True, timestamp) for f in files: # remove trailing noise f = f.strip("\r\n\x00",) uri = vfs_clean_uri(f) lines = None try: content = gnomevfs.read_entire_file(uri) except gnomevfs.IsDirectoryError: pass except gnomevfs.NotFoundError: pass else: lines = content.split("\n") if lines and self.get_hashfile_format(uri, lines): glade = os.path.join(DATADIR, "parano.glade") dialog = gtk.glade.XML(glade,"dialog_add_or_open") dialog = dialog.get_widget("dialog_add_or_open") result = dialog.run() dialog.hide_all() if result == gtk.RESPONSE_CANCEL: # abort drop return if result == gtk.RESPONSE_CLOSE: # open new hashfile self.load_hashfile(f) return # add the file or folder self.add_file(f) self.modified=True self.update_ui()
def restore(self, backup, spath, dpath=None): """ Restore one file or directory from the backup tdir with name spath to dpath (or to its old location). All existing files must be moved to a "*.before_restore_$time" files. """ if not dpath: dpath = spath # Gather spath and dpath information if spath[0] == "/": spath = spath[1:] (sparent, sname) = os.path.split(spath) if not sname: spath = sparent (sparent, sname) = os.path.split(sparent) dpath = os.path.normpath(dpath) (dparent, dname) = os.path.split(dpath) if not dname: dpath = dparent (dparent, dname) = os.path.split(dpath) now = datetime.datetime.now().isoformat("_").replace(":", ".") ver = str(gnomevfs.read_entire_file(backup + "/ver")) try: if ver[:3] == "1.4": self.childlist = [ x[1:] for x in gnomevfs.read_entire_file(backup + "/flist").split("\000") if x == "/" + spath or x[1:len(spath) + 2] == spath + "/" ] else: print("Only snapshot version 1.4 is supported") return False except: print("E: Error opening backup snapshot metadata") return False if len(self.childlist) == 0: print("E: File not found in the backup snapshot") return False if os.path.exists(dpath): if os.path.isdir(dpath): tdir = tempfile.mkdtemp(dir=dpath) self.extract(backup, spath, tdir) for _file in self.childlist: if len(self.childlist) == 1: bname = sname src = os.path.join(tdir, spath) else: bname = _file[len(spath) + 1:] src = os.path.join(tdir, spath, bname) dst = os.path.join(dpath, bname) if os.path.isdir(src): if not os.path.exists(dst): os.makedirs(dst) srcstat = os.stat(src) os.chown(dst, srcstat.st_uid, srcstat.st_gid) os.chmod(dst, srcstat.st_mode) elif os.path.isfile(src) or os.path.islink(src): if os.path.exists(dst) and not filecmp.cmp(src, dst): shutil.move(dst, dst + ".before_restore_" + now) if not os.path.exists(dst): shutil.move(src, dst) else: print( "W: Path '%s' is neither directory nor file! Skipped." % src) shutil.rmtree(tdir) else: tdir = tempfile.mkdtemp(dir=dparent) self.extract(backup, spath, tdir) shutil.move(dpath, dpath + ".before_restore_" + now) shutil.move(os.path.join(tdir, spath), dpath) shutil.rmtree(tdir) else: tdir = tempfile.mkdtemp(dir=dparent) self.extract(backup, spath, tdir) shutil.move(os.path.join(tdir, spath), dpath) shutil.rmtree(tdir) return True
def load_tree(self, target): """ Loads the tree information from the target backup directory """ self.treestore.clear() # Checking if the target directory is local or remote local = True try: if gnomevfs.URI(target).is_local: target = gnomevfs.get_local_path_from_uri(target) else: local = False except: pass self.local = local self.target = target # Checking if it is a readable directory if local: if not (os.path.exists(target) and os.path.isdir(target) and os.access(target, os.R_OK | os.X_OK)): self.treestore.append( None, ["Error: backups directory does not exist!"]) self.target = False else: if not (gnomevfs.exists(target) and gnomevfs.get_file_info(target).type == 2): self.treestore.append( None, ["Error: backups directory does not exist!"]) self.target = False # Get list of backup directories r = re.compile( r"^(\d{4})-(\d{2})-(\d{2})_(\d{2})[\:\.](\d{2})[\:\.](\d{2})\.\d+\..*?\.(.+)$" ) listing = [] if local and self.target: listing = os.listdir(target) listing = filter(r.search, listing) elif self.target: try: d = gnomevfs.open_directory(target) listing = [] for f in d: if f.type == 2 and f.name != "." and f.name != ".." and r.search( f.name): listing.append(f.name) except: pass # Check if these directories are complete and remove from the list those that are not for adir in listing[:]: if not gnomevfs.exists(self.target + "/" + adir + "/ver"): listing.remove(adir) continue else: if str( gnomevfs.read_entire_file(self.target + "/" + adir + "/ver"))[:3] != "1.4": listing.remove(adir) continue listing.sort() listing.reverse() self.vtree = {} if listing == []: self.treestore.append( None, ["Error: no backups found in the target directory"]) self.target = False else: for base in listing: if str(gnomevfs.read_entire_file(target + "/" + base + "/ver"))[:3] == "1.4": self.vtree[base] = str( gnomevfs.read_entire_file(target + "/" + base + "/flist")).split("\000") else: self.vtree[base] = str( gnomevfs.read_entire_file(target + "/" + base + "/flist")).split("\n") self.blist.clear() for base in listing: self.blist.append([base]) self.good = False self.on_selection_change() if self.target: self.treestore.append(None, [ "Select any of the available backups to see list of files that can be restored." ])
def get_contents(self): return gnomevfs.read_entire_file(self.get_text_uri())