54 lines
1.9 KiB
Python
Executable File
54 lines
1.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""清理验收台账中的陈旧证据:被后续同码记录覆盖的历史、以及阶段中断标记。"""
|
|
import argparse
|
|
import json
|
|
import pathlib
|
|
|
|
FILE = pathlib.Path(__file__).resolve().parent.parent / ".local" / "acceptance" / "evidence.jsonl"
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--drop-code", action="append", default=[],
|
|
help="彻底删除该编号的所有记录(用于已废弃的验收项编号)")
|
|
ap.add_argument("--dedup", action="store_true", help="每个 section|code 只保留最后一条")
|
|
ap.add_argument("--list", action="store_true", help="只列出现有编号")
|
|
args = ap.parse_args()
|
|
|
|
rows = [json.loads(l) for l in FILE.read_text(encoding="utf-8").splitlines() if l.strip()]
|
|
if args.list:
|
|
for key in sorted({f"{r['section']}|{r['code']}" for r in rows}):
|
|
print(key)
|
|
return 0
|
|
|
|
kept, dropped = [], 0
|
|
for r in rows:
|
|
if r["code"] in args.drop_code:
|
|
dropped += 1
|
|
continue
|
|
kept.append(r)
|
|
if args.dedup:
|
|
latest: dict[str, dict] = {}
|
|
order: list[str] = []
|
|
for r in kept:
|
|
key = f"{r['section']}|{r['code']}"
|
|
if key not in latest:
|
|
order.append(key)
|
|
latest[key] = r
|
|
dropped += len(kept) - len(latest)
|
|
kept = [latest[k] for k in order]
|
|
|
|
tmp = FILE.with_suffix(".jsonl.tmp")
|
|
with tmp.open("w", encoding="utf-8") as fh:
|
|
for r in kept:
|
|
fh.write(json.dumps(r, ensure_ascii=False) + "\n")
|
|
tmp.replace(FILE)
|
|
print(f"台账 {len(rows)} 条 → 保留 {len(kept)} 条(删除 {dropped} 条)")
|
|
bad = [r["code"] for r in kept if r["result"] != "通过"]
|
|
print("仍未通过:", bad or "无")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|