2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-13 14:07:02 +00:00
Files
openvswitch/tests/uuidfilt.py
Ben Pfaff c724bd674f tests: Convert uuidfilt utility from Perl to Python.
Perl is unfashionable and Python is more widely available and understood,
so this commit converts one of the OVS uses of Perl into Python.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Aaron Conole <aconole@redhat.com>
2017-11-26 16:07:55 -08:00

51 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python
import re
import sys
def lookup_uuid(uuids, match):
return "<%s>" % uuids.setdefault(match.group(0), len(uuids))
int_re = re.compile(r'\d+')
def sort_set(match):
s = match.group(0)
uuids = sorted([int(x) for x in int_re.findall(s)])
return '["set",[' + ','.join('["uuid","<%s>"]' % x for x in uuids) + ']]'
u = '[0-9a-fA-F]'
uuid_re = re.compile(r'%s{8}-%s{4}-%s{4}-%s{4}-%s{12}' % ((u,) * 5))
set_re = re.compile(r'(\["set",\[(,?\["uuid","<\d+>"\])+\]\])')
def filter_uuids(src, dst):
uuids = {}
def lf(match):
return lookup_uuid(uuids, match)
while True:
line = src.readline()
if not line:
break
line = uuid_re.sub(lf, line)
# Sort sets like this:
# [["uuid","<1>"],["uuid","<0>"]]
# to look like this:
# [["uuid","<0>"],["uuid","<1>"]]
line = set_re.sub(sort_set, line)
dst.write(line)
if __name__ == '__main__':
if len(sys.argv) > 1:
for src in sys.argv[1:]:
filter_uuids(open(src), sys.stdout)
else:
filter_uuids(sys.stdin, sys.stdout)