deltatar: adding support for filtering in restore index and adding unit tests
authorEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 09:35:41 +0000 (11:35 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 09:35:41 +0000 (11:35 +0200)
deltatar/deltatar.py
testing/test_deltatar.py

index fdb5a62..ef7ef98 100644 (file)
@@ -200,6 +200,7 @@ class DeltaTar(object):
         2. excluded_files
         3. filter_func (which must return whether the file is accepted or not)
         '''
+
         if len(source_path) > 0:
             path = path[len(source_path):]
 
@@ -574,6 +575,7 @@ class DeltaTar(object):
                                 password=self.password,
                                 new_volume_handler=new_volume_handler)
             os.chdir(target_path)
+            # TODO: filtering here
             tarobj.extractall()
             os.chdir(cwd)
             tarobj.close()
@@ -628,7 +630,10 @@ class DeltaTar(object):
                                      'restored: %s, line %d' % (op_type, l_no))
                     continue
 
-                # TODO: filter by j.get('path', '')
+                # filtering paths
+                op_path  = j.get('path', '')
+                if not self.filter_path(op_path, '.'):
+                    continue
 
                 vol_no = j.get('volume', -1)
                 if not isinstance(vol_no, int) or vol_no < 0:
@@ -653,7 +658,7 @@ class DeltaTar(object):
                 offset = j.get('offset', -1)
                 if tarobj:
                     member = tarobj.next()
-                    if member.path != j['path']:
+                    if member.path != op_path:
                         # force a seek and reopen
                         tarobj.close()
                         tarobj = None
index b844beb..8d4c813 100644 (file)
@@ -351,12 +351,12 @@ class DeltaTarTest(BaseTest):
 
         deltatar.restore_backup(target_path="source_dir",
                                 backup_tar_path=tar_path)
-        assert visited_paths == [
+        assert set(visited_paths) == set([
                 '/small',
                 '/test',
                 '/test/huge2',
                 '/test/test2'
-            ]
+            ])
 
     def test_create_filter_out_func(self):
         '''
@@ -391,16 +391,87 @@ class DeltaTarTest(BaseTest):
 
         deltatar.restore_backup(target_path="source_dir",
                                 backup_tar_path=tar_path)
-        assert visited_paths == [
+        assert set(visited_paths) == set([
                 '/small',
                 '/test'
-            ]
+            ])
 
         # check that effectively no file was backed up
         assert not os.path.exists("./source_dir/small")
         assert not os.path.exists("./source_dir/big")
         assert not os.path.exists("./source_dir/test")
 
+    def test_restore_index_basic_filtering(self):
+        '''
+        Creates a backup, and then filter when doing the index based restore.
+        '''
+        deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+                            logger=self.consoleLogger)
+
+        # create first backup
+        deltatar.create_full_backup(
+            source_path="source_dir",
+            backup_path="backup_dir")
+
+        assert os.path.exists("backup_dir")
+        shutil.rmtree("source_dir")
+
+        index_filename = deltatar.index_name_func(True)
+        index_path = os.path.join("backup_dir", index_filename)
+
+        deltatar.included_files = ["/test", "/small"]
+        deltatar.excluded_files = ["/test/huge"]
+        deltatar.restore_backup(target_path="source_dir",
+            backup_indexes_paths=[index_path])
+
+        assert os.path.exists("./source_dir/small")
+        assert os.path.exists("./source_dir/test")
+        assert os.path.exists("./source_dir/test/huge2")
+        assert os.path.exists("./source_dir/test/test2")
+
+        assert not os.path.exists("./source_dir/test/huge")
+        assert not os.path.exists("./source_dir/big")
+
+    def test_restore_index_filter_func(self):
+        '''
+        Creates a backup, and then filter when doing the index based restore,
+        using the filter function.
+        '''
+        visited_paths = []
+        def filter_func(visited_paths, path):
+            if path not in visited_paths:
+                visited_paths.append(path)
+            return True
+
+        filter_func = partial(filter_func, visited_paths)
+        deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+                            logger=self.consoleLogger)
+
+        # create first backup
+        deltatar.create_full_backup(
+            source_path="source_dir",
+            backup_path="backup_dir")
+
+        assert os.path.exists("backup_dir")
+        shutil.rmtree("source_dir")
+
+        index_filename = deltatar.index_name_func(True)
+        index_path = os.path.join("backup_dir", index_filename)
+
+        deltatar.included_files = ["/test", "/small"]
+        deltatar.excluded_files = ["/test/huge"]
+        deltatar.filter_func = filter_func
+        deltatar.restore_backup(target_path="source_dir",
+            backup_indexes_paths=[index_path])
+
+        assert set(visited_paths) == set([
+                '/small',
+                '/test',
+                '/test/huge2',
+                '/test/test2'
+            ])
+
+
 class DeltaTar2Test(DeltaTarTest):
     '''
     Same as DeltaTar but with specific ":" mode