#!/usr/bin/env python3 """Parity check for the generated IconXxx() wrappers. Compares the public icon API (name + normalized signature + icon source + content description) of a reference Icons.kt against one or more replacement Kotlin files (the hand-written Icons.kt plus the build-generated IconsGenerated.kt). Every reference entry must appear exactly once in the replacements, with an identical signature and the same icon/description. Usage: git show HEAD:library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt > /tmp/Icons.kt.old python3 scripts/check_icons_parity.py /tmp/Icons.kt.old \ library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt \ library/ui/build/generated/source/icons/debug/IconsGenerated.kt Exits 0 only on an exact multiset match (334/334 for the original file). Stdlib only. """ import re import sys from collections import Counter from pathlib import Path def extract(path): lines = Path(path).read_text().splitlines() rows = [] i = 0 while i < len(lines): line = lines[i] if line.strip() == "@Composable" and i + 1 < len(lines) and re.match( r"fun (Icon\w+|AppIcon)\b", lines[i + 1].strip() ): sig_lines = [] j = i + 1 text = "" endpos = None while j < len(lines): sig_lines.append(lines[j]) j += 1 text = "\n".join(sig_lines) depth = 0 started = False in_str = False esc = False for idx, ch in enumerate(text): if in_str: if esc: esc = False elif ch == "\\": esc = True elif ch == '"': in_str = False continue if ch == '"': in_str = True elif ch == "(": depth += 1 started = True elif ch == ")": depth -= 1 if started and depth == 0: endpos = idx break if endpos is not None: break name = re.match(r"\s*fun (Icon\w+|AppIcon)\b", text).group(1) params = " ".join(text[text.find("(") + 1 : endpos].split()).rstrip(",") params = re.sub(r",\s*\)", ")", params) tail = text[endpos + 1 :].strip() if tail.startswith("="): stmt = tail[1:].strip() if not stmt: stmt = lines[j].strip() j += 1 body = " ".join(stmt.split()) elif tail.startswith("{"): body_lines = [tail] k = j while k < len(lines): body_lines.append(lines[k]) if lines[k] == "}": k += 1 break k += 1 body = "\n".join(body_lines) j = k else: body = tail if name.startswith("Icon"): rows.append((name, params, semantics(name, params, body))) i = j continue i += 1 return rows def semantics(name, params, body): """(kind, source, description) for a wrapper; NAV for the back buttons.""" if name == "IconNavigation": return ("NAV", "", "") flat = " ".join(body.split()) pm = re.match( r'Icon\(painterResource\(R\.drawable\.(\w+)\),\s*("(?:[^"\\]|\\.)*"),\s*modifier = modifier, tint = tint\)', flat, ) if pm: return ("S", f"drawable:{pm.group(1)}", pm.group(2)) am = re.match( r'AppIcon\(([^,]+),\s*("(?:[^"\\]|\\.)*"|contentDescription),\s*modifier, tint\)$', flat ) if not am: return ("??", flat[:120], "") ref = am.group(1).strip() desc = am.group(2).strip() source = ref[len("Icons.") :] if ref.startswith("Icons.") else f"vector:{ref}" if params.rstrip(",") == "modifier: Modifier = Modifier, tint: Color = LocalContentColor.current": return ("S", source, desc) if "contentDescription: String?" in params and params.startswith("modifier:"): mm = re.search(r'contentDescription: String\? = ("(?:[^"\\]|\\.)*"),?$', params) return ("T", source, mm.group(1) if mm else "FAIL") if params.startswith("contentDescription: String?"): return ("F", source, "") return ("??", params, "") def main(argv): if len(argv) < 3: print(__doc__) return 2 reference = extract(argv[1]) candidate = [] for path in argv[2:]: candidate.extend(extract(path)) ref_counts = Counter(reference) cand_counts = Counter(candidate) missing = ref_counts - cand_counts extra = cand_counts - ref_counts print(f"reference: {len(reference)} wrappers ({sum(ref_counts.values())} entries)") print(f"candidate: {len(candidate)} wrappers ({sum(cand_counts.values())} entries)") for entry, count in sorted(missing.items()): print(f"MISSING x{count}: {entry}") for entry, count in sorted(extra.items()): print(f"EXTRA x{count}: {entry}") if missing or extra: print("PARITY: FAIL") return 1 print(f"PARITY: OK — {len(reference)}/{len(reference)} wrappers identical (name, signature, source, description)") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))