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', '-Kfdp'], 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)