120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Report changed or untracked generated outputs in the current checkout."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
GENERATED_PREFIXES = (
|
|
"backend/internal/adapter/primary/http/api/",
|
|
"backend/internal/adapter/secondary/ml/api/",
|
|
"backend/internal/adapter/secondary/code_runner/api/",
|
|
"backend/internal/adapter/secondary/postgres/sqlc/",
|
|
"frontend/src/shared/api/endpoints/",
|
|
"frontend/src/shared/api/schemas/",
|
|
"runner/internal/api/",
|
|
)
|
|
GENERATED_BASENAMES = {"wire_gen.go"}
|
|
GENERATED_FILES = {
|
|
"api/core/openapi.bundle.yml",
|
|
"api/core/components/schemas.yml",
|
|
"api/ml/openapi.bundle.yml",
|
|
"api/ml/components/schemas.yml",
|
|
"api/runner/openapi.bundle.yml",
|
|
"api/runner/components/schemas.yml",
|
|
"ml/src/lincodex_ml/api/models.py",
|
|
}
|
|
SOURCE_SUFFIXES = {".go", ".ts", ".tsx", ".js", ".sql", ".json", ".py"}
|
|
GENERATED_HEADER = re.compile(
|
|
rb"(?im)^\s*(?://|#|/\*|\*|<!--|--|;)\s*"
|
|
rb"(?:code generated|generated by|this file was generated|@generated)\b"
|
|
)
|
|
|
|
|
|
def git_output(root: Path, *args: str) -> bytes:
|
|
result = subprocess.run(
|
|
["git", *args], cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
|
)
|
|
if result.returncode != 0:
|
|
detail = result.stderr.decode("utf-8", errors="replace").strip()
|
|
raise RuntimeError(detail or f"git {' '.join(args)} failed")
|
|
return result.stdout
|
|
|
|
|
|
def is_generated(root: Path, relative_path: str) -> bool:
|
|
normalized = relative_path.replace(os.sep, "/")
|
|
if normalized.startswith("codegen/") or "/codegen/" in normalized:
|
|
return False
|
|
|
|
if normalized in GENERATED_FILES or normalized.startswith(GENERATED_PREFIXES):
|
|
return True
|
|
if Path(normalized).name in GENERATED_BASENAMES:
|
|
return True
|
|
if Path(normalized).suffix.lower() not in SOURCE_SUFFIXES:
|
|
return False
|
|
|
|
path = root / relative_path
|
|
if not path.is_file():
|
|
return False
|
|
try:
|
|
prefix = path.read_bytes()[:4096]
|
|
except OSError:
|
|
return False
|
|
return GENERATED_HEADER.search(prefix) is not None
|
|
|
|
|
|
def main() -> int:
|
|
try:
|
|
top = subprocess.run(
|
|
["git", "rev-parse", "--show-toplevel"],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
)
|
|
if top.returncode != 0:
|
|
print(
|
|
"check-generated: run this command from a Git working tree.",
|
|
file=sys.stderr,
|
|
)
|
|
return 2
|
|
root = Path(os.fsdecode(top.stdout.rstrip(b"\n"))).resolve()
|
|
git_output(root, "rev-parse", "--verify", "HEAD^{commit}")
|
|
changed = git_output(
|
|
root, "diff", "--name-only", "--diff-filter=ACMRTD", "-z", "HEAD", "--"
|
|
)
|
|
untracked = git_output(root, "ls-files", "--others", "--exclude-standard", "-z")
|
|
except RuntimeError as error:
|
|
print(f"check-generated: cannot inspect Git state: {error}", file=sys.stderr)
|
|
return 2
|
|
|
|
drift: list[tuple[str, str]] = []
|
|
for path in (os.fsdecode(part) for part in changed.split(b"\0") if part):
|
|
if is_generated(root, path):
|
|
drift.append(("changed", path))
|
|
for path in (os.fsdecode(part) for part in untracked.split(b"\0") if part):
|
|
if is_generated(root, path):
|
|
drift.append(("untracked", path))
|
|
|
|
if drift:
|
|
for state, path in sorted(set(drift)):
|
|
if state == "changed":
|
|
print(f"generated output differs from committed state: {path}", file=sys.stderr)
|
|
else:
|
|
print(f"untracked generated output is present: {path}", file=sys.stderr)
|
|
print(
|
|
"check-generated: regenerate outputs and commit the matching generator inputs and outputs.",
|
|
file=sys.stderr,
|
|
)
|
|
return 1
|
|
|
|
print("check-generated: generated outputs match the committed state.")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|