#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""筛出 crawl_*.py 里「手动写 gov_search」的脚本，并判断是否与 gov_raw 插入同处一个 try
（那才是会连带回滚、静默丢数据的危险模式）。"""
import io
import os
import re

D = "/root/gov_crawler"
pat_gs = re.compile(r"INSERT\s+(?:OR\s+REPLACE\s+)?INTO\s+gov_search", re.I)
pat_gr = re.compile(r"INSERT\s+(?:OR\s+(?:IGNORE|REPLACE)\s+)?INTO\s+gov_raw", re.I)

danger, other = [], []
for fn in sorted(os.listdir(D)):
    if not fn.startswith("crawl_") or not fn.endswith(".py"):
        continue
    src = io.open(os.path.join(D, fn), encoding="utf-8", errors="ignore").read()
    mgs = list(pat_gs.finditer(src))
    if not mgs:
        continue
    mgr = list(pat_gr.finditer(src))
    if not mgr:
        other.append((fn, "有 gov_search 插入但无 gov_raw 插入"))
        continue
    # 危险判据：gov_search 插入位置在 gov_raw 插入之后，且两者之间没有 commit
    bad = False
    for gs in mgs:
        for gr in mgr:
            if gr.start() < gs.start():
                seg = src[gr.end():gs.start()]
                if "commit" not in seg and len(seg) < 1200:
                    bad = True
    (danger if bad else other).append((fn, "gov_search 插入紧跟 gov_raw 且中间无 commit → 危险"))

print("=" * 92)
print("❌ 危险模式（gov_raw 插入后紧跟 gov_search 插入、中间没 commit）: %d 个" % len(danger))
print("=" * 92)
for fn, why in danger:
    print("   " + fn)
print()
print("其它（有 gov_search 插入但结构不同，需人工看）: %d 个" % len(other))
for fn, why in other:
    print("   %-46s %s" % (fn, why))
