Properly apply --exclude wildcards to the full path.

Closes #5
This commit is contained in:
Jonas Borgström 2014-01-30 20:33:29 +01:00
parent 1520a911af
commit 2068e7cf34
3 changed files with 25 additions and 14 deletions

View file

@ -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

View file

@ -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)

View file

@ -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):