|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Semantic diff of two update-graph YAML files. |
| 4 | +
|
| 5 | +Treats the `channels` list as an unordered set keyed by (name, datastore metadata). |
| 6 | +Reports per-channel differences in nodes and edges. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 diff-update-graphs.py <old-graph.yaml> <new-graph.yaml> |
| 10 | + python3 diff-update-graphs.py # defaults to old-proposed-update-graph.yaml vs proposed-update-graph.yaml |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +def load_graph(path): |
| 18 | + with open(path) as f: |
| 19 | + return yaml.safe_load(f) |
| 20 | + |
| 21 | + |
| 22 | +def index_channels(graph): |
| 23 | + idx = {} |
| 24 | + for ch in graph.get("channels", []): |
| 25 | + key = (ch["name"], ch.get("metadata", {}).get("datastore", "")) |
| 26 | + idx[key] = ch |
| 27 | + return idx |
| 28 | + |
| 29 | + |
| 30 | +def semver_sort_key(s): |
| 31 | + # Simple sort key: split on dots and dashes, numeric parts as ints |
| 32 | + import re |
| 33 | + return [int(p) if p.isdigit() else p for p in re.split(r"[.\-]", s.lstrip("v"))] |
| 34 | + |
| 35 | + |
| 36 | +def diff_graphs(old_path, new_path): |
| 37 | + old = load_graph(old_path) |
| 38 | + new = load_graph(new_path) |
| 39 | + |
| 40 | + old_ch = index_channels(old) |
| 41 | + new_ch = index_channels(new) |
| 42 | + |
| 43 | + old_keys = set(old_ch.keys()) |
| 44 | + new_keys = set(new_ch.keys()) |
| 45 | + |
| 46 | + print(f"Comparing:\n OLD: {old_path}\n NEW: {new_path}\n") |
| 47 | + |
| 48 | + only_old = sorted(old_keys - new_keys) |
| 49 | + only_new = sorted(new_keys - old_keys) |
| 50 | + |
| 51 | + if only_old: |
| 52 | + print("=== Channels only in OLD ===") |
| 53 | + for k in only_old: |
| 54 | + print(f" {k}") |
| 55 | + print() |
| 56 | + |
| 57 | + if only_new: |
| 58 | + print("=== Channels only in NEW ===") |
| 59 | + for k in only_new: |
| 60 | + print(f" {k}") |
| 61 | + print() |
| 62 | + |
| 63 | + print("=== Per-channel comparison ===") |
| 64 | + any_diff = False |
| 65 | + for key in sorted(old_keys & new_keys): |
| 66 | + oc = old_ch[key] |
| 67 | + nc = new_ch[key] |
| 68 | + |
| 69 | + old_nodes = {n["id"]: n for n in oc.get("nodes", [])} |
| 70 | + new_nodes = {n["id"]: n for n in nc.get("nodes", [])} |
| 71 | + |
| 72 | + only_old_nodes = sorted(set(old_nodes) - set(new_nodes), key=semver_sort_key) |
| 73 | + only_new_nodes = sorted(set(new_nodes) - set(old_nodes), key=semver_sort_key) |
| 74 | + |
| 75 | + old_edges = {k: set(v) for k, v in (oc.get("edges") or {}).items()} |
| 76 | + new_edges = {k: set(v) for k, v in (nc.get("edges") or {}).items()} |
| 77 | + |
| 78 | + all_from_nodes = set(old_edges) | set(new_edges) |
| 79 | + edge_diffs = {} |
| 80 | + for fn in all_from_nodes: |
| 81 | + oe = old_edges.get(fn, set()) |
| 82 | + ne = new_edges.get(fn, set()) |
| 83 | + only_old_e = sorted(oe - ne, key=semver_sort_key) |
| 84 | + only_new_e = sorted(ne - oe, key=semver_sort_key) |
| 85 | + if only_old_e or only_new_e: |
| 86 | + edge_diffs[fn] = (only_old_e, only_new_e) |
| 87 | + |
| 88 | + # Node field-level diffs |
| 89 | + field_diffs = {} |
| 90 | + for nid in sorted(set(old_nodes) & set(new_nodes), key=semver_sort_key): |
| 91 | + on = old_nodes[nid] |
| 92 | + nn = new_nodes[nid] |
| 93 | + diffs = {} |
| 94 | + for f in set(on) | set(nn): |
| 95 | + if on.get(f) != nn.get(f): |
| 96 | + diffs[f] = (on.get(f), nn.get(f)) |
| 97 | + if diffs: |
| 98 | + field_diffs[nid] = diffs |
| 99 | + |
| 100 | + channel_label = f"{key[1]}/{key[0]}" |
| 101 | + if not only_old_nodes and not only_new_nodes and not edge_diffs and not field_diffs: |
| 102 | + print(f"\n {channel_label}: IDENTICAL") |
| 103 | + else: |
| 104 | + any_diff = True |
| 105 | + print(f"\n {channel_label}: DIFFERS") |
| 106 | + if only_old_nodes: |
| 107 | + print(f" Nodes only in OLD: {only_old_nodes}") |
| 108 | + if only_new_nodes: |
| 109 | + print(f" Nodes only in NEW: {only_new_nodes}") |
| 110 | + for fn in sorted(edge_diffs, key=semver_sort_key): |
| 111 | + only_old_e, only_new_e = edge_diffs[fn] |
| 112 | + if only_old_e: |
| 113 | + print(f" Edge {fn} -> REMOVED targets: {only_old_e}") |
| 114 | + if only_new_e: |
| 115 | + print(f" Edge {fn} -> ADDED targets: {only_new_e}") |
| 116 | + for nid, diffs in sorted(field_diffs.items(), key=lambda x: semver_sort_key(x[0])): |
| 117 | + print(f" Node {nid} field diffs: {diffs}") |
| 118 | + |
| 119 | + if not any_diff and not only_old and not only_new: |
| 120 | + print("\nGraphs are SEMANTICALLY EQUIVALENT (order-agnostic).") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + if len(sys.argv) == 3: |
| 125 | + old_path, new_path = sys.argv[1], sys.argv[2] |
| 126 | + elif len(sys.argv) == 1: |
| 127 | + old_path = "old-proposed-update-graph.yaml" |
| 128 | + new_path = "proposed-update-graph.yaml" |
| 129 | + else: |
| 130 | + print("Usage: diff-update-graphs.py [<old.yaml> <new.yaml>]") |
| 131 | + sys.exit(1) |
| 132 | + |
| 133 | + diff_graphs(old_path, new_path) |
0 commit comments