From 4290b6b425edc3a35ba76741d83925ca3a9c960d Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Wed, 21 Aug 2019 03:12:36 +0000 Subject: [PATCH] Bug 1575375 - Fix mozpath.match with python 3.7. r=nalexander Python 3.7 changed what it escapes with re.escape. Adapt mozpath.match to account for this. Differential Revision: https://phabricator.services.mozilla.com/D42750 --- python/mozbuild/mozpack/path.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/mozbuild/mozpack/path.py b/python/mozbuild/mozpack/path.py index 3510037bcd4d..063d7acb86c7 100644 --- a/python/mozbuild/mozpack/path.py +++ b/python/mozbuild/mozpack/path.py @@ -93,6 +93,13 @@ def basedir(path, bases): re_cache = {} +# Python versions < 3.7 return r'\/' for re.escape('/'). +if re.escape('/') == '/': + MATCH_STAR_STAR_RE = re.compile(r'(^|/)\\\*\\\*/') + MATCH_STAR_STAR_END_RE = re.compile(r'(^|/)\\\*\\\*$') +else: + MATCH_STAR_STAR_RE = re.compile(r'(^|\\\/)\\\*\\\*\\\/') + MATCH_STAR_STAR_END_RE = re.compile(r'(^|\\\/)\\\*\\\*$') def match(path, pattern): @@ -121,8 +128,8 @@ def match(path, pattern): return True if pattern not in re_cache: p = re.escape(pattern) - p = re.sub(r'(^|\\\/)\\\*\\\*\\\/', r'\1(?:.+/)?', p) - p = re.sub(r'(^|\\\/)\\\*\\\*$', r'(?:\1.+)?', p) + p = MATCH_STAR_STAR_RE.sub(r'\1(?:.+/)?', p) + p = MATCH_STAR_STAR_END_RE.sub(r'(?:\1.+)?', p) p = p.replace(r'\*', '[^/]*') + '(?:/.*)?$' re_cache[pattern] = re.compile(p) return re_cache[pattern].match(path) is not None