diff --git a/.github/workflows/repository-hygiene.yml b/.github/workflows/repository-hygiene.yml index 8ead8b173..bd93c824b 100644 --- a/.github/workflows/repository-hygiene.yml +++ b/.github/workflows/repository-hygiene.yml @@ -30,3 +30,14 @@ jobs: - name: Run repository hygiene check run: python3 scripts/check_workspace_hygiene.py + + router-index: + name: Check router index fingerprint + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Compare the router index with its sources + run: python3 -m deeptutor.api.router_index diff --git a/deeptutor/api/router_index.py b/deeptutor/api/router_index.py new file mode 100644 index 000000000..81b681762 --- /dev/null +++ b/deeptutor/api/router_index.py @@ -0,0 +1,39 @@ +"""Check the generated router index without loading the API.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path + +INDEX_PATH = Path(__file__).with_name("router_index.json") + + +def source_fingerprint() -> str: + digest = hashlib.sha256() + root = Path(__file__).with_name("routers") + for path in sorted(root.rglob("*.py")): + digest.update(str(path.relative_to(root)).encode()) + digest.update(path.read_bytes()) + return digest.hexdigest() + + +def main() -> None: + try: + recorded = json.loads(INDEX_PATH.read_text(encoding="utf-8"))["fingerprint"] + except (KeyError, OSError, TypeError, ValueError) as exc: + raise SystemExit( + f"Cannot read the router index fingerprint: {exc}. " + "Run python scripts/build_router_index.py." + ) from exc + + if recorded != source_fingerprint(): + raise SystemExit( + "The router index fingerprint is out of date. Run python scripts/build_router_index.py." + ) + + print("The router index fingerprint matches the router sources.") + + +if __name__ == "__main__": + main() diff --git a/deeptutor/api/router_loader.py b/deeptutor/api/router_loader.py index 2107b85e6..98bee99fb 100644 --- a/deeptutor/api/router_loader.py +++ b/deeptutor/api/router_loader.py @@ -8,28 +8,16 @@ index disables deferral; OpenAPI expands every router before schema generation. from __future__ import annotations from copy import copy -import hashlib from importlib import import_module import json -from pathlib import Path from typing import Any from fastapi import APIRouter, FastAPI from starlette.routing import BaseRoute, Match, Route, WebSocketRoute +from deeptutor.api.router_index import INDEX_PATH, source_fingerprint from deeptutor.services.file_io import read_text -INDEX_PATH = Path(__file__).with_name("router_index.json") - - -def source_fingerprint() -> str: - digest = hashlib.sha256() - root = Path(__file__).with_name("routers") - for path in sorted(root.rglob("*.py")): - digest.update(str(path.relative_to(root)).encode()) - digest.update(path.read_bytes()) - return digest.hexdigest() - async def _unreachable(*_args: Any) -> None: raise RuntimeError("Deferred routing pattern cannot execute a request") diff --git a/scripts/build_router_index.py b/scripts/build_router_index.py index 91619ecac..8b8794567 100644 --- a/scripts/build_router_index.py +++ b/scripts/build_router_index.py @@ -15,7 +15,7 @@ os.environ["DEEPTUTOR_CHAT_ENGINE"] = "python" from starlette.routing import WebSocketRoute # noqa: E402 from deeptutor.api.main import _router_loader # noqa: E402 -from deeptutor.api.router_loader import INDEX_PATH, source_fingerprint # noqa: E402 +from deeptutor.api.router_index import INDEX_PATH, source_fingerprint # noqa: E402 routers = {} for reference, router in _router_loader.records.items():