chain_gap_check.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. r"""六层链"缺料"检查器(2026-09-17 用户令 #2 的收口件)。
  4. 作用只有一个:**把"还缺什么"变成一条命令**,而不是靠人记着哪四个脚本没随包。
  5. python scripts/chain_gap_check.py # 人看
  6. python scripts/chain_gap_check.py --check # 只出结论(缺料 → 退出码 5)
  7. 判据(全部是"文件在不在"这种可核事实,不含猜测):
  8. A. 四步脚本是否在位 —— 壳 `src/windcms/pipeline.py::analyze` 会调它们;
  9. B. 两件"标准答案"是否在位 —— `model_run_l6.parquet` / `fusion_38.csv`(逐值对拍的前提);
  10. C. 台账里这一族还剩多少"✗ 无生成端"(接料后应显著下降)。
  11. 取料单:`docs/向振动线取料单_v0.1.md`;接口规格:`docs/振动六层链_接口规格与缺口_v0.1.md`。
  12. """
  13. from __future__ import annotations
  14. import argparse
  15. import pathlib
  16. import sys
  17. ROOT = pathlib.Path(__file__).resolve().parents[1]
  18. sys.path.insert(0, str(ROOT))
  19. sys.path.insert(0, str(ROOT / 'scripts'))
  20. from src import paths as P # noqa: E402
  21. # 壳里写死的四步脚本(改壳才会变;这里只是把"缺什么"列清楚)
  22. STEPS = [
  23. ('oem_scan', 'scripts/rudong_tcm_oem_scan.py', '特征频率扫描(轴承/齿轮/保持架/叶片 1P)'),
  24. ('energy_share', 'scripts/rudong_line_energy_share.py', '线能量占比'),
  25. ('model_run', 'scripts/rudong_model_run.py', '六层模型出件(model_run_l6.parquet + 摘要)'),
  26. ('fusion', 'scripts/rudong_fusion_run.py', '融合面出件(fusion_38.csv 等)'),
  27. ]
  28. # 逐值对拍用的"标准答案"(随包件里有,但两件关键件从未随过包)
  29. # ★2026-09-19: 这两件现在**本机能自算**(scripts/rudong_model_run.py / rudong_fusion_run.py, 按口径重建)。
  30. # 于是"文件在不在"再也不能代表"有没有标准答案" —— 本机算出来的就躺在同一个路径上。
  31. # 判据改成看**台账里的来路**: builder 文本含"按口径重建" ⇒ 那是自算产物, 记 [自算] 不记 [OK]。
  32. SAMPLES = [
  33. ('model_run_l6.parquet', 'model_run 步的标准答案'),
  34. ('fusion_38.csv', 'fusion 步的标准答案'),
  35. ]
  36. def _selfmade(farm: str) -> set:
  37. """台账里标了"按口径重建"的件 —— 它们是**本机自算**, 不是随包标准答案。"""
  38. import json as _json
  39. out = set()
  40. f = P.out_root(farm) / '_provenance.json'
  41. if not f.is_file():
  42. return out
  43. try:
  44. files = (_json.loads(f.read_text(encoding='utf-8')) or {}).get('files') or {}
  45. except Exception:
  46. return out
  47. for rel, v in files.items():
  48. if '按口径重建' in str((v or {}).get('builder', '')) + str((v or {}).get('why', '')):
  49. out.add(pathlib.Path(rel).name)
  50. return out
  51. def main() -> int:
  52. ap = argparse.ArgumentParser(description='六层链缺料检查')
  53. ap.add_argument('--check', action='store_true', help='只出结论(缺料 → rc=5)')
  54. ap.add_argument('--farm', default=None)
  55. a = ap.parse_args()
  56. farm = a.farm or P.farm()
  57. m5 = P.m5(farm)
  58. missing_scripts = [s for s in STEPS if not (ROOT / s[1]).is_file()]
  59. partial = [] # 脚本在位、但自报 complete=false ⇒ 只是"部分实现"(分母就位/分子待口径)
  60. have_fusion_impl = (ROOT / 'scripts/rudong_fusion_run.py').is_file()
  61. missing_samples = [s for s in SAMPLES if not (m5 / s[0]).is_file()]
  62. print(f'== 六层链缺料检查 · 场站 {farm} ==')
  63. print(f' 壳: src/windcms/pipeline.py::analyze(它按固定路径调这四步)')
  64. print(f' 规格: docs/振动六层链_接口规格与缺口_v0.1.md | 取料单: docs/向振动线取料单_v0.1.md')
  65. print()
  66. print('A. 四步脚本:')
  67. for name, rel, what in STEPS:
  68. p = ROOT / rel
  69. ok = p.is_file()
  70. tag, extra = ('OK ', '') if ok else ('缺 ', '')
  71. if ok:
  72. # ★ 脚本在位 ≠ 这一步能出产物: 逆向实现可能是"分母就位、分子待口径"(自报 --status)。
  73. # 这里调一次 --status 把真实状态问出来, 免得自查器给出"看起来齐了"的假结论。
  74. import json as _json
  75. import subprocess as _sp
  76. try:
  77. r = _sp.run([sys.executable, str(p), '--status'], capture_output=True, text=True,
  78. errors='replace', timeout=120)
  79. st = _json.loads((r.stdout or '').strip().splitlines()[-1]) if (r.stdout or '').strip() else {}
  80. if st and st.get('complete') is False:
  81. tag = '部分'
  82. extra = '(' + st.get('denominator', '') + ';缺: ' + ';'.join(st.get('missing', []) or []) + ')'
  83. except Exception:
  84. pass
  85. if ok and tag == '部分':
  86. partial.append(name)
  87. if not ok and name == 'fusion' and have_fusion_impl:
  88. extra = '(本包已逆向实现其中一件产物: fleet_scalar_z;源码仍缺)'
  89. print(f' [{tag}] {name:12s} {rel:44s} {what}{extra}')
  90. print()
  91. print('B. 逐值对拍的标准答案:')
  92. selfmade = _selfmade(farm)
  93. for nm, why in SAMPLES:
  94. ok = (m5 / nm).is_file()
  95. if ok and nm in selfmade:
  96. print(f' [自算] {nm:28s} 本机**按口径重建**的产物在位(不是标准答案: 无样件可对拍) ({P.rel(m5 / nm)})')
  97. else:
  98. print(f' [{"OK " if ok else "缺 "}] {nm:28s} {why} ({P.rel(m5 / nm)})')
  99. # 已完成的那件: 用本器自带 verify 报一句现状(不写盘)
  100. fr = ROOT / 'scripts/rudong_fusion_run.py'
  101. if fr.is_file():
  102. print()
  103. print('C. 已自研部分(fleet_scalar_z): 跑 python scripts/rudong_fusion_run.py --verify 看逐值对拍')
  104. print()
  105. rc = 5 if (missing_scripts or partial) else 0
  106. selfmade_txt = (';两件关键产物已**按口径重建**(无标准答案可对拍: model_run_l6 / fusion_38)'
  107. if selfmade else '')
  108. tail = ('四步脚本齐全且都已落地 ⇒ 六层链可跑' if not (missing_scripts or partial)
  109. else (f'{len(missing_scripts)} 个步骤脚本未随包' if missing_scripts else '')
  110. + (';' if (missing_scripts and partial) else '')
  111. + (f'{len(partial)} 个为"部分实现"(分母就位 / 分子待口径)' if partial else '')
  112. + ' ⇒ 六层链跑不动(取料单见上)') + selfmade_txt
  113. print(f' 结论: {tail}')
  114. if a.check:
  115. print(f'[{"OK" if rc == 0 else "X"}] chain_gap_check rc={rc}')
  116. return rc
  117. if __name__ == '__main__':
  118. for _s in (sys.stdout, sys.stderr):
  119. try:
  120. _s.reconfigure(errors='replace')
  121. except Exception:
  122. pass
  123. sys.exit(main())