You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
birdvisu/snippets/test_diff.py

48 lines
1.2 KiB
Python

#!/usr/bin/env python3
from birdvisu import maps
from pprint import pprint
import sys
import subprocess
from io import StringIO
with open('current.ospf') as actual:
act_topo = maps.Topology.from_ospffile(actual)
with open('reference.ospf') as reference:
ref_topo = maps.Topology.from_ospffile(reference)
diff = maps.TopologyDifference(act_topo, ref_topo)
diff.compare()
def visualize_graphviz(diff):
graph = StringIO()
# Prologue
graph.write('strict graph "OSPF Visualization" {\n')
for r, n, m in set(diff.reference.links + diff.actual.links):
graph.write(f'"{r}" -- "{n}" [label="{m}"];\n')
for r in diff.routers_missing | diff.networks_missing:
graph.write(f'"{r}" [color=red];\n')
for r in diff.routers_extra | diff.networks_extra:
graph.write(f'"{r}" [color=green];\n')
for r, n, _ in diff.links_missing:
graph.write(f'"{r}" -- "{n}" [color=red];\n')
for r, n, _ in diff.links_extra:
graph.write(f'"{r}" -- "{n}" [color=green];\n')
# Epilogue:
graph.write('}\n')
# Show:
gv = subprocess.Popen(['dot', '-Tgtk', '-K'+(sys.argv[2] if len(sys.argv)>=3 else 'fdp')], text=True, stdin=subprocess.PIPE)
gv.communicate(graph.getvalue())
gv.wait()
if len(sys.argv) == 1:
pprint(vars(diff))
elif 'dot' in sys.argv:
visualize_graphviz(diff)