Bug 893976 - Use (cached) regular expressions for mozpack.path.match. r=gps

This commit is contained in:
Mike Hommey 2013-07-30 08:57:27 +09:00
parent d5467c1467
commit 5b1a1afd94
4 changed files with 15 additions and 23 deletions

View File

@ -588,7 +588,7 @@ class FileFinder(BaseFinder):
for p in os.listdir(os.path.join(self.base, base)):
if p.startswith('.') and not pattern[0].startswith('.'):
continue
if re.match(mozpack.path.translate(pattern[0]), p):
if mozpack.path.match(p, pattern[0]):
for p_, f in self._find_glob(mozpack.path.join(base, p),
pattern[1:]):
yield p_, f

View File

@ -260,10 +260,11 @@ class OmniJarFormatter(FlatFormatter):
omnijar archive.
'''
base = self._get_base(path)
path = mozpack.path.split(mozpack.path.relpath(path, base))
path = mozpack.path.relpath(path, base)
if any(mozpack.path.match(path, p.replace('*', '**'))
for p in self._non_resources):
return False
path = mozpack.path.split(path)
if path[0] == 'chrome':
return len(path) == 1 or path[1] != 'icons'
if path[0] == 'components':

View File

@ -77,6 +77,8 @@ def basedir(path, bases):
return b
re_cache = {}
def match(path, pattern):
'''
Return whether the given path matches the given pattern.
@ -94,27 +96,13 @@ def match(path, pattern):
'''
if not pattern:
return True
if isinstance(path, basestring):
path = split(path)
if isinstance(pattern, basestring):
pattern = split(pattern)
if pattern[0] == '**':
if len(pattern) == 1:
return True
return any(match(path[n:], pattern[1:]) for n in xrange(len(path)))
if len(pattern) > 1:
return match(path[:1], pattern[:1]) and match(path[1:], pattern[1:])
if path:
return re.match(translate(pattern[0]), path[0]) is not None
return False
def translate(pattern):
'''
Translate the globbing pattern to a regular expression.
'''
return re.escape(pattern).replace('\*', '.*') + '$'
if not pattern in re_cache:
pattern = re.escape(pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*\\\/', r'\1(?:.+/)?', pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*$', r'(?:\1.+)?', pattern)
pattern = pattern.replace(r'\*', '[^/]*') + '(?:/.*)?$'
re_cache[pattern] = re.compile(pattern)
return re_cache[pattern].match(path) is not None
def rebase(oldbase, base, relativepath):

View File

@ -110,6 +110,9 @@ class TestPath(unittest.TestCase):
self.assertTrue(match('foo/bar/baz.qux', '**/*.qux'))
self.assertFalse(match('foo/bar/baz.qux', '**.qux'))
self.assertFalse(match('foo/bar', 'foo/*/bar'))
self.assertTrue(match('foo/bar/baz.qux', 'foo/**/bar/**'))
self.assertFalse(match('foo/nobar/baz.qux', 'foo/**/bar/**'))
self.assertTrue(match('foo/bar', 'foo/**/bar/**'))
def test_rebase(self):
self.assertEqual(rebase('foo', 'foo/bar', 'bar/baz'), 'baz')