diff --git a/CHANGES b/CHANGES index 06e8018d0..26b85b663 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,8 @@ Version 0.10 (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) Version 0.9 diff --git a/attic/helpers.py b/attic/helpers.py index e3f3b8df5..1d9b5ba69 100644 --- a/attic/helpers.py +++ b/attic/helpers.py @@ -178,14 +178,12 @@ class ExcludePattern(IncludePattern): """ def __init__(self, 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): 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): return '%s(%s)' % (type(self), self.pattern) diff --git a/attic/testsuite/helpers.py b/attic/testsuite/helpers.py index da0aff91a..0ff79ab76 100644 --- a/attic/testsuite/helpers.py +++ b/attic/testsuite/helpers.py @@ -2,7 +2,7 @@ from datetime import datetime import os import tempfile 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 @@ -48,15 +48,26 @@ class FormatTimedeltaTestCase(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): - 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):