check_configs.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """校验 configs/*.json 能被加载、与 DEFAULTS 正确深合并(打包/上线前自检)。
  2. 用法:
  3. python scripts/check_configs.py [configs/*.json 或目录 ...]
  4. 退出码:0 全部通过;1 有问题(打印到标准错误),可直接用于 CI 或发布前门禁。
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import glob
  9. import os
  10. import sys
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. from portcv.config import DEFAULTS, load_config # noqa: E402
  13. from portcv.plc.registers import addresses # noqa: E402
  14. def check_one(path: str) -> str:
  15. """检查单个配置文件,返回一行摘要;不通过时抛 ``AssertionError``。"""
  16. cfg = load_config(path)
  17. addr = addresses(cfg)
  18. # 1) 所有默认段都必须存在(深合并不会丢段)
  19. missing = [key for key in DEFAULTS if key not in cfg.data]
  20. assert not missing, "缺少配置段: %s" % missing
  21. # 2) 关键契约:4 个类别、12 字结果寄存器、4 字指令寄存器、方阵输入
  22. assert len(cfg.labels) == 4, "model.labels 必须是 4 类(container/bed/flatbed/not_on_train)"
  23. assert int(addr["input_count"]) == 12, "结果寄存器应为 12 字"
  24. assert int(addr["holding_count"]) == 4, "指令寄存器应为 4 字"
  25. width, height = cfg.input_size
  26. assert width > 0 and height > 0 and width % 32 == 0, "model.input_size 必须为正且 32 对齐"
  27. # 3) 没写到的字段必须仍是默认值(防止"手抄模板"覆盖掉默认值)
  28. assert cfg.get_path("decision.min_votes") == DEFAULTS["decision"]["min_votes"], "未覆盖项被意外改写"
  29. assert cfg.get_path("model.labels") == DEFAULTS["model"]["labels"], "类别顺序被意外改写"
  30. # 4) 后端与所需模型路径要配套
  31. backend = str(cfg.get_path("run.backend", "mock"))
  32. key = {"tensorrt": "model.engine_path", "rknn": "model.rknn_path"}.get(backend, "model.path")
  33. assert cfg.get_path(key), "backend=%s 需要 %s" % (backend, key)
  34. if backend == "rknn":
  35. assert str(cfg.get_path("model.input_format")) in ("rgb_uint8", "bgr_uint8"), \
  36. "RKNN INT8 通常需要 *_uint8 输入格式"
  37. return (
  38. "%-24s backend=%-9s source=%-6s in=%s regs=%d+%d plc=%s:%d unit=%d fps=%.1f"
  39. % (
  40. os.path.basename(path),
  41. backend,
  42. str(cfg.get_path("source.type")),
  43. cfg.input_size,
  44. int(addr["input_count"]),
  45. int(addr["holding_count"]),
  46. addr["host"],
  47. int(addr["port"]),
  48. int(addr["unit_id"]),
  49. float(cfg.get_path("run.max_fps", 0.0) or 0.0),
  50. )
  51. )
  52. def main(argv=None) -> int:
  53. parser = argparse.ArgumentParser(description="校验 configs/*.json")
  54. parser.add_argument("paths", nargs="*", default=None, help="配置文件或目录(默认 configs/*.json)")
  55. args = parser.parse_args(argv)
  56. targets = []
  57. for item in args.paths or [os.path.join("configs", "*.json")]:
  58. targets.extend(sorted(glob.glob(item)) if os.path.isdir(item) or "*" in item else [item])
  59. if not targets:
  60. print("没有找到任何配置文件", file=sys.stderr)
  61. return 1
  62. failures = 0
  63. for path in targets:
  64. try:
  65. print(check_one(path))
  66. except Exception as exc: # config 错误一律报告而不是中断,便于一次看完
  67. failures += 1
  68. print("FAIL %s: %s" % (path, exc), file=sys.stderr)
  69. if failures:
  70. print("%d/%d 个配置有问题" % (failures, len(targets)), file=sys.stderr)
  71. return 1
  72. print("全部通过:%d 个配置文件。" % len(targets))
  73. return 0
  74. if __name__ == "__main__":
  75. raise SystemExit(main())