Explorar el Código

Properly apply --exclude wildcards to the full path.

Closes #5
Jonas Borgström hace 11 años
padre
commit
2068e7cf34
Se han modificado 3 ficheros con 25 adiciones y 14 borrados
  1. 2 0
      CHANGES
  2. 3 5
      attic/helpers.py
  3. 20 9
      attic/testsuite/helpers.py

+ 2 - 0
CHANGES

@@ -8,6 +8,8 @@ Version 0.10
 
 
 (feature release, released on X)
 (feature release, released on X)
 
 
+- "--exclude" wildcard patterns are now properly applied to the full path
+  not just the file name part (#5).
 - Make source code endianness agnostic (#1)
 - Make source code endianness agnostic (#1)
 
 
 Version 0.9
 Version 0.9

+ 3 - 5
attic/helpers.py

@@ -178,14 +178,12 @@ class ExcludePattern(IncludePattern):
     """
     """
     def __init__(self, pattern):
     def __init__(self, pattern):
         self.pattern = self.dirpattern = pattern
         self.pattern = self.dirpattern = pattern
-        if not pattern.endswith(os.path.sep):
-            self.dirpattern += os.path.sep
+        if not pattern.endswith('/'):
+            self.dirpattern += '/*'
 
 
     def match(self, path):
     def match(self, path):
         dir, name = os.path.split(path)
         dir, name = os.path.split(path)
-        return (path == self.pattern
-                or (dir + os.path.sep).startswith(self.dirpattern)
-                or fnmatchcase(name, self.pattern))
+        return fnmatchcase(path, self.pattern) or fnmatchcase(dir + '/', self.dirpattern)
 
 
     def __repr__(self):
     def __repr__(self):
         return '%s(%s)' % (type(self), self.pattern)
         return '%s(%s)' % (type(self), self.pattern)

+ 20 - 9
attic/testsuite/helpers.py

@@ -2,7 +2,7 @@ from datetime import datetime
 import os
 import os
 import tempfile
 import tempfile
 import unittest
 import unittest
-from attic.helpers import Location, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, UpgradableLock
+from attic.helpers import adjust_patterns, exclude_path, Location, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, UpgradableLock
 from attic.testsuite import AtticTestCase
 from attic.testsuite import AtticTestCase
 
 
 
 
@@ -48,15 +48,26 @@ class FormatTimedeltaTestCase(AtticTestCase):
 
 
 class PatternTestCase(AtticTestCase):
 class PatternTestCase(AtticTestCase):
 
 
+    files = [
+        '/etc/passwd', '/etc/hosts',
+        '/home/user/.profile', '/home/user/.bashrc',
+        '/home/user2/.profile', '/home/user2/public_html/index.html',
+        '/var/log/messages', '/var/log/dmesg',
+    ]
+
+    def evaluate(self, paths, excludes):
+        patterns = adjust_patterns(paths, [ExcludePattern(p) for p in excludes])
+        return [path for path in self.files if not exclude_path(path, patterns)]
+
     def test(self):
     def test(self):
-        self.assert_equal(IncludePattern('/usr').match('/usr'), True)
-        self.assert_equal(IncludePattern('/usr').match('/usr/bin'), True)
-        self.assert_equal(IncludePattern('/usr').match('/usrbin'), False)
-        self.assert_equal(ExcludePattern('*.py').match('foo.py'), True)
-        self.assert_equal(ExcludePattern('*.py').match('foo.pl'), False)
-        self.assert_equal(ExcludePattern('/tmp').match('/tmp'), True)
-        self.assert_equal(ExcludePattern('/tmp').match('/tmp/foo'), True)
-        self.assert_equal(ExcludePattern('/tmp').match('/tmofoo'), False)
+        self.assert_equal(self.evaluate(['/'], ['/home']),
+                          ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
+        self.assert_equal(self.evaluate(['/'], ['*.profile', '/var/log']),
+                          ['/etc/passwd', '/etc/hosts', '/home/user/.bashrc', '/home/user2/public_html/index.html'])
+        self.assert_equal(self.evaluate(['/'], ['/home/*/public_html', '*.profile', '*/log/*']),
+                          ['/etc/passwd', '/etc/hosts', '/home/user/.bashrc'])
+        self.assert_equal(self.evaluate(['/etc', '/var'], ['dmesg']),
+                          ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
 
 
 
 
 class MakePathSafeTestCase(AtticTestCase):
 class MakePathSafeTestCase(AtticTestCase):