Files
DeepTutor/scripts/build_router_index.py
AleFlyX b4da3fefba chore: 增加路由索引指纹检查
变更:
- 在 Repository Hygiene 中校验已提交的路由指纹,与生成器和加载器共用算法。

验证:
- 路由加载测试 5 项通过。
- 校验命令对有效索引通过,对过期、缺失和损坏的索引失败。
- 重新生成的索引没有差异,改动文件的 Ruff 检查通过。
2026-11-30 21:39:50 +08:00

47 lines
1.6 KiB
Python

"""Regenerate the lazy API routing index after changing router declarations.
Run with the project's Python environment: python scripts/build_router_index.py
The index is an optimization only: a stale fingerprint falls back to eager load.
"""
import json
import os
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
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_index import INDEX_PATH, source_fingerprint # noqa: E402
routers = {}
for reference, router in _router_loader.records.items():
# Router-level lifecycle callbacks must be installed eagerly by FastAPI.
if (
router.on_startup
or router.on_shutdown
or type(router.lifespan_context).__name__ != "_DefaultLifespan"
):
continue
patterns = []
for route in router.routes:
if not hasattr(route, "path") or not hasattr(route, "name"):
raise ValueError(f"Unsupported nested route: {reference}")
patterns.append(
{
"path": route.path,
"name": route.name,
"kind": "websocket" if isinstance(route, WebSocketRoute) else "http",
"methods": sorted(getattr(route, "methods", None) or []),
}
)
routers[reference] = patterns
INDEX_PATH.write_text(
json.dumps({"fingerprint": source_fingerprint(), "routers": routers}, indent=2) + "\n",
encoding="utf-8",
)
print(f"Indexed {len(routers)} routers; {sum(map(len, routers.values()))} routes")