|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Detect added and removed sdk.name values in Config.kt between two revisions.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +CONFIG_PATH = "buildSrc/src/main/java/Config.kt" |
| 14 | +SDK_MAP_SHEET_URL = ( |
| 15 | + "https://docs.google.com/spreadsheets/d/1hqFhytQuHMvuOz1XD0kCXg6x0ViflHrpjW7nNhvzYmU/" |
| 16 | + "edit?gid=334165604" |
| 17 | +) |
| 18 | +SDK_NAME_PATTERN = re.compile(r"^\s*val (\w+SDK_NAME) = (.+)$") |
| 19 | +STRING_LITERAL = re.compile(r'^"((?:[^"\\]|\\.)*)"$') |
| 20 | +INTERPOLATION_PATTERN = re.compile(r"\$([A-Za-z0-9_]+)") |
| 21 | + |
| 22 | + |
| 23 | +def parse_sdk_constants(content: str) -> dict[str, str]: |
| 24 | + """Return mapping of constant name to resolved sdk.name string.""" |
| 25 | + resolved: dict[str, str] = {} |
| 26 | + for line in content.splitlines(): |
| 27 | + match = SDK_NAME_PATTERN.match(line) |
| 28 | + if not match: |
| 29 | + continue |
| 30 | + name, rhs = match.group(1), match.group(2).strip() |
| 31 | + resolved[name] = resolve_rhs(rhs, resolved) |
| 32 | + return resolved |
| 33 | + |
| 34 | + |
| 35 | +def resolve_rhs(rhs: str, resolved: dict[str, str]) -> str: |
| 36 | + literal = STRING_LITERAL.match(rhs) |
| 37 | + if not literal: |
| 38 | + raise ValueError(f"Cannot parse SDK name assignment: {rhs}") |
| 39 | + return expand_interpolation(literal.group(1), resolved) |
| 40 | + |
| 41 | + |
| 42 | +def expand_interpolation(value: str, resolved: dict[str, str]) -> str: |
| 43 | + if "${" in value: |
| 44 | + raise ValueError(f"Unsupported brace interpolation in SDK name value: {value}") |
| 45 | + |
| 46 | + def replace(match: re.Match[str]) -> str: |
| 47 | + name = match.group(1) |
| 48 | + if name not in resolved: |
| 49 | + raise ValueError(f"Unknown SDK name constant: {name}") |
| 50 | + return resolved[name] |
| 51 | + |
| 52 | + return INTERPOLATION_PATTERN.sub(replace, value) |
| 53 | + |
| 54 | + |
| 55 | +def find_sdk_name_changes( |
| 56 | + base: dict[str, str], head: dict[str, str] |
| 57 | +) -> tuple[list[str], list[str]]: |
| 58 | + base_values = set(base.values()) |
| 59 | + head_values = set(head.values()) |
| 60 | + added = sorted(head_values - base_values) |
| 61 | + removed = sorted(base_values - head_values) |
| 62 | + return added, removed |
| 63 | + |
| 64 | + |
| 65 | +def git_show(ref: str, path: str) -> str: |
| 66 | + try: |
| 67 | + return subprocess.check_output( |
| 68 | + ["git", "show", f"{ref}:{path}"], |
| 69 | + text=True, |
| 70 | + stderr=subprocess.PIPE, |
| 71 | + ) |
| 72 | + except subprocess.CalledProcessError as error: |
| 73 | + if error.returncode == 128 and "exists on disk, but not in" in error.stderr: |
| 74 | + return "" |
| 75 | + raise |
| 76 | + |
| 77 | + |
| 78 | +def read_config_source(base: str | None, head: str | None, config_path: str) -> tuple[str, str]: |
| 79 | + if base is None or head is None: |
| 80 | + raise ValueError("Both base and head refs are required") |
| 81 | + return git_show(base, config_path), git_show(head, config_path) |
| 82 | + |
| 83 | + |
| 84 | +def format_changes(added: list[str], removed: list[str]) -> str: |
| 85 | + return json.dumps( |
| 86 | + { |
| 87 | + "added": added, |
| 88 | + "removed": removed, |
| 89 | + "sdk_map_sheet_url": SDK_MAP_SHEET_URL, |
| 90 | + }, |
| 91 | + separators=(",", ":"), |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def main(argv: list[str] | None = None) -> int: |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + description="Print added and removed sdk.name values as JSON." |
| 98 | + ) |
| 99 | + parser.add_argument("--base", help="Git ref for the PR base revision") |
| 100 | + parser.add_argument("--head", help="Git ref for the PR head revision") |
| 101 | + parser.add_argument("--base-file", type=Path, help="Base Config.kt file") |
| 102 | + parser.add_argument("--head-file", type=Path, help="Head Config.kt file") |
| 103 | + parser.add_argument("--config-path", default=CONFIG_PATH) |
| 104 | + args = parser.parse_args(argv) |
| 105 | + |
| 106 | + try: |
| 107 | + if args.base_file and args.head_file: |
| 108 | + base_content = args.base_file.read_text(encoding="utf-8") |
| 109 | + head_content = args.head_file.read_text(encoding="utf-8") |
| 110 | + elif args.base and args.head: |
| 111 | + base_content, head_content = read_config_source( |
| 112 | + args.base, args.head, args.config_path |
| 113 | + ) |
| 114 | + else: |
| 115 | + parser.error("Provide --base/--head or --base-file/--head-file") |
| 116 | + |
| 117 | + base_sdk = parse_sdk_constants(base_content) |
| 118 | + head_sdk = parse_sdk_constants(head_content) |
| 119 | + except ValueError as error: |
| 120 | + print(f"error: {error}", file=sys.stderr) |
| 121 | + return 1 |
| 122 | + except subprocess.CalledProcessError as error: |
| 123 | + print(f"error: git command failed: {' '.join(error.cmd)}", file=sys.stderr) |
| 124 | + return 1 |
| 125 | + |
| 126 | + added, removed = find_sdk_name_changes(base_sdk, head_sdk) |
| 127 | + print(format_changes(added, removed)) |
| 128 | + return 0 |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + sys.exit(main()) |
0 commit comments