##// END OF EJS Templates
minifileset: fix on Python 3...
Augie Fackler -
r37893:9c98cb30 default
parent child Browse files
Show More
@@ -1,85 +1,87 b''
1 1 # minifileset.py - a simple language to select files
2 2 #
3 3 # Copyright 2017 Facebook, Inc.
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 from __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from . import (
12 12 error,
13 13 fileset,
14 pycompat,
14 15 )
15 16
16 17 def _compile(tree):
17 18 if not tree:
18 19 raise error.ParseError(_("missing argument"))
19 20 op = tree[0]
20 21 if op in {'symbol', 'string', 'kindpat'}:
21 22 name = fileset.getpattern(tree, {'path'}, _('invalid file pattern'))
22 23 if name.startswith('**'): # file extension test, ex. "**.tar.gz"
23 24 ext = name[2:]
24 for c in ext:
25 for c in pycompat.bytestr(ext):
25 26 if c in '*{}[]?/\\':
26 27 raise error.ParseError(_('reserved character: %s') % c)
27 28 return lambda n, s: n.endswith(ext)
28 29 elif name.startswith('path:'): # directory or full path test
29 30 p = name[5:] # prefix
30 31 pl = len(p)
31 f = lambda n, s: n.startswith(p) and (len(n) == pl or n[pl] == '/')
32 f = lambda n, s: n.startswith(p) and (len(n) == pl
33 or n[pl:pl + 1] == '/')
32 34 return f
33 35 raise error.ParseError(_("unsupported file pattern: %s") % name,
34 36 hint=_('paths must be prefixed with "path:"'))
35 37 elif op == 'or':
36 38 func1 = _compile(tree[1])
37 39 func2 = _compile(tree[2])
38 40 return lambda n, s: func1(n, s) or func2(n, s)
39 41 elif op == 'and':
40 42 func1 = _compile(tree[1])
41 43 func2 = _compile(tree[2])
42 44 return lambda n, s: func1(n, s) and func2(n, s)
43 45 elif op == 'not':
44 46 return lambda n, s: not _compile(tree[1])(n, s)
45 47 elif op == 'group':
46 48 return _compile(tree[1])
47 49 elif op == 'func':
48 50 symbols = {
49 51 'all': lambda n, s: True,
50 52 'none': lambda n, s: False,
51 53 'size': lambda n, s: fileset.sizematcher(tree[2])(s),
52 54 }
53 55
54 56 name = fileset.getsymbol(tree[1])
55 57 if name in symbols:
56 58 return symbols[name]
57 59
58 60 raise error.UnknownIdentifier(name, symbols.keys())
59 61 elif op == 'minus': # equivalent to 'x and not y'
60 62 func1 = _compile(tree[1])
61 63 func2 = _compile(tree[2])
62 64 return lambda n, s: func1(n, s) and not func2(n, s)
63 65 elif op == 'negate':
64 66 raise error.ParseError(_("can't use negate operator in this context"))
65 67 elif op == 'list':
66 68 raise error.ParseError(_("can't use a list in this context"),
67 69 hint=_('see hg help "filesets.x or y"'))
68 70 raise error.ProgrammingError('illegal tree: %r' % (tree,))
69 71
70 72 def compile(text):
71 73 """generate a function (path, size) -> bool from filter specification.
72 74
73 75 "text" could contain the operators defined by the fileset language for
74 76 common logic operations, and parenthesis for grouping. The supported path
75 77 tests are '**.extname' for file extension test, and '"path:dir/subdir"'
76 78 for prefix test. The ``size()`` predicate is borrowed from filesets to test
77 79 file size. The predicates ``all()`` and ``none()`` are also supported.
78 80
79 81 '(**.php & size(">10MB")) | **.zip | (path:bin & !path:bin/README)' for
80 82 example, will catch all php files whose size is greater than 10 MB, all
81 83 files whose name ends with ".zip", and all files under "bin" in the repo
82 84 root except for "bin/README".
83 85 """
84 86 tree = fileset.parse(text)
85 87 return _compile(tree)
General Comments 0
You need to be logged in to leave comments. Login now