Show More
@@ -1,474 +1,474 b'' | |||
|
1 | 1 | # dagparser.py - parser and generator for concise description of DAGs |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2010 Peter Arrenbrecht <peter@arrenbrecht.ch> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import re, string |
|
9 | 9 | import util |
|
10 | 10 | from i18n import _ |
|
11 | 11 | |
|
12 | 12 | def parsedag(desc): |
|
13 | 13 | '''parses a DAG from a concise textual description; generates events |
|
14 | 14 | |
|
15 | 15 | "+n" is a linear run of n nodes based on the current default parent |
|
16 | 16 | "." is a single node based on the current default parent |
|
17 | 17 | "$" resets the default parent to -1 (implied at the start); |
|
18 | 18 | otherwise the default parent is always the last node created |
|
19 | 19 | "<p" sets the default parent to the backref p |
|
20 | 20 | "*p" is a fork at parent p, where p is a backref |
|
21 | 21 | "*p1/p2/.../pn" is a merge of parents p1..pn, where the pi are backrefs |
|
22 | 22 | "/p2/.../pn" is a merge of the preceding node and p2..pn |
|
23 | 23 | ":name" defines a label for the preceding node; labels can be redefined |
|
24 | 24 | "@text" emits an annotation event for text |
|
25 | 25 | "!command" emits an action event for the current node |
|
26 | 26 | "!!my command\n" is like "!", but to the end of the line |
|
27 | 27 | "#...\n" is a comment up to the end of the line |
|
28 | 28 | |
|
29 | 29 | Whitespace between the above elements is ignored. |
|
30 | 30 | |
|
31 | 31 | A backref is either |
|
32 | 32 | * a number n, which references the node curr-n, where curr is the current |
|
33 | 33 | node, or |
|
34 | 34 | * the name of a label you placed earlier using ":name", or |
|
35 | 35 | * empty to denote the default parent. |
|
36 | 36 | |
|
37 | 37 | All string valued-elements are either strictly alphanumeric, or must |
|
38 | 38 | be enclosed in double quotes ("..."), with "\" as escape character. |
|
39 | 39 | |
|
40 | 40 | Generates sequence of |
|
41 | 41 | |
|
42 | 42 | ('n', (id, [parentids])) for node creation |
|
43 | 43 | ('l', (id, labelname)) for labels on nodes |
|
44 | 44 | ('a', text) for annotations |
|
45 | 45 | ('c', command) for actions (!) |
|
46 | 46 | ('C', command) for line actions (!!) |
|
47 | 47 | |
|
48 | 48 | Examples |
|
49 | 49 | -------- |
|
50 | 50 | |
|
51 | 51 | Example of a complex graph (output not shown for brevity): |
|
52 | 52 | |
|
53 | 53 | >>> len(list(parsedag(""" |
|
54 | 54 | ... |
|
55 | 55 | ... +3 # 3 nodes in linear run |
|
56 | 56 | ... :forkhere # a label for the last of the 3 nodes from above |
|
57 | 57 | ... +5 # 5 more nodes on one branch |
|
58 | 58 | ... :mergethis # label again |
|
59 | 59 | ... <forkhere # set default parent to labelled fork node |
|
60 | 60 | ... +10 # 10 more nodes on a parallel branch |
|
61 | 61 | ... @stable # following nodes will be annotated as "stable" |
|
62 | 62 | ... +5 # 5 nodes in stable |
|
63 | 63 | ... !addfile # custom command; could trigger new file in next node |
|
64 | 64 | ... +2 # two more nodes |
|
65 | 65 | ... /mergethis # merge last node with labelled node |
|
66 | 66 | ... +4 # 4 more nodes descending from merge node |
|
67 | 67 | ... |
|
68 | 68 | ... """))) |
|
69 | 69 | 34 |
|
70 | 70 | |
|
71 | 71 | Empty list: |
|
72 | 72 | |
|
73 | 73 | >>> list(parsedag("")) |
|
74 | 74 | [] |
|
75 | 75 | |
|
76 | 76 | A simple linear run: |
|
77 | 77 | |
|
78 | 78 | >>> list(parsedag("+3")) |
|
79 | 79 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))] |
|
80 | 80 | |
|
81 | 81 | Some non-standard ways to define such runs: |
|
82 | 82 | |
|
83 | 83 | >>> list(parsedag("+1+2")) |
|
84 | 84 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))] |
|
85 | 85 | |
|
86 | 86 | >>> list(parsedag("+1*1*")) |
|
87 | 87 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))] |
|
88 | 88 | |
|
89 | 89 | >>> list(parsedag("*")) |
|
90 | 90 | [('n', (0, [-1]))] |
|
91 | 91 | |
|
92 | 92 | >>> list(parsedag("...")) |
|
93 | 93 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))] |
|
94 | 94 | |
|
95 | 95 | A fork and a join, using numeric back references: |
|
96 | 96 | |
|
97 | 97 | >>> list(parsedag("+2*2*/2")) |
|
98 | 98 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))] |
|
99 | 99 | |
|
100 | 100 | >>> list(parsedag("+2<2+1/2")) |
|
101 | 101 | [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))] |
|
102 | 102 | |
|
103 | 103 | Placing a label: |
|
104 | 104 | |
|
105 | 105 | >>> list(parsedag("+1 :mylabel +1")) |
|
106 | 106 | [('n', (0, [-1])), ('l', (0, 'mylabel')), ('n', (1, [0]))] |
|
107 | 107 | |
|
108 | 108 | An empty label (silly, really): |
|
109 | 109 | |
|
110 | 110 | >>> list(parsedag("+1:+1")) |
|
111 | 111 | [('n', (0, [-1])), ('l', (0, '')), ('n', (1, [0]))] |
|
112 | 112 | |
|
113 | 113 | Fork and join, but with labels instead of numeric back references: |
|
114 | 114 | |
|
115 | 115 | >>> list(parsedag("+1:f +1:p2 *f */p2")) |
|
116 | 116 | [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')), |
|
117 | 117 | ('n', (2, [0])), ('n', (3, [2, 1]))] |
|
118 | 118 | |
|
119 | 119 | >>> list(parsedag("+1:f +1:p2 <f +1 /p2")) |
|
120 | 120 | [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')), |
|
121 | 121 | ('n', (2, [0])), ('n', (3, [2, 1]))] |
|
122 | 122 | |
|
123 | 123 | Restarting from the root: |
|
124 | 124 | |
|
125 | 125 | >>> list(parsedag("+1 $ +1")) |
|
126 | 126 | [('n', (0, [-1])), ('n', (1, [-1]))] |
|
127 | 127 | |
|
128 | 128 | Annotations, which are meant to introduce sticky state for subsequent nodes: |
|
129 | 129 | |
|
130 | 130 | >>> list(parsedag("+1 @ann +1")) |
|
131 | 131 | [('n', (0, [-1])), ('a', 'ann'), ('n', (1, [0]))] |
|
132 | 132 | |
|
133 | 133 | >>> list(parsedag('+1 @"my annotation" +1')) |
|
134 | 134 | [('n', (0, [-1])), ('a', 'my annotation'), ('n', (1, [0]))] |
|
135 | 135 | |
|
136 | 136 | Commands, which are meant to operate on the most recently created node: |
|
137 | 137 | |
|
138 | 138 | >>> list(parsedag("+1 !cmd +1")) |
|
139 | 139 | [('n', (0, [-1])), ('c', 'cmd'), ('n', (1, [0]))] |
|
140 | 140 | |
|
141 | 141 | >>> list(parsedag('+1 !"my command" +1')) |
|
142 | 142 | [('n', (0, [-1])), ('c', 'my command'), ('n', (1, [0]))] |
|
143 | 143 | |
|
144 | 144 | >>> list(parsedag('+1 !!my command line\\n +1')) |
|
145 | 145 | [('n', (0, [-1])), ('C', 'my command line'), ('n', (1, [0]))] |
|
146 | 146 | |
|
147 | 147 | Comments, which extend to the end of the line: |
|
148 | 148 | |
|
149 | 149 | >>> list(parsedag('+1 # comment\\n+1')) |
|
150 | 150 | [('n', (0, [-1])), ('n', (1, [0]))] |
|
151 | 151 | |
|
152 | 152 | Error: |
|
153 | 153 | |
|
154 | 154 | >>> try: list(parsedag('+1 bad')) |
|
155 | 155 | ... except Exception, e: print e |
|
156 | 156 | invalid character in dag description: bad... |
|
157 | 157 | |
|
158 | 158 | ''' |
|
159 | 159 | if not desc: |
|
160 | 160 | return |
|
161 | 161 | |
|
162 | 162 | wordchars = string.ascii_letters + string.digits |
|
163 | 163 | |
|
164 | 164 | labels = {} |
|
165 | 165 | p1 = -1 |
|
166 | 166 | r = 0 |
|
167 | 167 | |
|
168 | 168 | def resolve(ref): |
|
169 | 169 | if not ref: |
|
170 | 170 | return p1 |
|
171 | 171 | elif ref[0] in string.digits: |
|
172 | 172 | return r - int(ref) |
|
173 | 173 | else: |
|
174 | 174 | return labels[ref] |
|
175 | 175 | |
|
176 | 176 | chiter = (c for c in desc) |
|
177 | 177 | |
|
178 | 178 | def nextch(): |
|
179 | 179 | try: |
|
180 | 180 | return chiter.next() |
|
181 | 181 | except StopIteration: |
|
182 | 182 | return '\0' |
|
183 | 183 | |
|
184 | 184 | def nextrun(c, allow): |
|
185 | 185 | s = '' |
|
186 | 186 | while c in allow: |
|
187 | 187 | s += c |
|
188 | 188 | c = nextch() |
|
189 | 189 | return c, s |
|
190 | 190 | |
|
191 | 191 | def nextdelimited(c, limit, escape): |
|
192 | 192 | s = '' |
|
193 | 193 | while c != limit: |
|
194 | 194 | if c == escape: |
|
195 | 195 | c = nextch() |
|
196 | 196 | s += c |
|
197 | 197 | c = nextch() |
|
198 | 198 | return nextch(), s |
|
199 | 199 | |
|
200 | 200 | def nextstring(c): |
|
201 | 201 | if c == '"': |
|
202 | 202 | return nextdelimited(nextch(), '"', '\\') |
|
203 | 203 | else: |
|
204 | 204 | return nextrun(c, wordchars) |
|
205 | 205 | |
|
206 | 206 | c = nextch() |
|
207 | 207 | while c != '\0': |
|
208 | 208 | while c in string.whitespace: |
|
209 | 209 | c = nextch() |
|
210 | 210 | if c == '.': |
|
211 | 211 | yield 'n', (r, [p1]) |
|
212 | 212 | p1 = r |
|
213 | 213 | r += 1 |
|
214 | 214 | c = nextch() |
|
215 | 215 | elif c == '+': |
|
216 | 216 | c, digs = nextrun(nextch(), string.digits) |
|
217 | 217 | n = int(digs) |
|
218 | 218 | for i in xrange(0, n): |
|
219 | 219 | yield 'n', (r, [p1]) |
|
220 | 220 | p1 = r |
|
221 | 221 | r += 1 |
|
222 | 222 | elif c == '*' or c == '/': |
|
223 | 223 | if c == '*': |
|
224 | 224 | c = nextch() |
|
225 | 225 | c, pref = nextstring(c) |
|
226 | 226 | prefs = [pref] |
|
227 | 227 | while c == '/': |
|
228 | 228 | c, pref = nextstring(nextch()) |
|
229 | 229 | prefs.append(pref) |
|
230 | 230 | ps = [resolve(ref) for ref in prefs] |
|
231 | 231 | yield 'n', (r, ps) |
|
232 | 232 | p1 = r |
|
233 | 233 | r += 1 |
|
234 | 234 | elif c == '<': |
|
235 | 235 | c, ref = nextstring(nextch()) |
|
236 | 236 | p1 = resolve(ref) |
|
237 | 237 | elif c == ':': |
|
238 | 238 | c, name = nextstring(nextch()) |
|
239 | 239 | labels[name] = p1 |
|
240 | 240 | yield 'l', (p1, name) |
|
241 | 241 | elif c == '@': |
|
242 | 242 | c, text = nextstring(nextch()) |
|
243 | 243 | yield 'a', text |
|
244 | 244 | elif c == '!': |
|
245 | 245 | c = nextch() |
|
246 | 246 | if c == '!': |
|
247 | 247 | cmd = '' |
|
248 | 248 | c = nextch() |
|
249 | 249 | while c not in '\n\r\0': |
|
250 | 250 | cmd += c |
|
251 | 251 | c = nextch() |
|
252 | 252 | yield 'C', cmd |
|
253 | 253 | else: |
|
254 | 254 | c, cmd = nextstring(c) |
|
255 | 255 | yield 'c', cmd |
|
256 | 256 | elif c == '#': |
|
257 | 257 | while c not in '\n\r\0': |
|
258 | 258 | c = nextch() |
|
259 | 259 | elif c == '$': |
|
260 | 260 | p1 = -1 |
|
261 | 261 | c = nextch() |
|
262 | 262 | elif c == '\0': |
|
263 | 263 | return # in case it was preceded by whitespace |
|
264 | 264 | else: |
|
265 | 265 | s = '' |
|
266 | 266 | i = 0 |
|
267 | 267 | while c != '\0' and i < 10: |
|
268 | 268 | s += c |
|
269 | 269 | i += 1 |
|
270 | 270 | c = nextch() |
|
271 | raise util.Abort("invalid character in dag description: %s..." % s) | |
|
271 | raise util.Abort(_("invalid character in dag description: %s...") % s) | |
|
272 | 272 | |
|
273 | 273 | def dagtextlines(events, |
|
274 | 274 | addspaces=True, |
|
275 | 275 | wraplabels=False, |
|
276 | 276 | wrapannotations=False, |
|
277 | 277 | wrapcommands=False, |
|
278 | 278 | wrapnonlinear=False, |
|
279 | 279 | usedots=False, |
|
280 | 280 | maxlinewidth=70): |
|
281 | 281 | '''generates single lines for dagtext()''' |
|
282 | 282 | |
|
283 | 283 | def wrapstring(text): |
|
284 | 284 | if re.match("^[0-9a-z]*$", text): |
|
285 | 285 | return text |
|
286 | 286 | return '"' + text.replace('\\', '\\\\').replace('"', '\"') + '"' |
|
287 | 287 | |
|
288 | 288 | def gen(): |
|
289 | 289 | labels = {} |
|
290 | 290 | run = 0 |
|
291 | 291 | wantr = 0 |
|
292 | 292 | needroot = False |
|
293 | 293 | for kind, data in events: |
|
294 | 294 | if kind == 'n': |
|
295 | 295 | r, ps = data |
|
296 | 296 | |
|
297 | 297 | # sanity check |
|
298 | 298 | if r != wantr: |
|
299 |
raise util.Abort(" |
|
|
299 | raise util.Abort(_("expected id %i, got %i") % (wantr, r)) | |
|
300 | 300 | if not ps: |
|
301 | 301 | ps = [-1] |
|
302 | 302 | else: |
|
303 | 303 | for p in ps: |
|
304 | 304 | if p >= r: |
|
305 |
raise util.Abort(" |
|
|
306 | "current id %i" % (p, r)) | |
|
305 | raise util.Abort(_("parent id %i is larger than " | |
|
306 | "current id %i") % (p, r)) | |
|
307 | 307 | wantr += 1 |
|
308 | 308 | |
|
309 | 309 | # new root? |
|
310 | 310 | p1 = r - 1 |
|
311 | 311 | if len(ps) == 1 and ps[0] == -1: |
|
312 | 312 | if needroot: |
|
313 | 313 | if run: |
|
314 | 314 | yield '+' + str(run) |
|
315 | 315 | run = 0 |
|
316 | 316 | if wrapnonlinear: |
|
317 | 317 | yield '\n' |
|
318 | 318 | yield '$' |
|
319 | 319 | p1 = -1 |
|
320 | 320 | else: |
|
321 | 321 | needroot = True |
|
322 | 322 | if len(ps) == 1 and ps[0] == p1: |
|
323 | 323 | if usedots: |
|
324 | 324 | yield "." |
|
325 | 325 | else: |
|
326 | 326 | run += 1 |
|
327 | 327 | else: |
|
328 | 328 | if run: |
|
329 | 329 | yield '+' + str(run) |
|
330 | 330 | run = 0 |
|
331 | 331 | if wrapnonlinear: |
|
332 | 332 | yield '\n' |
|
333 | 333 | prefs = [] |
|
334 | 334 | for p in ps: |
|
335 | 335 | if p == p1: |
|
336 | 336 | prefs.append('') |
|
337 | 337 | elif p in labels: |
|
338 | 338 | prefs.append(labels[p]) |
|
339 | 339 | else: |
|
340 | 340 | prefs.append(str(r - p)) |
|
341 | 341 | yield '*' + '/'.join(prefs) |
|
342 | 342 | else: |
|
343 | 343 | if run: |
|
344 | 344 | yield '+' + str(run) |
|
345 | 345 | run = 0 |
|
346 | 346 | if kind == 'l': |
|
347 | 347 | rid, name = data |
|
348 | 348 | labels[rid] = name |
|
349 | 349 | yield ':' + name |
|
350 | 350 | if wraplabels: |
|
351 | 351 | yield '\n' |
|
352 | 352 | elif kind == 'c': |
|
353 | 353 | yield '!' + wrapstring(data) |
|
354 | 354 | if wrapcommands: |
|
355 | 355 | yield '\n' |
|
356 | 356 | elif kind == 'C': |
|
357 | 357 | yield '!!' + data |
|
358 | 358 | yield '\n' |
|
359 | 359 | elif kind == 'a': |
|
360 | 360 | if wrapannotations: |
|
361 | 361 | yield '\n' |
|
362 | 362 | yield '@' + wrapstring(data) |
|
363 | 363 | elif kind == '#': |
|
364 | 364 | yield '#' + data |
|
365 | 365 | yield '\n' |
|
366 | 366 | else: |
|
367 | 367 | raise util.Abort(_("invalid event type in dag: %s") |
|
368 | 368 | % str((type, data))) |
|
369 | 369 | if run: |
|
370 | 370 | yield '+' + str(run) |
|
371 | 371 | |
|
372 | 372 | line = '' |
|
373 | 373 | for part in gen(): |
|
374 | 374 | if part == '\n': |
|
375 | 375 | if line: |
|
376 | 376 | yield line |
|
377 | 377 | line = '' |
|
378 | 378 | else: |
|
379 | 379 | if len(line) + len(part) >= maxlinewidth: |
|
380 | 380 | yield line |
|
381 | 381 | line = '' |
|
382 | 382 | elif addspaces and line and part != '.': |
|
383 | 383 | line += ' ' |
|
384 | 384 | line += part |
|
385 | 385 | if line: |
|
386 | 386 | yield line |
|
387 | 387 | |
|
388 | 388 | def dagtext(dag, |
|
389 | 389 | addspaces=True, |
|
390 | 390 | wraplabels=False, |
|
391 | 391 | wrapannotations=False, |
|
392 | 392 | wrapcommands=False, |
|
393 | 393 | wrapnonlinear=False, |
|
394 | 394 | usedots=False, |
|
395 | 395 | maxlinewidth=70): |
|
396 | 396 | '''generates lines of a textual representation for a dag event stream |
|
397 | 397 | |
|
398 | 398 | events should generate what parsedag() does, so: |
|
399 | 399 | |
|
400 | 400 | ('n', (id, [parentids])) for node creation |
|
401 | 401 | ('l', (id, labelname)) for labels on nodes |
|
402 | 402 | ('a', text) for annotations |
|
403 | 403 | ('c', text) for commands |
|
404 | 404 | ('C', text) for line commands ('!!') |
|
405 | 405 | ('#', text) for comment lines |
|
406 | 406 | |
|
407 | 407 | Parent nodes must come before child nodes. |
|
408 | 408 | |
|
409 | 409 | Examples |
|
410 | 410 | -------- |
|
411 | 411 | |
|
412 | 412 | Linear run: |
|
413 | 413 | |
|
414 | 414 | >>> dagtext([('n', (0, [-1])), ('n', (1, [0]))]) |
|
415 | 415 | '+2' |
|
416 | 416 | |
|
417 | 417 | Two roots: |
|
418 | 418 | |
|
419 | 419 | >>> dagtext([('n', (0, [-1])), ('n', (1, [-1]))]) |
|
420 | 420 | '+1 $ +1' |
|
421 | 421 | |
|
422 | 422 | Fork and join: |
|
423 | 423 | |
|
424 | 424 | >>> dagtext([('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), |
|
425 | 425 | ... ('n', (3, [2, 1]))]) |
|
426 | 426 | '+2 *2 */2' |
|
427 | 427 | |
|
428 | 428 | Fork and join with labels: |
|
429 | 429 | |
|
430 | 430 | >>> dagtext([('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), |
|
431 | 431 | ... ('l', (1, 'p2')), ('n', (2, [0])), ('n', (3, [2, 1]))]) |
|
432 | 432 | '+1 :f +1 :p2 *f */p2' |
|
433 | 433 | |
|
434 | 434 | Annotations: |
|
435 | 435 | |
|
436 | 436 | >>> dagtext([('n', (0, [-1])), ('a', 'ann'), ('n', (1, [0]))]) |
|
437 | 437 | '+1 @ann +1' |
|
438 | 438 | |
|
439 | 439 | >>> dagtext([('n', (0, [-1])), ('a', 'my annotation'), ('n', (1, [0]))]) |
|
440 | 440 | '+1 @"my annotation" +1' |
|
441 | 441 | |
|
442 | 442 | Commands: |
|
443 | 443 | |
|
444 | 444 | >>> dagtext([('n', (0, [-1])), ('c', 'cmd'), ('n', (1, [0]))]) |
|
445 | 445 | '+1 !cmd +1' |
|
446 | 446 | |
|
447 | 447 | >>> dagtext([('n', (0, [-1])), ('c', 'my command'), ('n', (1, [0]))]) |
|
448 | 448 | '+1 !"my command" +1' |
|
449 | 449 | |
|
450 | 450 | >>> dagtext([('n', (0, [-1])), ('C', 'my command line'), ('n', (1, [0]))]) |
|
451 | 451 | '+1 !!my command line\\n+1' |
|
452 | 452 | |
|
453 | 453 | Comments: |
|
454 | 454 | |
|
455 | 455 | >>> dagtext([('n', (0, [-1])), ('#', ' comment'), ('n', (1, [0]))]) |
|
456 | 456 | '+1 # comment\\n+1' |
|
457 | 457 | |
|
458 | 458 | >>> dagtext([]) |
|
459 | 459 | '' |
|
460 | 460 | |
|
461 | 461 | Combining parsedag and dagtext: |
|
462 | 462 | |
|
463 | 463 | >>> dagtext(parsedag('+1 :f +1 :p2 *f */p2')) |
|
464 | 464 | '+1 :f +1 :p2 *f */p2' |
|
465 | 465 | |
|
466 | 466 | ''' |
|
467 | 467 | return "\n".join(dagtextlines(dag, |
|
468 | 468 | addspaces, |
|
469 | 469 | wraplabels, |
|
470 | 470 | wrapannotations, |
|
471 | 471 | wrapcommands, |
|
472 | 472 | wrapnonlinear, |
|
473 | 473 | usedots, |
|
474 | 474 | maxlinewidth)) |
@@ -1,250 +1,251 b'' | |||
|
1 | 1 | # match.py - filename matching |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import re |
|
9 | 9 | import util |
|
10 | from i18n import _ | |
|
10 | 11 | |
|
11 | 12 | class match(object): |
|
12 | 13 | def __init__(self, root, cwd, patterns, include=[], exclude=[], |
|
13 | 14 | default='glob', exact=False): |
|
14 | 15 | """build an object to match a set of file patterns |
|
15 | 16 | |
|
16 | 17 | arguments: |
|
17 | 18 | root - the canonical root of the tree you're matching against |
|
18 | 19 | cwd - the current working directory, if relevant |
|
19 | 20 | patterns - patterns to find |
|
20 | 21 | include - patterns to include |
|
21 | 22 | exclude - patterns to exclude |
|
22 | 23 | default - if a pattern in names has no explicit type, assume this one |
|
23 | 24 | exact - patterns are actually literals |
|
24 | 25 | |
|
25 | 26 | a pattern is one of: |
|
26 | 27 | 'glob:<glob>' - a glob relative to cwd |
|
27 | 28 | 're:<regexp>' - a regular expression |
|
28 | 29 | 'path:<path>' - a path relative to canonroot |
|
29 | 30 | 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs) |
|
30 | 31 | 'relpath:<path>' - a path relative to cwd |
|
31 | 32 | 'relre:<regexp>' - a regexp that needn't match the start of a name |
|
32 | 33 | '<something>' - a pattern of the specified default type |
|
33 | 34 | """ |
|
34 | 35 | |
|
35 | 36 | self._root = root |
|
36 | 37 | self._cwd = cwd |
|
37 | 38 | self._files = [] |
|
38 | 39 | self._anypats = bool(include or exclude) |
|
39 | 40 | |
|
40 | 41 | if include: |
|
41 | 42 | im = _buildmatch(_normalize(include, 'glob', root, cwd), '(?:/|$)') |
|
42 | 43 | if exclude: |
|
43 | 44 | em = _buildmatch(_normalize(exclude, 'glob', root, cwd), '(?:/|$)') |
|
44 | 45 | if exact: |
|
45 | 46 | self._files = patterns |
|
46 | 47 | pm = self.exact |
|
47 | 48 | elif patterns: |
|
48 | 49 | pats = _normalize(patterns, default, root, cwd) |
|
49 | 50 | self._files = _roots(pats) |
|
50 | 51 | self._anypats = self._anypats or _anypats(pats) |
|
51 | 52 | pm = _buildmatch(pats, '$') |
|
52 | 53 | |
|
53 | 54 | if patterns or exact: |
|
54 | 55 | if include: |
|
55 | 56 | if exclude: |
|
56 | 57 | m = lambda f: im(f) and not em(f) and pm(f) |
|
57 | 58 | else: |
|
58 | 59 | m = lambda f: im(f) and pm(f) |
|
59 | 60 | else: |
|
60 | 61 | if exclude: |
|
61 | 62 | m = lambda f: not em(f) and pm(f) |
|
62 | 63 | else: |
|
63 | 64 | m = pm |
|
64 | 65 | else: |
|
65 | 66 | if include: |
|
66 | 67 | if exclude: |
|
67 | 68 | m = lambda f: im(f) and not em(f) |
|
68 | 69 | else: |
|
69 | 70 | m = im |
|
70 | 71 | else: |
|
71 | 72 | if exclude: |
|
72 | 73 | m = lambda f: not em(f) |
|
73 | 74 | else: |
|
74 | 75 | m = lambda f: True |
|
75 | 76 | |
|
76 | 77 | self.matchfn = m |
|
77 | 78 | self._fmap = set(self._files) |
|
78 | 79 | |
|
79 | 80 | def __call__(self, fn): |
|
80 | 81 | return self.matchfn(fn) |
|
81 | 82 | def __iter__(self): |
|
82 | 83 | for f in self._files: |
|
83 | 84 | yield f |
|
84 | 85 | def bad(self, f, msg): |
|
85 | 86 | '''callback for each explicit file that can't be |
|
86 | 87 | found/accessed, with an error message |
|
87 | 88 | ''' |
|
88 | 89 | pass |
|
89 | 90 | def dir(self, f): |
|
90 | 91 | pass |
|
91 | 92 | def missing(self, f): |
|
92 | 93 | pass |
|
93 | 94 | def exact(self, f): |
|
94 | 95 | return f in self._fmap |
|
95 | 96 | def rel(self, f): |
|
96 | 97 | return util.pathto(self._root, self._cwd, f) |
|
97 | 98 | def files(self): |
|
98 | 99 | return self._files |
|
99 | 100 | def anypats(self): |
|
100 | 101 | return self._anypats |
|
101 | 102 | |
|
102 | 103 | class exact(match): |
|
103 | 104 | def __init__(self, root, cwd, files): |
|
104 | 105 | match.__init__(self, root, cwd, files, exact = True) |
|
105 | 106 | |
|
106 | 107 | class always(match): |
|
107 | 108 | def __init__(self, root, cwd): |
|
108 | 109 | match.__init__(self, root, cwd, []) |
|
109 | 110 | |
|
110 | 111 | def patkind(pat): |
|
111 | 112 | return _patsplit(pat, None)[0] |
|
112 | 113 | |
|
113 | 114 | def _patsplit(pat, default): |
|
114 | 115 | """Split a string into an optional pattern kind prefix and the |
|
115 | 116 | actual pattern.""" |
|
116 | 117 | if ':' in pat: |
|
117 | 118 | kind, val = pat.split(':', 1) |
|
118 | 119 | if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre'): |
|
119 | 120 | return kind, val |
|
120 | 121 | return default, pat |
|
121 | 122 | |
|
122 | 123 | def _globre(pat): |
|
123 | 124 | "convert a glob pattern into a regexp" |
|
124 | 125 | i, n = 0, len(pat) |
|
125 | 126 | res = '' |
|
126 | 127 | group = 0 |
|
127 | 128 | escape = re.escape |
|
128 | 129 | def peek(): |
|
129 | 130 | return i < n and pat[i] |
|
130 | 131 | while i < n: |
|
131 | 132 | c = pat[i] |
|
132 | 133 | i += 1 |
|
133 | 134 | if c not in '*?[{},\\': |
|
134 | 135 | res += escape(c) |
|
135 | 136 | elif c == '*': |
|
136 | 137 | if peek() == '*': |
|
137 | 138 | i += 1 |
|
138 | 139 | res += '.*' |
|
139 | 140 | else: |
|
140 | 141 | res += '[^/]*' |
|
141 | 142 | elif c == '?': |
|
142 | 143 | res += '.' |
|
143 | 144 | elif c == '[': |
|
144 | 145 | j = i |
|
145 | 146 | if j < n and pat[j] in '!]': |
|
146 | 147 | j += 1 |
|
147 | 148 | while j < n and pat[j] != ']': |
|
148 | 149 | j += 1 |
|
149 | 150 | if j >= n: |
|
150 | 151 | res += '\\[' |
|
151 | 152 | else: |
|
152 | 153 | stuff = pat[i:j].replace('\\','\\\\') |
|
153 | 154 | i = j + 1 |
|
154 | 155 | if stuff[0] == '!': |
|
155 | 156 | stuff = '^' + stuff[1:] |
|
156 | 157 | elif stuff[0] == '^': |
|
157 | 158 | stuff = '\\' + stuff |
|
158 | 159 | res = '%s[%s]' % (res, stuff) |
|
159 | 160 | elif c == '{': |
|
160 | 161 | group += 1 |
|
161 | 162 | res += '(?:' |
|
162 | 163 | elif c == '}' and group: |
|
163 | 164 | res += ')' |
|
164 | 165 | group -= 1 |
|
165 | 166 | elif c == ',' and group: |
|
166 | 167 | res += '|' |
|
167 | 168 | elif c == '\\': |
|
168 | 169 | p = peek() |
|
169 | 170 | if p: |
|
170 | 171 | i += 1 |
|
171 | 172 | res += escape(p) |
|
172 | 173 | else: |
|
173 | 174 | res += escape(c) |
|
174 | 175 | else: |
|
175 | 176 | res += escape(c) |
|
176 | 177 | return res |
|
177 | 178 | |
|
178 | 179 | def _regex(kind, name, tail): |
|
179 | 180 | '''convert a pattern into a regular expression''' |
|
180 | 181 | if not name: |
|
181 | 182 | return '' |
|
182 | 183 | if kind == 're': |
|
183 | 184 | return name |
|
184 | 185 | elif kind == 'path': |
|
185 | 186 | return '^' + re.escape(name) + '(?:/|$)' |
|
186 | 187 | elif kind == 'relglob': |
|
187 | 188 | return '(?:|.*/)' + _globre(name) + tail |
|
188 | 189 | elif kind == 'relpath': |
|
189 | 190 | return re.escape(name) + '(?:/|$)' |
|
190 | 191 | elif kind == 'relre': |
|
191 | 192 | if name.startswith('^'): |
|
192 | 193 | return name |
|
193 | 194 | return '.*' + name |
|
194 | 195 | return _globre(name) + tail |
|
195 | 196 | |
|
196 | 197 | def _buildmatch(pats, tail): |
|
197 | 198 | """build a matching function from a set of patterns""" |
|
198 | 199 | try: |
|
199 | 200 | pat = '(?:%s)' % '|'.join([_regex(k, p, tail) for (k, p) in pats]) |
|
200 | 201 | if len(pat) > 20000: |
|
201 | 202 | raise OverflowError() |
|
202 | 203 | return re.compile(pat).match |
|
203 | 204 | except OverflowError: |
|
204 | 205 | # We're using a Python with a tiny regex engine and we |
|
205 | 206 | # made it explode, so we'll divide the pattern list in two |
|
206 | 207 | # until it works |
|
207 | 208 | l = len(pats) |
|
208 | 209 | if l < 2: |
|
209 | 210 | raise |
|
210 | 211 | a, b = _buildmatch(pats[:l//2], tail), _buildmatch(pats[l//2:], tail) |
|
211 | 212 | return lambda s: a(s) or b(s) |
|
212 | 213 | except re.error: |
|
213 | 214 | for k, p in pats: |
|
214 | 215 | try: |
|
215 | 216 | re.compile('(?:%s)' % _regex(k, p, tail)) |
|
216 | 217 | except re.error: |
|
217 | raise util.Abort("invalid pattern (%s): %s" % (k, p)) | |
|
218 | raise util.Abort("invalid pattern") | |
|
218 | raise util.Abort(_("invalid pattern (%s): %s") % (k, p)) | |
|
219 | raise util.Abort(_("invalid pattern")) | |
|
219 | 220 | |
|
220 | 221 | def _normalize(names, default, root, cwd): |
|
221 | 222 | pats = [] |
|
222 | 223 | for kind, name in [_patsplit(p, default) for p in names]: |
|
223 | 224 | if kind in ('glob', 'relpath'): |
|
224 | 225 | name = util.canonpath(root, cwd, name) |
|
225 | 226 | elif kind in ('relglob', 'path'): |
|
226 | 227 | name = util.normpath(name) |
|
227 | 228 | |
|
228 | 229 | pats.append((kind, name)) |
|
229 | 230 | return pats |
|
230 | 231 | |
|
231 | 232 | def _roots(patterns): |
|
232 | 233 | r = [] |
|
233 | 234 | for kind, name in patterns: |
|
234 | 235 | if kind == 'glob': # find the non-glob prefix |
|
235 | 236 | root = [] |
|
236 | 237 | for p in name.split('/'): |
|
237 | 238 | if '[' in p or '{' in p or '*' in p or '?' in p: |
|
238 | 239 | break |
|
239 | 240 | root.append(p) |
|
240 | 241 | r.append('/'.join(root) or '.') |
|
241 | 242 | elif kind in ('relpath', 'path'): |
|
242 | 243 | r.append(name or '.') |
|
243 | 244 | elif kind == 'relglob': |
|
244 | 245 | r.append('.') |
|
245 | 246 | return r |
|
246 | 247 | |
|
247 | 248 | def _anypats(patterns): |
|
248 | 249 | for kind, name in patterns: |
|
249 | 250 | if kind in ('glob', 're', 'relglob', 'relre'): |
|
250 | 251 | return True |
General Comments 0
You need to be logged in to leave comments.
Login now