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

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

40 lines
1.1 KiB
Python

"""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()