|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -------------------------------------------------------------------------- |
| 3 | +# Inspect only changed FastAPI templates (for pre-commit / local use) |
| 4 | +# -------------------------------------------------------------------------- |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from typing import List, Set |
| 10 | + |
| 11 | +# Ensure src is importable |
| 12 | +PROJECT_ROOT = Path(__file__).parent.parent |
| 13 | +sys.path.insert(0, str(PROJECT_ROOT / "src")) |
| 14 | + |
| 15 | +from fastapi_fastkit.backend.inspector import inspect_fastapi_template |
| 16 | + |
| 17 | +TEMPLATE_DIR = PROJECT_ROOT / "src" / "fastapi_fastkit" / "fastapi_project_template" |
| 18 | + |
| 19 | + |
| 20 | +def get_changed_files() -> List[str]: |
| 21 | + """Return a list of changed files according to git (staged + unstaged).""" |
| 22 | + # Include staged and unstaged changes compared to HEAD |
| 23 | + cmd = [ |
| 24 | + "git", |
| 25 | + "diff", |
| 26 | + "--name-only", |
| 27 | + "--cached", |
| 28 | + ] |
| 29 | + staged = subprocess.run(cmd, capture_output=True, text=True, check=False) |
| 30 | + cmd_unstaged = [ |
| 31 | + "git", |
| 32 | + "diff", |
| 33 | + "--name-only", |
| 34 | + ] |
| 35 | + unstaged = subprocess.run(cmd_unstaged, capture_output=True, text=True, check=False) |
| 36 | + |
| 37 | + files: Set[str] = set() |
| 38 | + if staged.stdout: |
| 39 | + files.update( |
| 40 | + [line.strip() for line in staged.stdout.splitlines() if line.strip()] |
| 41 | + ) |
| 42 | + if unstaged.stdout: |
| 43 | + files.update( |
| 44 | + [line.strip() for line in unstaged.stdout.splitlines() if line.strip()] |
| 45 | + ) |
| 46 | + return sorted(files) |
| 47 | + |
| 48 | + |
| 49 | +def extract_changed_templates(changed_files: List[str]) -> List[str]: |
| 50 | + """Map changed files to template directory names under TEMPLATE_DIR.""" |
| 51 | + templates: Set[str] = set() |
| 52 | + template_root_str = str(TEMPLATE_DIR).rstrip("/") |
| 53 | + |
| 54 | + for f in changed_files: |
| 55 | + fp = (PROJECT_ROOT / f).resolve() |
| 56 | + # Only consider files under template root |
| 57 | + try: |
| 58 | + if str(fp).startswith(template_root_str): |
| 59 | + # template name is immediate child dir of TEMPLATE_DIR |
| 60 | + # e.g., .../fastapi_project_template/<template>/... |
| 61 | + rel = str(fp)[len(template_root_str) + 1 :] |
| 62 | + parts = rel.split(os.sep) |
| 63 | + if parts: |
| 64 | + templates.add(parts[0]) |
| 65 | + except Exception: |
| 66 | + continue |
| 67 | + |
| 68 | + # Filter only directories that truly exist |
| 69 | + return sorted([t for t in templates if (TEMPLATE_DIR / t).is_dir()]) |
| 70 | + |
| 71 | + |
| 72 | +def main() -> int: |
| 73 | + changed_files = get_changed_files() |
| 74 | + changed_templates = extract_changed_templates(changed_files) |
| 75 | + |
| 76 | + if not changed_templates: |
| 77 | + print("No template changes detected; skipping inspection.") |
| 78 | + return 0 |
| 79 | + |
| 80 | + print(f"Detected changed templates: {', '.join(changed_templates)}") |
| 81 | + any_failed = False |
| 82 | + |
| 83 | + for template in changed_templates: |
| 84 | + t_path = TEMPLATE_DIR / template |
| 85 | + print(f"Inspecting changed template: {template}\n Path: {t_path}") |
| 86 | + try: |
| 87 | + result = inspect_fastapi_template(str(t_path)) |
| 88 | + if not result.get("is_valid", False): |
| 89 | + any_failed = True |
| 90 | + except Exception as e: |
| 91 | + any_failed = True |
| 92 | + print(f"Exception while inspecting {template}: {e}") |
| 93 | + |
| 94 | + if any_failed: |
| 95 | + print("Template inspection failed for at least one changed template.") |
| 96 | + return 1 |
| 97 | + |
| 98 | + print("All changed templates passed inspection.") |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + sys.exit(main()) |
0 commit comments