#!/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"", html))) print(f" map.html div 配平: {o}/{c} {'OK' if o == c else '!!'}") ok = ok and o == c js = (stage / "static/js/map.js").read_text(encoding="utf-8") c1 = "const CLUSTER_ZOOM = 18" in js c2 = js.count("parseFloat(b.latitude) && parseFloat(b.longitude)") >= 2 c3 = "if (!isFinite(gx) || !isFinite(gy)) return;" in js print(f" 聚类阈值18: {'OK' if c1 else '!!'};NaN防线: {'OK' if c2 and c3 else '!!'}") ok = ok and c1 and c2 and c3 r2 = subprocess.run([sys.executable, "-m", "pytest", "--collect-only", "-q"], capture_output=True, text=True, cwd=str(stage)) tail = (r2.stdout or "").strip().splitlines() print(" pytest 收集:", tail[-1] if tail else "(无)") ok = ok and bool(tail and "test" in tail[-1].lower()) shutil.rmtree(stage, ignore_errors=True) print("冒烟结果:", "通过" if ok else "失败") sys.exit(0 if ok else 1)