|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +#MISE description="Make sure the BOM has all necessary modules" |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | +import sys |
| 8 | +from fnmatch import fnmatch |
| 9 | +from pathlib import Path |
| 10 | +from typing import List |
| 11 | +import difflib |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parents[2] # repo root (.. from .mise/tasks) |
| 14 | +IGNORE_DIRS = {"prometheus-metrics-parent"} |
| 15 | +MODULE_PREFIX = "prometheus-metrics" |
| 16 | +BOM_POM = ROOT / "prometheus-metrics-bom" / "pom.xml" |
| 17 | + |
| 18 | + |
| 19 | +def first_artifact_id(pom_file: Path) -> str: |
| 20 | + """Return the second <artifactId> value from the given pom.xml (matches original script). |
| 21 | +
|
| 22 | + The original shell function greps all <artifactId> lines and returns the second one |
| 23 | + (head -n 2 | tail -n 1). We replicate that behavior exactly. |
| 24 | + """ |
| 25 | + if not pom_file.is_file(): |
| 26 | + raise FileNotFoundError(f"File {pom_file} does not exist.") |
| 27 | + |
| 28 | + text = pom_file.read_text(encoding="utf-8") |
| 29 | + matches = re.findall(r"<artifactId>\s*(.*?)\s*</artifactId>", text) |
| 30 | + if len(matches) < 2: |
| 31 | + return "" |
| 32 | + return matches[1].strip() |
| 33 | + |
| 34 | + |
| 35 | +def add_dir(dir_path: Path, want: List[str]): |
| 36 | + if not dir_path.is_dir(): |
| 37 | + raise FileNotFoundError(f"Directory {dir_path} does not exist.") |
| 38 | + |
| 39 | + if any(dir_path.name == ig for ig in IGNORE_DIRS): |
| 40 | + print(f"Skipping {dir_path}") |
| 41 | + return |
| 42 | + |
| 43 | + pom = dir_path / "pom.xml" |
| 44 | + if not pom.is_file(): |
| 45 | + raise FileNotFoundError(f"File {pom} does not exist.") |
| 46 | + |
| 47 | + artifact_id = first_artifact_id(pom) |
| 48 | + if not artifact_id: |
| 49 | + raise RuntimeError(f"No artifactId found in {pom}") |
| 50 | + |
| 51 | + print(f"Found artifactId '{artifact_id}' in {pom}") |
| 52 | + want.append(artifact_id) |
| 53 | + |
| 54 | + |
| 55 | +def collect_want(root: Path) -> List[str]: |
| 56 | + want: List[str] = [] |
| 57 | + # top-level prometheus-metrics* |
| 58 | + for entry in sorted(root.iterdir()): |
| 59 | + if entry.is_dir() and fnmatch(entry.name, f"{MODULE_PREFIX}*"): |
| 60 | + add_dir(entry, want) |
| 61 | + |
| 62 | + # prometheus-metrics-tracer/prometheus-metrics* |
| 63 | + tracer_dir = root / "prometheus-metrics-tracer" |
| 64 | + if tracer_dir.is_dir(): |
| 65 | + for entry in sorted(tracer_dir.iterdir()): |
| 66 | + if entry.is_dir() and fnmatch(entry.name, f"{MODULE_PREFIX}*"): |
| 67 | + add_dir(entry, want) |
| 68 | + |
| 69 | + # deduplicate and sort |
| 70 | + want_unique = sorted(set(want)) |
| 71 | + return want_unique |
| 72 | + |
| 73 | + |
| 74 | +def collect_have(bom_pom: Path) -> List[str]: |
| 75 | + if not bom_pom.is_file(): |
| 76 | + raise FileNotFoundError(f"BOM file {bom_pom} does not exist.") |
| 77 | + |
| 78 | + text = bom_pom.read_text(encoding="utf-8") |
| 79 | + # find artifactId values that start with MODULE_PREFIX |
| 80 | + matches = re.findall(r"<artifactId>\s*(%s[^<\s]*)\s*</artifactId>" % re.escape(MODULE_PREFIX), text) |
| 81 | + return sorted(matches) |
| 82 | + |
| 83 | + |
| 84 | +def main() -> int: |
| 85 | + try: |
| 86 | + want = collect_want(ROOT) |
| 87 | + have = collect_have(BOM_POM) |
| 88 | + |
| 89 | + want_text = "\n".join(want) |
| 90 | + have_text = "\n".join(have) |
| 91 | + |
| 92 | + if want_text != have_text: |
| 93 | + print("The BOM file prometheus-metrics-bom/bom.xml does not match the current directory contents.") |
| 94 | + print("Expected:") |
| 95 | + print(want_text) |
| 96 | + print("Found:") |
| 97 | + print(have_text) |
| 98 | + print() |
| 99 | + diff = difflib.unified_diff(have_text.splitlines(keepends=True), want_text.splitlines(keepends=True), fromfile="found", tofile="expected") |
| 100 | + sys.stdout.writelines(diff) |
| 101 | + return 1 |
| 102 | + else: |
| 103 | + print("BOM file is up to date.") |
| 104 | + return 0 |
| 105 | + |
| 106 | + except Exception as e: |
| 107 | + print(e, file=sys.stderr) |
| 108 | + return 1 |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + sys.exit(main()) |
| 113 | + |
0 commit comments