#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fix_govsearch_rebuild.py — 重建语句改 INSERT OR REPLACE（删 DELETE 才成立）
import ast, os, py_compile, re, shutil
D = '/root/gov_crawler/'
ARC = D + 'Archive/'
os.makedirs(ARC, exist_ok=True)
pat = re.compile(r'INSERT\s+INTO\s+gov_search\s*\(', re.I)
done, 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
    if not pat.search(src):
        continue
    if re.search(r'INSERT\s+OR\s+REPLACE\s+INTO\s+gov_search', src, re.I):
        continue
    new = pat.sub('INSERT OR REPLACE INTO gov_search(', src)
    try:
        ast.parse(new)
    except SyntaxError as e:
        errs.append((f, str(e)))
        continue
    b = ARC + f + '.bak_20260926_orreplace'
    if not os.path.exists(b):
        shutil.copy2(p, b)
    open(p, 'w', encoding='utf-8').write(new)
    done.append(f)
print('已改 %d 个脚本（INSERT INTO gov_search → INSERT OR REPLACE）' % len(done))
for f in done[:15]:
    print('  ', f)
if errs:
    print('语法错:', errs)
ok = bad = 0
for f in done:
    try:
        py_compile.compile(D + f, doraise=True, cfile='/tmp/p_%s_c' % f)
        ok += 1
    except Exception:
        bad += 1
print('py_compile 通过 %d / 失败 %d' % (ok, bad))
