#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fix_del_govsearch_batch.py — 注释掉生效中的「整站类」gov_search 删除
import ast, os, py_compile, re, shutil, sqlite3
D = '/root/gov_crawler/'
ARC = D + 'Archive/'
os.makedirs(ARC, exist_ok=True)
TAG = '# QC20260926 去掉手写 gov_search 整站删除(抢锁源; FTS 由 gov_raw 触发器维护) '
pat_site = re.compile(r'DELETE\s+FROM\s+gov_search\s+WHERE\s+site_name\s*=', re.I)
pat_rowin = re.compile(r'DELETE\s+FROM\s+gov_search\s+WHERE\s+rowid\s+IN\s*\(', re.I)
done, skip, errs = [], [], []
for f in sorted(os.listdir(D)):
    if not (f.startswith('crawl_') and f.endswith('.py')):
        continue
    p = D + f
    try:
        src = open(p, encoding='utf-8').read()
    except Exception:
        continue
    lines = src.split('\n')
    changed = 0
    for i, ln in enumerate(lines):
        st = ln.strip()
        if st.startswith('#') or not st:
            continue
        if pat_site.search(ln) or pat_rowin.search(ln):
            indent = ln[:len(ln) - len(ln.lstrip())]
            lines[i] = indent + TAG + '\n' + indent + '# ' + ln.strip()
            changed += 1
    if not changed:
        continue
    new = '\n'.join(lines)
    try:
        ast.parse(new)
    except SyntaxError as e:
        errs.append((f, str(e)))
        continue
    b = ARC + f + '.bak_20260926_govsearch'
    if not os.path.exists(b):
        shutil.copy2(p, b)
    open(p, 'w', encoding='utf-8').write(new)
    done.append((f, changed))
print('已改脚本 %d 个 / 共注释 %d 处' % (len(done), sum(c for _, c in done)))
if errs:
    print('语法错未改:', errs)
ok = bad = 0
for f, _ in done:
    try:
        py_compile.compile(D + f, doraise=True, cfile='/tmp/pyc_' + f + 'c')
        ok += 1
    except Exception:
        bad += 1
print('py_compile: 通过 %d / 失败 %d' % (ok, bad))
print()
print('改动前 10 个:')
for f, c in done[:10]:
    print('  %-34s %d 处' % (f, c))
con = sqlite3.connect('file:/root/search.db?mode=ro', uri=True, timeout=60)
try:
    n_raw = con.execute('SELECT COUNT(*) FROM gov_raw').fetchone()[0]
except Exception as e:
    n_raw = -1
try:
    n_s = con.execute('SELECT COUNT(*) FROM gov_search').fetchone()[0]
except Exception as e:
    n_s = -1
print()
print('全库对照: gov_raw=%s  gov_search=%s  (FTS 应 >= raw)' % (n_raw, n_s))
print('结论:', 'OK FTS 未缺失' if (n_s >= n_raw and n_raw > 0) else '注意：需进一步核对')
