77 res += '|' |
77 res += '|' |
78 else: |
78 else: |
79 res += re.escape(c) |
79 res += re.escape(c) |
80 return head + res + tail |
80 return head + res + tail |
81 |
81 |
|
82 def matcher(cwd, pats, inc, exc, head = ''): |
|
83 def regex(name, tail): |
|
84 '''convert a pattern into a regular expression''' |
|
85 if name.startswith('re:'): |
|
86 return name[3:] |
|
87 elif name.startswith('path:'): |
|
88 return '^' + re.escape(name[5:]) + '$' |
|
89 elif name.startswith('glob:'): |
|
90 return head + globre(name[5:], '', tail) |
|
91 return head + globre(name, '', tail) |
|
92 |
|
93 def under(fn): |
|
94 """check if fn is under our cwd""" |
|
95 return not cwd or fn.startswith(cwdsep) |
|
96 |
|
97 def matchfn(pats, tail): |
|
98 """build a matching function from a set of patterns""" |
|
99 if pats: |
|
100 pat = '(?:%s)' % '|'.join([regex(p, tail) for p in pats]) |
|
101 if cwd: |
|
102 pat = re.escape(cwd + os.sep) + pat |
|
103 return re.compile(pat).match |
|
104 |
|
105 cwdsep = cwd + os.sep |
|
106 patmatch = matchfn(pats, '$') or (lambda fn: True) |
|
107 incmatch = matchfn(inc, '(?:/|$)') or under |
|
108 excmatch = matchfn(exc, '(?:/|$)') or (lambda fn: False) |
|
109 |
|
110 return lambda fn: (incmatch(fn) and not excmatch(fn) and |
|
111 (fn.endswith('/') or patmatch(fn))) |
|
112 |
82 def system(cmd, errprefix=None): |
113 def system(cmd, errprefix=None): |
83 """execute a shell command that must succeed""" |
114 """execute a shell command that must succeed""" |
84 rc = os.system(cmd) |
115 rc = os.system(cmd) |
85 if rc: |
116 if rc: |
86 errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]), |
117 errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]), |