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