Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/fnmatch.py @ 308:652870b20f9e
treesum: Implement --accept-treesum: trust a treesum-file for a directory checksum
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 08 Mar 2025 04:49:06 +0100 |
| parents | 6c212e407524 |
| children | f5f54b9c3552 |
comparison
equal
deleted
inserted
replaced
| 307:64df94bf4659 | 308:652870b20f9e |
|---|---|
| 13 __all__ = ["FnMatcher"] | 13 __all__ = ["FnMatcher"] |
| 14 | 14 |
| 15 | 15 |
| 16 import re | 16 import re |
| 17 | 17 |
| 18 from . import PY2 | |
| 18 from . import glob | 19 from . import glob |
| 19 | 20 |
| 20 | 21 |
| 21 HELP_DESCRIPTION = """\ | 22 HELP_DESCRIPTION = """\ |
| 22 PATTERNs | 23 PATTERNs |
| 93 "re": re_factory, | 94 "re": re_factory, |
| 94 "path": path_factory, | 95 "path": path_factory, |
| 95 "fullpath": fullpath_factory, | 96 "fullpath": fullpath_factory, |
| 96 } | 97 } |
| 97 | 98 |
| 98 VISIT_DEFAULT = True # Overall default value for visiting | |
| 99 | |
| 100 def __init__(self, matchers): | 99 def __init__(self, matchers): |
| 101 super(FnMatcher, self).__init__() | 100 super(FnMatcher, self).__init__() |
| 102 self._matchers = matchers | 101 self._matchers = matchers |
| 103 | 102 |
| 104 @classmethod | 103 @classmethod |
| 105 def build_from_commandline_patterns(klass, filter_definitions): | 104 def build_from_commandline_patterns(klass, filter_definitions): |
| 106 matchers = [] | 105 matchers = [] |
| 107 if filter_definitions: | 106 if filter_definitions: |
| 108 for action, kpattern in filter_definitions: | 107 for action, kpattern in filter_definitions: |
| 108 assert action in ("include", "exclude", "accept-treesum") | |
| 109 kind, sep, pattern = kpattern.partition(':') | 109 kind, sep, pattern = kpattern.partition(':') |
| 110 if not sep: | 110 if not sep: |
| 111 # use the default | 111 # use the default |
| 112 kind = "glob" | 112 kind = "glob" |
| 113 pattern = kpattern | 113 pattern = kpattern |
| 115 if not factory: | 115 if not factory: |
| 116 raise RuntimeError("unknown pattern kind: {}".format(kind)) | 116 raise RuntimeError("unknown pattern kind: {}".format(kind)) |
| 117 matchers.append((action, kind, factory(pattern), pattern)) | 117 matchers.append((action, kind, factory(pattern), pattern)) |
| 118 return klass(matchers) | 118 return klass(matchers) |
| 119 | 119 |
| 120 def shall_visit(self, fn, default=None): | 120 def shall_visit(self, fn, default=True): |
| 121 visit = default if default is not None else self.VISIT_DEFAULT | 121 visit = default |
| 122 for action, kind, matcher, orig_pattern in self._matchers: | 122 for action, kind, matcher, orig_pattern in self._matchers: |
| 123 res = matcher(fn) | 123 if matcher(fn): |
| 124 if res: | |
| 125 if action == "include": | 124 if action == "include": |
| 126 visit = True | 125 visit = True |
| 127 elif action == "exclude": | 126 elif action in ("exclude", "accept-treesum"): |
| 128 visit = False | 127 visit = False |
| 129 else: | 128 else: |
| 130 raise RuntimeError("unknown action: {}".format(action)) | 129 raise RuntimeError("unknown action: {}".format(action)) |
| 131 return visit | 130 return visit |
| 132 | 131 |
| 132 def shall_accept_treesum(self, fn, default=False): | |
| 133 accept = default | |
| 134 for action, kind, matcher, orig_pattern in self._matchers: | |
| 135 if action == "accept-treesum": | |
| 136 if matcher(fn): | |
| 137 accept = True | |
| 138 elif action in ("include", "exclude"): | |
| 139 pass | |
| 140 else: | |
| 141 raise RuntimeError("unknown action: {}".format(action)) | |
| 142 return accept | |
| 143 | |
| 133 def definitions(self): | 144 def definitions(self): |
| 134 for action, kind, matcher, orig_pattern in self._matchers: | 145 for action, kind, matcher, orig_pattern in self._matchers: |
| 135 yield (action, kind, orig_pattern) | 146 yield (action, kind, orig_pattern) |
| 147 | |
| 148 def __bool__(self): | |
| 149 return bool(self._matchers) | |
| 150 | |
| 151 if PY2: | |
| 152 __nonzero__ = __bool__ |
