From: Philipp Gesang Date: Fri, 25 Aug 2017 08:27:39 +0000 (+0200) Subject: add iterator mode for reconstructed index X-Git-Tag: v2.2~7^2~48 X-Git-Url: http://developer.intra2net.com/git/?p=python-delta-tar;a=commitdiff_plain;h=b84beea7df2647d2f3f9d8c1203d02de0654da31 add iterator mode for reconstructed index --- diff --git a/deltatar/deltatar.py b/deltatar/deltatar.py index 02670da..2983ac2 100644 --- a/deltatar/deltatar.py +++ b/deltatar/deltatar.py @@ -1169,6 +1169,43 @@ class DeltaTar(object): except StopIteration: break + def iterate_disaster_index (self, index): + """ + Mimick the behavior of the other object iterators, just with the inputs + supplied directly as *index*. + """ + + class RawIndexIterator(object): + def __init__(self, delta_tar, tar_path, index): + self.delta_tar = delta_tar + self.index = index + self.__enter__() + + def __iter__(self): + return self + + def release(self): + if self.tar_obj: + self.tar_obj.close() + + def __enter__(self): + ''' + Allows this iterator to be used with the "with" statement + ''' + self.iter = self.index.__iter__ () + return self + + def __exit__(self, type, value, tb): + ''' + Allows this iterator to be used with the "with" statement + ''' + + def __next__(self): + idxent = self.iter.__next__ () + return idxent + + return RawIndexIterator(self, index) + def collate_iterators(self, it1, it2): ''' Collate two iterators, so that it returns pairs of the items of each @@ -1296,7 +1333,7 @@ class DeltaTar(object): def restore_backup(self, target_path, backup_indexes_paths=[], backup_tar_path=None, restore_callback=None, - disaster=tarfile.TOLERANCE_STRICT): + disaster=tarfile.TOLERANCE_STRICT, backup_index=None): ''' Restores a backup. @@ -1331,7 +1368,9 @@ class DeltaTar(object): if backup_indexes_paths is None and backup_tar_path == []: raise Exception("You have to either provide index paths or a tar path") - if len(backup_indexes_paths) == 0: + if isinstance (backup_index, list) is True: + mode = "disaster" + elif len(backup_indexes_paths) == 0: mode = "tar" else: mode = "diff" @@ -1402,6 +1441,10 @@ class DeltaTar(object): self.logger.error("failed to read file [%s]: %s; is this an " "actual index file?" % (index1, str (exn))) return [(index1, exn)] + elif mode == "disaster": + index_it = self.iterate_disaster_index (backup_index) + helper = RestoreHelper (self, cwd, disaster=disaster) + dir_it = self._recursive_walk_dir('.') dir_path_it = self.jsonize_path_iterator(dir_it) @@ -1497,12 +1540,13 @@ class DeltaTar(object): files may be corrupt; skim files for header-like information and attempt to retrieve the data. """ - faux_index = tarfile.gen_rescue_index(backup_tar_path, - password=self.password, - key=self.crypto_key) + backup_index = tarfile.gen_rescue_index(backup_tar_path, + self.mode, + password=self.password, + key=self.crypto_key) return self.restore_backup(target_path, - backup_index=faux_index, + backup_index=backup_index, disaster=tarfile.TOLERANCE_RESCUE)