-
Notifications
You must be signed in to change notification settings - Fork 4
/
kkcheck.py
92 lines (83 loc) · 2.72 KB
/
kkcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# vim:ts=4:et
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
from cfgnode import *
import sys
import os
from uuid import uuid4
def recurse_tree(path, func):
files = os.listdir(path)
files.sort()
for f in files:
if f[0] in [".", "_"]:
continue
p = os.path.join(path, f)
if os.path.isdir(p):
recurse_tree(p, func)
else:
func(p)
static_by_uuid = {}
def find_statics(path):
if path[-4:].lower() != ".cfg":
return
node = ConfigNode.loadfile(path)
for n in node.nodes:
if n[0].split(':', 1)[0] != 'STATIC':
continue
static = n[1]
pname = static.GetValue("pointername")
instances = static.GetNodes("Instances")
for inst in instances:
group = inst.GetValue("Group")
uuid = inst.GetValue("UUID")
if uuid not in static_by_uuid:
static_by_uuid[uuid] = []
static_by_uuid[uuid].append((group, pname, path))
def genUUID():
while True:
uuid = uuid4().urn[9:]
if uuid not in static_by_uuid:
static_by_uuid[uuid] = None
return uuid
files_to_fix = set()
recurse_tree(".", find_statics)
for u in static_by_uuid:
s = static_by_uuid[u]
if len(s) > 1:
print(f"duplicate uuid {u} on:")
for d in s:
print(" ", d[0], d[1])
files_to_fix.add(d[2])
while files_to_fix:
path = files_to_fix.pop()
node = ConfigNode.loadfile(path)
of = open(path, "wt")
for n in node.nodes:
if n[0].split(':', 1)[0] != 'STATIC':
continue
static = n[1]
pname = static.GetValue("pointername")
instances = static.GetNodes("Instances")
for inst in instances:
group = inst.GetValue("Group")
uuid = genUUID()
print(inst.GetValue("UUID"), uuid)
inst.SetValue("UUID", uuid)
of.write(n[0] + " " + static.ToString())
of.close()