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