first implementation of filtered restore_backup and unit test
authorEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 08:17:04 +0000 (10:17 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 08:17:04 +0000 (10:17 +0200)
deltatar/deltatar.py
testing/test_deltatar.py

index c7eea43..fdb5a62 100644 (file)
@@ -200,61 +200,67 @@ class DeltaTar(object):
         2. excluded_files
         3. filter_func (which must return whether the file is accepted or not)
         '''
-        #print "filter: path '%s', source_path: '%s'" % (path, source_path)
-
-        if len(path) > 0:
+        if len(source_path) > 0:
             path = path[len(source_path):]
 
         # 1. filter included_files
         if len(self.included_files) > 0:
+            matches = False
             for i in self.included_files:
                 # it can be either a regexp or a string
-                if isinstance(i, str):
+                if isinstance(i, basestring):
                     # if the string matches, then continue
                     if i == path:
-                        continue
+                        matches = True
+                        break
 
                     # if the string ends with / it's a directory, and if the
                     # path does not start with the directory, then it's not
                     # included
-                    if i.endswith('/') and not path.startswith(i):
-                        return False
+                    if i.endswith('/') and path.startswith(i):
+                        matches = True
+                        break
 
                     # if the string doesn't end with /, add it and do the same
                     # check
-                    elif not path.startswith(i + '/'):
-                        return False
+                    elif path.startswith(i + '/'):
+                        matches = True
+                        break
 
                 # if it's a reg exp, then we just check if it matches
                 elif isinstance(i, re._pattern_type):
-                    if not i.match(path):
-                        return False
+                    if i.match(path):
+                        matches = True
+                        break
                 else:
                     self.logger.warn('Invalid pattern in included_files: %s' % str(i))
 
+            if not matches:
+                return False
+
             for e in self.excluded_files:
                 # it can be either a regexp or a string
-                if isinstance(i, str):
+                if isinstance(e, basestring):
                     # if the string matches, then exclude
-                    if i == path:
+                    if e == path:
                         return False
 
                     # if the string ends with / it's a directory, and if the
                     # path starts with the directory, then exclude
-                    if i.endswith('/') and path.startswith(i):
+                    if e.endswith('/') and path.startswith(e):
                         return False
 
                     # if the string doesn't end with /, do the same check with
                     # the slash added
-                    elif path.startswith(i + '/'):
+                    elif path.startswith(e + '/'):
                         return False
 
                 # if it's a reg exp, then we just check if it matches
-                elif isinstance(i, re._pattern_type):
-                    if i.match(path):
+                elif isinstance(e, re._pattern_type):
+                    if e.match(path):
                         return False
                 else:
-                    self.logger.warn('Invalid pattern in excluded_files: %s' % str(i))
+                    self.logger.warn('Invalid pattern in excluded_files: %s' % str(e))
 
         if self.filter_func:
             return self.filter_func(path)
index 27b2388..4368ba8 100644 (file)
@@ -290,6 +290,37 @@ class DeltaTarTest(BaseTest):
             if value:
                 assert value == self.md5sum(key)
 
+    def test_create_basic_filtering(self):
+        '''
+        Tests create backup basic filtering.
+        '''
+        deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+                            logger=self.consoleLogger,
+                            included_files=["/test", "/small"],
+                            excluded_files=["/test/huge"])
+
+        # 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")
+
+        tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+        tar_path = os.path.join("backup_dir", tar_filename)
+
+        deltatar.restore_backup(target_path="source_dir",
+                                backup_tar_path=tar_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")
+
 class DeltaTar2Test(DeltaTarTest):
     '''
     Same as DeltaTar but with specific ":" mode