add iterator mode for reconstructed index
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 25 Aug 2017 08:27:39 +0000 (10:27 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
deltatar/deltatar.py

index 02670da..2983ac2 100644 (file)
@@ -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)