]>
Commit | Line | Data |
---|---|---|
90c1cb3f GR |
1 | #!/usr/bin/env python3 |
2 | # Copyright (c) 2020 VMware, Inc. | |
3 | # | |
4 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | # you may not use this file except in compliance with the License. | |
6 | # You may obtain a copy of the License at: | |
7 | # | |
8 | # http://www.apache.org/licenses/LICENSE-2.0 | |
9 | # | |
10 | # Unless required by applicable law or agreed to in writing, software | |
11 | # distributed under the License is distributed on an "AS IS" BASIS, | |
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | # See the License for the specific language governing permissions and | |
14 | # limitations under the License. | |
15 | # Breaks lines read from stdin into groups using blank lines as | |
16 | # group separators, then sorts lines within the groups for | |
17 | # reproducibility. | |
c724bd67 BP |
18 | |
19 | import re | |
20 | import sys | |
21 | ||
22 | ||
23 | def lookup_uuid(uuids, match): | |
24 | return "<%s>" % uuids.setdefault(match.group(0), len(uuids)) | |
25 | ||
26 | ||
27 | int_re = re.compile(r'\d+') | |
28 | ||
29 | ||
30 | def sort_set(match): | |
31 | s = match.group(0) | |
32 | uuids = sorted([int(x) for x in int_re.findall(s)]) | |
33 | return '["set",[' + ','.join('["uuid","<%s>"]' % x for x in uuids) + ']]' | |
34 | ||
35 | ||
36 | u = '[0-9a-fA-F]' | |
a529e3cd BP |
37 | uuid_re = re.compile(r'%s{8}(?<!ffffffff)-%s{4}-%s{4}-%s{4}-%s{12}' |
38 | % ((u,) * 5)) | |
c724bd67 BP |
39 | set_re = re.compile(r'(\["set",\[(,?\["uuid","<\d+>"\])+\]\])') |
40 | ||
41 | ||
42 | def filter_uuids(src, dst): | |
43 | uuids = {} | |
44 | ||
45 | def lf(match): | |
46 | return lookup_uuid(uuids, match) | |
47 | ||
48 | while True: | |
49 | line = src.readline() | |
50 | if not line: | |
51 | break | |
52 | line = uuid_re.sub(lf, line) | |
53 | ||
54 | # Sort sets like this: | |
55 | # [["uuid","<1>"],["uuid","<0>"]] | |
56 | # to look like this: | |
57 | # [["uuid","<0>"],["uuid","<1>"]] | |
58 | line = set_re.sub(sort_set, line) | |
59 | dst.write(line) | |
60 | ||
61 | ||
62 | if __name__ == '__main__': | |
a529e3cd BP |
63 | if '--help' in sys.argv: |
64 | print("""\ | |
65 | uuidfilt, for filtering UUIDs into numbered markers | |
66 | usage: %s [INPUT..] > OUTPUT | |
67 | or %s < INPUT > OUTPUT | |
68 | ||
69 | Reads each INPUT, locates UUIDs in the standard textual form, and | |
70 | converts them into numbered markers <0>, <1>, ..., <n>, where <0> | |
71 | stands in for each instance of the first unique UUID found, <1> for | |
72 | each instance of the second, and so on. | |
73 | ||
74 | UUIDs that begin with ffffffff are not converted to markers. | |
75 | """) | |
76 | elif len(sys.argv) > 1: | |
c724bd67 BP |
77 | for src in sys.argv[1:]: |
78 | filter_uuids(open(src), sys.stdout) | |
79 | else: | |
80 | filter_uuids(sys.stdin, sys.stdout) |