Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / test / diff_kinds.py
diff_kinds.py170 lines · 5.6 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """diff_kinds.py — does this archive carry the kinds the schema promises? |
| 3 | |
| 4 | Decodes a `.mamaps` archive with `tile_build`'s `mamaps_dump` and checks the kind |
| 5 | vocabulary per layer: |
| 6 | |
| 7 | * The archive has 9 layers with `landtype` first (v8 merge of the four wash layers). |
| 8 | * The v8 kinds are present: `orchard`, `vineyard`, `quarry`, `swimming_pool` |
| 9 | (the first three are new ids; `swimming_pool` predates v8 but was never emitted). |
| 10 | * Zero unknown-kind errors: every kind on every feature resolves through the |
| 11 | archive's own dictionary. |
| 12 | |
| 13 | Usage: |
| 14 | ./diff_kinds.py after.mamaps |
| 15 | ./diff_kinds.py before.mamaps after.mamaps (also reports which kinds appeared) |
| 16 | |
| 17 | Exit code 0 when the vocabulary holds, 1 otherwise. |
| 18 | """ |
| 19 | |
| 20 | from __future__ import annotations |
| 21 | |
| 22 | import subprocess |
| 23 | import sys |
| 24 | from pathlib import Path |
| 25 | |
| 26 | ROOT = Path(__file__).resolve().parent.parent |
| 27 | CRATE = ROOT / "tile_build" |
| 28 | |
| 29 | EXPECTED_LAYERS = [ |
| 30 | "landtype", |
| 31 | "roads", |
| 32 | "boundaries", |
| 33 | "buildings", |
| 34 | "places", |
| 35 | "poi", |
| 36 | "transit", |
| 37 | "traffic", |
| 38 | "junction", |
| 39 | ] |
| 40 | |
| 41 | # v8: what the wash used to drop (`swimming_pool` already had an id — paint only). |
| 42 | EXPECTED_NEW_KINDS = ["orchard", "vineyard", "quarry", "swimming_pool"] |
| 43 | |
| 44 | |
| 45 | def dump_command(explicit: Path | None) -> list[str]: |
| 46 | if explicit: |
| 47 | return [str(explicit)] |
| 48 | for name in ("mamaps_dump", "mamaps_dump.exe"): |
| 49 | built = CRATE / "target" / "release" / name |
| 50 | if built.exists(): |
| 51 | return [str(built)] |
| 52 | return [ |
| 53 | "cargo", |
| 54 | "run", |
| 55 | "--release", |
| 56 | "--quiet", |
| 57 | "--manifest-path", |
| 58 | str(CRATE / "Cargo.toml"), |
| 59 | "--bin", |
| 60 | "mamaps_dump", |
| 61 | "--", |
| 62 | ] |
| 63 | |
| 64 | |
| 65 | def run_dump(cmd: list[str], archive: Path, *extra: str) -> str: |
| 66 | proc = subprocess.run(cmd + [str(archive), *extra], capture_output=True, text=True) |
| 67 | if proc.returncode != 0: |
| 68 | sys.stderr.write(proc.stderr) |
| 69 | raise SystemExit(f"mamaps_dump failed on {archive}") |
| 70 | if proc.stderr.strip(): |
| 71 | sys.stderr.write(proc.stderr) |
| 72 | return proc.stdout |
| 73 | |
| 74 | |
| 75 | def dict_layers(cmd: list[str], archive: Path) -> list[str]: |
| 76 | layers: list[str] = [] |
| 77 | for line in run_dump(cmd, archive, "--mode", "dict").splitlines(): |
| 78 | # `layer\t{id}\t{name}` lines; kinds and details follow their own prefixes. |
| 79 | parts = line.split("\t") |
| 80 | if len(parts) == 3 and parts[0] == "layer": |
| 81 | layers.append(parts[2]) |
| 82 | return layers |
| 83 | |
| 84 | |
| 85 | def dict_kinds(cmd: list[str], archive: Path) -> list[str]: |
| 86 | kinds: list[str] = [] |
| 87 | for line in run_dump(cmd, archive, "--mode", "dict").splitlines(): |
| 88 | parts = line.split("\t") |
| 89 | if len(parts) == 3 and parts[0] == "kind": |
| 90 | kinds.append(parts[2]) |
| 91 | return kinds |
| 92 | |
| 93 | |
| 94 | def summary_kinds(cmd: list[str], archive: Path) -> dict[str, set[str]]: |
| 95 | """layer -> set of kind names, from `--mode summary` (has a `kinds=` column).""" |
| 96 | out: dict[str, set[str]] = {} |
| 97 | for line in run_dump(cmd, archive, "--mode", "summary").splitlines(): |
| 98 | parts = line.split("\t") |
| 99 | if len(parts) < 3: |
| 100 | continue |
| 101 | layer = parts[1] |
| 102 | kinds: set[str] = set() |
| 103 | for p in parts[2:]: |
| 104 | k, _, v = p.partition("=") |
| 105 | if k == "kinds" and v: |
| 106 | # `kind_label` appends `:detail` when a feature carries one; the kind |
| 107 | # is the head. Landtype features never carry details, but strip anyway. |
| 108 | kinds.update(name.partition(":")[0] for name in v.split(",")) |
| 109 | out.setdefault(layer, set()).update(kinds) |
| 110 | return out |
| 111 | |
| 112 | |
| 113 | def main() -> int: |
| 114 | args = [a for a in sys.argv[1:] if not a.startswith("--dump")] |
| 115 | dump = None |
| 116 | for i, a in enumerate(sys.argv[1:]): |
| 117 | if a == "--dump": |
| 118 | dump = Path(sys.argv[1:][i + 1]) |
| 119 | if not args: |
| 120 | print("usage: diff_kinds.py after.mamaps [before.mamaps] [--dump BIN]", file=sys.stderr) |
| 121 | return 2 |
| 122 | cmd = dump_command(dump) |
| 123 | after = Path(args[0]) |
| 124 | before = Path(args[1]) if len(args) > 1 else None |
| 125 | |
| 126 | failures: list[str] = [] |
| 127 | |
| 128 | layers = dict_layers(cmd, after) |
| 129 | if layers != EXPECTED_LAYERS: |
| 130 | failures.append(f"layers {layers} != expected {EXPECTED_LAYERS}") |
| 131 | |
| 132 | kinds = summary_kinds(cmd, after) |
| 133 | landtype_kinds = kinds.get("landtype", set()) |
| 134 | for kind in EXPECTED_NEW_KINDS: |
| 135 | if kind not in landtype_kinds: |
| 136 | failures.append(f"landtype carries no `{kind}`") |
| 137 | # The dictionary itself must carry the new ids: a kind on a feature that the |
| 138 | # dict lacks prints as `-` in the dump. |
| 139 | dict_kind_names = dict_kinds(cmd, after) |
| 140 | for kind in EXPECTED_NEW_KINDS: |
| 141 | if kind not in dict_kind_names: |
| 142 | failures.append(f"dictionary carries no kind `{kind}`") |
| 143 | # Unknown kinds surface as `-` in the dump rather than names. |
| 144 | for layer, names in sorted(kinds.items()): |
| 145 | if "-" in names: |
| 146 | failures.append(f"{layer} carries features with an unknown kind") |
| 147 | |
| 148 | print(f"layers: {layers}") |
| 149 | print(f"landtype kinds ({len(landtype_kinds)}): {sorted(landtype_kinds)}") |
| 150 | if before is not None: |
| 151 | before_kinds = summary_kinds(cmd, before) |
| 152 | appeared = { |
| 153 | layer: sorted(names - before_kinds.get(layer, set())) |
| 154 | for layer, names in kinds.items() |
| 155 | if names - before_kinds.get(layer, set()) |
| 156 | } |
| 157 | print(f"kinds appearing vs {before}: {appeared or 'none'}") |
| 158 | |
| 159 | print() |
| 160 | if not failures: |
| 161 | print("OK: 9 layers with landtype first, 4 v8 kinds present, no unknown kinds") |
| 162 | return 0 |
| 163 | print(f"FAIL ({len(failures)}):") |
| 164 | for f in failures: |
| 165 | print(f" {f}") |
| 166 | return 1 |
| 167 | |
| 168 | |
| 169 | if __name__ == "__main__": |
| 170 | sys.exit(main()) |