瀏覽代碼

Simplify the ExcludePattern logic further, and explain.

Dan Christensen 11 年之前
父節點
當前提交
b76a40c987
共有 1 個文件被更改,包括 12 次插入9 次删除
  1. 12 9
      attic/helpers.py

+ 12 - 9
attic/helpers.py

@@ -158,6 +158,12 @@ def exclude_path(path, patterns):
     return False
 
 
+# For both IncludePattern and ExcludePattern, we require that
+# the pattern either match the whole path or an initial segment
+# of the path up to but not including a path separator.  To
+# unify the two cases, we add a path separator to the end of
+# the path before matching.
+
 class IncludePattern:
     """Literal files or directories listed on the command line
     for some operations (e.g. extract, but create).
@@ -179,19 +185,16 @@ class ExcludePattern(IncludePattern):
     exclude the contents of a directory, but not the directory itself.
     """
     def __init__(self, pattern):
-        self.pattern = pattern
-        # fnmatch and re.match both cache compiled regular expressions.
-        # Nevertheless, this is about 10 times faster.
         if pattern.endswith(os.path.sep):
-            regex = translate(pattern+'*')
+            self.pattern = pattern+'*'+os.path.sep
         else:
-            regex1 = translate(pattern)
-            regex2 = translate(pattern+os.path.sep+'*')
-            regex = '(' + regex1 + ')|(' + regex2 + ')'
-        self.regobj = re.compile(regex)
+            self.pattern = pattern+os.path.sep+'*'
+        # fnmatch and re.match both cache compiled regular expressions.
+        # Nevertheless, this is about 10 times faster.
+        self.regex = re.compile(translate(self.pattern))
 
     def match(self, path):
-        return self.regobj.match(path) is not None
+        return self.regex.match(path+os.path.sep) is not None
 
     def __repr__(self):
         return '%s(%s)' % (type(self), self.pattern)