#!/usr/bin/env python3
"""Add Archive scan to validate_crawler_config.py orphan detection"""
import re

PATH = '/root/gov_crawler/validate_crawler_config.py'

with open(PATH, 'r') as f:
    content = f.read()

# The skip_list line is at:     skip_list = {"crawl_TEMPLATE.py", ...}
# We need to add Archive scanning AFTER it
old = '    skip_list = {"crawl_TEMPLATE.py", "crawl_yidu_backup.py", "crawl_scheduler_v2.py"}\n    config_script_names'
new = '    skip_list = {"crawl_TEMPLATE.py", "crawl_yidu_backup.py", "crawl_scheduler_v2.py"}\n    # 扫描Archive目录——已主动归档的脚本跳过孤儿检测\n    archive_dir = os.path.join(GOV_DIR, "Archive")\n    if os.path.isdir(archive_dir):\n        for fname in os.listdir(archive_dir):\n            if fname.startswith("crawl_") and (fname.endswith(".py") or fname.endswith(".js")):\n                skip_list.add(fname)\n    config_script_names'

if old in content:
    content = content.replace(old, new, 1)
    with open(PATH, 'w') as f:
        f.write(content)
    print("Patch applied to validate_crawler_config.py")
else:
    print("WARNING: Marker not found in validate_crawler_config.py!")

# Verify syntax
import py_compile
py_compile.compile(PATH, doraise=True)
print("Syntax OK")
