]> git.proxmox.com Git - ovs.git/blame - tests/uuidfilt.py
Fix ovs-dpctl-top by removing 3 wrong hunks in py3-compat.patch.
[ovs.git] / tests / uuidfilt.py
CommitLineData
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
19import re
20import sys
21
22
23def lookup_uuid(uuids, match):
24 return "<%s>" % uuids.setdefault(match.group(0), len(uuids))
25
26
27int_re = re.compile(r'\d+')
28
29
30def 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
36u = '[0-9a-fA-F]'
a529e3cd
BP
37uuid_re = re.compile(r'%s{8}(?<!ffffffff)-%s{4}-%s{4}-%s{4}-%s{12}'
38 % ((u,) * 5))
c724bd67
BP
39set_re = re.compile(r'(\["set",\[(,?\["uuid","<\d+>"\])+\]\])')
40
41
42def 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
62if __name__ == '__main__':
a529e3cd
BP
63 if '--help' in sys.argv:
64 print("""\
65uuidfilt, for filtering UUIDs into numbered markers
66usage: %s [INPUT..] > OUTPUT
67 or %s < INPUT > OUTPUT
68
69Reads each INPUT, locates UUIDs in the standard textual form, and
70converts them into numbered markers <0>, <1>, ..., <n>, where <0>
71stands in for each instance of the first unique UUID found, <1> for
72each instance of the second, and so on.
73
74UUIDs 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)