forked from FreifunkVogtland/site-merge-detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-merge-detect.py
executable file
·154 lines (108 loc) · 3.53 KB
/
site-merge-detect.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python3
# -*- coding: utf-8; -*-
import json
import os
import os.path
import sys
def load_whitelisted():
whitelisted_sites = json.load(open('whitelisted_sites.json'))
if type(whitelisted_sites) is not list:
raise Exception('whitelisted_sites.json not a simple list')
for site in whitelisted_sites:
if type(site) is not str:
raise Exception('whitelisted_sites entry not a string')
return whitelisted_sites
def get_nodes(meshviewer):
nodes = {}
for node in meshviewer['nodes']:
node_id = node.get('node_id')
if node_id is None:
continue
site_code = node.get('site_code')
if site_code is None:
continue
nodes[node_id] = site_code
return nodes
def get_nodes_with_invalid_sites(nodes, whitelisted):
invalid = []
if not whitelisted:
return invalid
for node_id in nodes:
if nodes[node_id] not in whitelisted:
invalid.append({
'node_id': node_id,
'site_code': nodes[node_id],
})
return invalid
def print_invalid_site_nodes(invalid_site_nodes):
print("Nodes with invalid site_code found")
print("==================================\n")
for node in invalid_site_nodes:
print(" * {node_id} ({site_code})".format(**node))
print()
def get_intersite_links(nodes, meshviewer):
intersite = []
links = set()
for link in meshviewer['links']:
source_site_code = nodes.get(link['source'])
if source_site_code is None:
continue
target_site_code = nodes.get(link['target'])
if target_site_code is None:
continue
if source_site_code == target_site_code:
continue
if link['source'] < link['target']:
node_id1 = link['source']
site_code1 = source_site_code
node_id2 = link['target']
site_code2 = target_site_code
else:
node_id1 = link['target']
site_code1 = target_site_code
node_id2 = link['source']
site_code2 = source_site_code
linkid = (node_id1, link['type'], node_id2)
if linkid in links:
continue
links.add(linkid)
intersite.append({
'site_code1': site_code1,
'node_id1': node_id1,
'type': link['type'],
'node_id2': node_id2,
'site_code2': site_code2,
})
return intersite
def print_intersite_links(intersite_links):
print("Links with different site_code found")
print("====================================\n")
for link in intersite_links:
fmt = " * {node_id1} ({site_code1})"
fmt += " <--({type})--> "
fmt += "{node_id2} ({site_code2})"
print(fmt.format(**link))
print()
def main():
if len(sys.argv) != 2:
print("./site-merge-detect.py nodes.json")
sys.exit(1)
meshviewer_in = sys.argv[1]
# load
meshviewer = json.load(open(meshviewer_in))
whitelisted = load_whitelisted()
# process
nodes = get_nodes(meshviewer)
invalid_site_nodes = get_nodes_with_invalid_sites(nodes, whitelisted)
intersite_links = get_intersite_links(nodes, meshviewer)
# list errors
exit_code = 0
if invalid_site_nodes:
exit_code = 1
print_invalid_site_nodes(invalid_site_nodes)
if intersite_links:
exit_code = 1
print_intersite_links(intersite_links)
sys.exit(exit_code)
if __name__ == "__main__":
main()