|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Update dynamic badges in README.md with current project stats.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from urllib.parse import quote |
| 11 | + |
| 12 | +ROOT = Path(__file__).resolve().parent.parent |
| 13 | +README = ROOT / "README.md" |
| 14 | + |
| 15 | + |
| 16 | +def _run(cmd: list[str], **kwargs) -> str: |
| 17 | + return subprocess.check_output(cmd, cwd=ROOT, text=True, **kwargs).strip() |
| 18 | + |
| 19 | + |
| 20 | +def count_files() -> int: |
| 21 | + src = list((ROOT / "src").rglob("*.py")) |
| 22 | + tests = list((ROOT / "tests").rglob("*.py")) |
| 23 | + return len(src) + len(tests) |
| 24 | + |
| 25 | + |
| 26 | +def count_loc() -> int: |
| 27 | + total = 0 |
| 28 | + for d in ("src", "tests"): |
| 29 | + for f in (ROOT / d).rglob("*.py"): |
| 30 | + total += sum(1 for _ in f.open()) |
| 31 | + return total |
| 32 | + |
| 33 | + |
| 34 | +def count_tests() -> int: |
| 35 | + try: |
| 36 | + out = _run( |
| 37 | + [sys.executable, "-m", "pytest", "tests/", "-q", "--tb=no", "--no-header"], |
| 38 | + stderr=subprocess.STDOUT, |
| 39 | + ) |
| 40 | + except subprocess.CalledProcessError as e: |
| 41 | + out = e.output |
| 42 | + # Match "113 passed" from pytest output |
| 43 | + m = re.search(r"(\d+) passed", out) |
| 44 | + return int(m.group(1)) if m else 0 |
| 45 | + |
| 46 | + |
| 47 | +def count_detectors_and_languages() -> tuple[int, int]: |
| 48 | + out = _run([ |
| 49 | + sys.executable, "-c", |
| 50 | + "from code_intelligence.detectors.registry import DetectorRegistry; " |
| 51 | + "r = DetectorRegistry(); r.load_builtin_detectors(); " |
| 52 | + "langs = {l for d in r.all_detectors() for l in d.supported_languages}; " |
| 53 | + "print(len(r.all_detectors()), len(langs))", |
| 54 | + ]) |
| 55 | + parts = out.strip().split() |
| 56 | + return int(parts[0]), int(parts[1]) |
| 57 | + |
| 58 | + |
| 59 | +def fmt(n: int) -> str: |
| 60 | + """Format number with commas for display, URL-encoded.""" |
| 61 | + return f"{n:,}" |
| 62 | + |
| 63 | + |
| 64 | +def badge(label: str, value: str, color: str, logo: str) -> str: |
| 65 | + """Generate a shields.io badge HTML snippet.""" |
| 66 | + val_encoded = quote(value, safe="") |
| 67 | + return ( |
| 68 | + f'<a href="https://github.com/RandomCodeSpace/code-iq">' |
| 69 | + f'<img src="https://img.shields.io/badge/{label}-{val_encoded}-{color}' |
| 70 | + f'?style=flat-square&logo={logo}&logoColor=white" alt="{value} {label.capitalize()}">' |
| 71 | + f"</a>" |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def update_badge(content: str, name: str, new_badge: str) -> str: |
| 76 | + """Replace content between DYNAMIC markers.""" |
| 77 | + pattern = rf"(<!-- DYNAMIC:{name} -->).*?(<!-- /DYNAMIC:{name} -->)" |
| 78 | + replacement = rf"\g<1>{new_badge}\g<2>" |
| 79 | + return re.sub(pattern, replacement, content) |
| 80 | + |
| 81 | + |
| 82 | +def main() -> None: |
| 83 | + print("Collecting project stats...") |
| 84 | + |
| 85 | + files = count_files() |
| 86 | + loc = count_loc() |
| 87 | + tests = count_tests() |
| 88 | + detectors, languages = count_detectors_and_languages() |
| 89 | + |
| 90 | + print(f" Files: {files}") |
| 91 | + print(f" LOC: {loc:,}") |
| 92 | + print(f" Tests: {tests}") |
| 93 | + print(f" Detectors: {detectors}") |
| 94 | + print(f" Languages: {languages}") |
| 95 | + |
| 96 | + content = README.read_text() |
| 97 | + original = content |
| 98 | + |
| 99 | + content = update_badge( |
| 100 | + content, "detectors", |
| 101 | + badge("detectors", str(detectors), "brightgreen", "codefactor"), |
| 102 | + ) |
| 103 | + content = update_badge( |
| 104 | + content, "languages", |
| 105 | + badge("languages", str(languages), "blue", "stackblitz"), |
| 106 | + ) |
| 107 | + content = update_badge( |
| 108 | + content, "tests", |
| 109 | + badge("tests", f"{tests} passed", "brightgreen", "pytest"), |
| 110 | + ) |
| 111 | + content = update_badge( |
| 112 | + content, "files", |
| 113 | + badge("files", str(files), "informational", "files"), |
| 114 | + ) |
| 115 | + content = update_badge( |
| 116 | + content, "loc", |
| 117 | + badge("LOC", fmt(loc), "informational", "codacy"), |
| 118 | + ) |
| 119 | + |
| 120 | + if content != original: |
| 121 | + README.write_text(content) |
| 122 | + print("README.md updated.") |
| 123 | + else: |
| 124 | + print("No changes needed.") |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments