#!/usr/bin/env python3 # -*- coding: utf-8 -*- """重打 1.zip:最新代码;排除安装包/数据/凭据/缓存。 完成后解压冒烟:路由 + DOM 配平 + 聚类 NaN 防线 + 测试可收集。""" import re import shutil import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).parent.parent ZIP = ROOT / "1.zip" EXCLUDE_DIR_PARTS = {"__pycache__", ".pytest_cache", ".reasonix", "backups", "artifacts", ".git", "downloads"} EXCLUDE_SUFFIX = (".pyc", ".pyo", ".log", ".tmp", ".bak", ".zip", ".db", ".db-wal", ".db-shm", ".pem", ".key", ".crt", ".exe", ".sqlite") EXCLUDE_STEMS = ("token_config", "smtp_config", "admin_config", "secret_key") def excluded(rel: Path) -> bool: if set(rel.parts) & EXCLUDE_DIR_PARTS: return True if rel.suffix.lower() in EXCLUDE_SUFFIX: return True stem = rel.name.lower() return stem == ".pack_baseline.json" or any(stem.startswith(p) for p in EXCLUDE_STEMS) files = [(p, p.relative_to(ROOT).as_posix()) for p in sorted(ROOT.rglob("*")) if p.is_file() and not excluded(p.relative_to(ROOT))] if ZIP.exists(): ZIP.unlink() with zipfile.ZipFile(ZIP, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as zf: for p, arc in files: zf.write(p, arc) zf = zipfile.ZipFile(ZIP) names = zf.namelist() print(f"OK 1.zip: {len(names)} 文件, {ZIP.stat().st_size / 1048576:.2f} MB") bad = [n for n in names if n.lower().endswith((".exe", ".db", ".pem", ".key", ".crt", ".zip", ".log")) or any(k in n.lower() for k in ("token_config", "smtp_config", "admin_config", "secret_key", "users.db", "pack_baseline"))] print("敏感审计:", ("!! " + str(bad)) if bad else "干净") zf.close() # ---------- 解压冒烟 ---------- stage = Path(tempfile.mkdtemp(prefix="xbniu_smoke_")) zipfile.ZipFile(ZIP).extractall(stage) ok = True probe = ( "import sys, tempfile\n" "from pathlib import Path\n" f"sys.path.insert(0, r'{stage}')\n" "import app as app_mod\n" "import db\n" "h = tempfile.NamedTemporaryFile(suffix='.db', delete=False); h.close()\n" "db.DB_PATH = Path(h.name)\n" "db.init_db()\n" "rules = sorted(str(x) for x in app_mod.app.url_map.iter_rules())\n" "need = ['/api/order/preview', '/api/order/add_round', '/api/map/freshness',\n" " '/api/history/stats', '/api/campus/locations']\n" "missing = [p for p in need if p not in rules]\n" "print('routes:', 'OK' if not missing else missing)\n" "sys.exit(1 if missing else 0)\n" ) r = subprocess.run([sys.executable, "-c", probe], capture_output=True, text=True, cwd=str(stage)) print(" ", (r.stdout or "").strip() or (r.stderr or "")[:200]) ok = ok and r.returncode == 0 html = (stage / "templates/map.html").read_text(encoding="utf-8") o, c = (len(re.findall(r"