]> git.proxmox.com Git - mirror_ovs.git/blame - tests/ovsdb-monitor-sort.py
ofproto-dpif: Expose datapath capability to ovsdb.
[mirror_ovs.git] / tests / ovsdb-monitor-sort.py
CommitLineData
d4cd4d30
BP
1#! /usr/bin/env python
2
3# Breaks lines read from stdin into groups using blank lines as
4# group separators, then sorts lines within the groups for
5# reproducibility.
6
7import re
8import sys
9
10
11# This is copied out of the Python Sorting HOWTO at
12# https://docs.python.org/3/howto/sorting.html#sortinghowto
13def cmp_to_key(mycmp):
14 'Convert a cmp= function into a key= function'
15 class K(object):
16
17 def __init__(self, obj, *args):
18 self.obj = obj
19
20 def __lt__(self, other):
21 return mycmp(self.obj, other.obj) < 0
22
23 def __gt__(self, other):
24 return mycmp(self.obj, other.obj) > 0
25
26 def __eq__(self, other):
27 return mycmp(self.obj, other.obj) == 0
28
29 def __le__(self, other):
30 return mycmp(self.obj, other.obj) <= 0
31
32 def __ge__(self, other):
33 return mycmp(self.obj, other.obj) >= 0
34
35 def __ne__(self, other):
36 return mycmp(self.obj, other.obj) != 0
37
38 return K
39
40
41u = '[0-9a-fA-F]'
42uuid_re = re.compile(r'%s{8}-%s{4}-%s{4}-%s{4}-%s{12}' % ((u,) * 5))
43
44
45def cmp(a, b):
46 return (a > b) - (a < b)
47
48
49def compare_lines(a, b):
50 if uuid_re.match(a):
51 if uuid_re.match(b):
52 return cmp(a[36:], b[36:])
53 else:
54 return 1
55 elif uuid_re.match(b):
56 return -1
57 else:
58 return cmp(a, b)
59
60
61def output_group(group, dst):
62 for x in sorted(group, key=cmp_to_key(compare_lines)):
63 dst.write(x)
64
65
66def ovsdb_monitor_sort(src, dst):
67 group = []
68 while True:
69 line = src.readline()
70 if not line:
71 break
72 if line.rstrip() == '':
73 output_group(group, dst)
74 group = []
75 dst.write(line)
76 elif line.startswith(',') and group:
77 group[len(group) - 1] += line
78 else:
79 group.append(line)
80 if group:
81 output_group(group, dst)
82
83
84if __name__ == '__main__':
85 ovsdb_monitor_sort(sys.stdin, sys.stdout)