198 lines
8.6 KiB
Python
198 lines
8.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""界面体检 —— 总入口。
|
||
|
||
用法(主人一般只用两个按钮,不用命令行):
|
||
双击「界面及功能检测.bat」 = 体检:界面照片比底片 + 功能照检测单走一遍
|
||
双击「确认无误-更新底片.bat」 = 主人确认页面改版后,按最新页面重新留底
|
||
|
||
开发/试跑附加参数(AI 用):
|
||
--only sessions|workbench|admin 只跑一个页面(无视 enabled,用于试跑给主人看)
|
||
--update 更新底片
|
||
"""
|
||
import json
|
||
import os
|
||
import sys
|
||
import traceback
|
||
|
||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||
_PKG = os.path.dirname(os.path.abspath(__file__))
|
||
os.chdir(_PKG)
|
||
sys.path.insert(0, _PKG)
|
||
|
||
import config # noqa: E402
|
||
|
||
|
||
def _load_registry():
|
||
with open(os.path.join(_PKG, "页面清单.json"), encoding="utf-8") as f:
|
||
return json.load(f)
|
||
|
||
|
||
def seed(server, with_batch=True):
|
||
"""预置样例数据(全部在临时副本里,退出即删):2 个审核会话 + 1 个后台批次。
|
||
|
||
会话走真实用户路径:上传(进软件目录)→ 审核 —— 否则列表会显示"源文件缺失",
|
||
和主人日常看到的样子不一样。后台批次走批量导入目录,同真实流程。
|
||
"""
|
||
import base64
|
||
import cos4maker
|
||
# 甲:2 条分部分项 + 措施 → 会话(工作台/最近审核用)
|
||
pa = cos4maker.make_cos4(os.path.join(server.src_dir, "样例甲.cos4"),
|
||
"审核工程甲", [config.CAM, config.METER],
|
||
[config.LME_CAM, config.LME_CONC])
|
||
sid_a = _audit_via_upload(server, pa, "审核工程甲")
|
||
# 乙:1 条分部分项 → 第二个会话(列表/删除用)
|
||
pb = cos4maker.make_cos4(os.path.join(server.src_dir, "样例乙.cos4"),
|
||
"审核工程乙", [config.CAM], [config.LME_CAM])
|
||
_audit_via_upload(server, pb, "审核工程乙")
|
||
# 历史工程B:入库后台批次(后台列表 + 比对源;与甲的摄像机编码相同 → 类似清单有东西可出)
|
||
# with_batch=False:只造会话不造批次——工作台重造样例用,避开「同名同期」防重闸(2026-10-20)
|
||
pc = cos4maker.make_cos4(os.path.join(server.src_dir, "样例历史B.cos4"),
|
||
"历史工程B", [(config.CAM[0], config.CAM[1], config.CAM[2],
|
||
config.CAM[3], 8, 1800)],
|
||
[config.LME_CAM, config.LME_CONC])
|
||
if not with_batch:
|
||
return sid_a
|
||
r = server.post("/api/admin/import", dict(config.META, path=pc, name="历史工程B",
|
||
file_kind=1))
|
||
assert r.get("ok"), "造后台批次失败:%s" % r
|
||
return sid_a
|
||
|
||
|
||
def _audit_via_upload(server, file_path, name):
|
||
"""真实路径造会话:/api/admin/upload 落进软件目录 → /api/audit 开会话。"""
|
||
import base64
|
||
with open(file_path, "rb") as f:
|
||
raw = f.read()
|
||
up = server.post("/api/admin/upload", {
|
||
"name": os.path.basename(file_path),
|
||
"data": base64.b64encode(raw).decode("ascii")})
|
||
assert up.get("ok"), "上传样例失败:%s" % up
|
||
r = server.post("/api/audit", dict(config.AUDIT_BASE, path=up["path"], name=name))
|
||
assert r.get("ok"), "造会话 %s 失败:%s" % (name, r)
|
||
return r["session_id"]
|
||
|
||
|
||
def pick_browser(pw):
|
||
"""按 config.BROWSER_CHAIN 顺序找一台能用的浏览器。"""
|
||
ways = {
|
||
"chromium": lambda: pw.chromium.launch(headless=True, channel="chromium"),
|
||
"chromium-shell": lambda: pw.chromium.launch(headless=True),
|
||
"msedge": lambda: pw.chromium.launch(headless=True, channel="msedge"),
|
||
"chrome": lambda: pw.chromium.launch(headless=True, channel="chrome"),
|
||
}
|
||
last = None
|
||
for kind in config.BROWSER_CHAIN:
|
||
try:
|
||
return kind, ways[kind]()
|
||
except Exception as e: # 未安装/起不来就走下一个
|
||
last = e
|
||
raise RuntimeError("浏览器都起不来(%s):%s" % ("/".join(config.BROWSER_CHAIN), last))
|
||
|
||
|
||
def main():
|
||
argv = sys.argv[1:]
|
||
update = "--update" in argv
|
||
only = argv[argv.index("--only") + 1] if "--only" in argv else None
|
||
|
||
reg = _load_registry()
|
||
pages = reg["pages"]
|
||
if only:
|
||
name = pages.get(only, {}).get("名称", only)
|
||
todo = [(only, {"名称": "%s(试跑·未正式启用)" % name})]
|
||
else:
|
||
todo = [(pid, p) for pid, p in pages.items() if p.get("enabled")]
|
||
|
||
from report import Report
|
||
rep = Report(update_mode=update)
|
||
|
||
print("=" * 56)
|
||
print(" 界面及功能体检 %s" % ("【更新底片】" if update else "【体检对比】"))
|
||
print("=" * 56)
|
||
if not todo:
|
||
print("\n还没有已启用的页面。")
|
||
print("规矩(主人定的):页面一个一个点名 —— 主人说测哪个页面,")
|
||
print("AI 才把 页面清单.json 里那个页面 enabled 改 true,然后跑体检。")
|
||
for pid, p in pages.items():
|
||
print(" - %s(%s):未启用" % (p.get("名称", pid), pid))
|
||
|
||
server = None
|
||
browser = None
|
||
try:
|
||
from server_env import Server
|
||
out_dir = os.path.join(_PKG, config.OUT_DIR)
|
||
os.makedirs(out_dir, exist_ok=True)
|
||
log_path = os.path.join(out_dir, "server.log")
|
||
print("\n[1/4] 起隔离后台(临时副本,生产数据零接触)…")
|
||
server = Server(log_path)
|
||
server.__enter__()
|
||
print(" 后台端口 %d 就绪" % server.port)
|
||
print("[2/4] 预检接口 + 造样例数据 …")
|
||
st = server.get("/api/license/status")
|
||
assert st.get("ok") is True, "授权状态异常:%s" % st
|
||
server.get("/api/version")
|
||
n = server.get("/api/sessions")
|
||
assert isinstance(n.get("items"), list), "/api/sessions 结构异常"
|
||
sid_a = seed(server)
|
||
print(" 会话甲 #%d、会话乙、后台批次「历史工程B」就绪" % sid_a)
|
||
|
||
print("[3/4] 开浏览器逐页体检 …")
|
||
from playwright.sync_api import sync_playwright
|
||
with sync_playwright() as pw:
|
||
tag, browser = pick_browser(pw)
|
||
rep.browser_used = tag
|
||
print(" 浏览器:%s" % tag)
|
||
import pages_checks
|
||
for pid, p in todo:
|
||
print(" ▶ %s" % p.get("名称", pid))
|
||
if pid == "workbench":
|
||
# 「最近审核」页的删除测试会把样例会话删空 → 工作台前重造一轮样例,
|
||
# 否则 ?sid=N 开的是已删会话,顶栏工程名永远出不来(2026-10-20 实测真因)
|
||
sid_wb = seed(server, with_batch=False)
|
||
pages_checks.check_workbench(browser, server, rep, update, sid_wb)
|
||
else:
|
||
pages_checks.CHECKS[pid](browser, server, rep, update)
|
||
except SystemExit:
|
||
raise
|
||
except Exception:
|
||
print("\n!! 体检中途出错(工具本身的问题,不是页面的问题):")
|
||
traceback.print_exc()
|
||
rep.step("fail", "工具自身出错,本轮结果不完整", str(sys.exc_info()[1])[:200])
|
||
finally:
|
||
print("[4/4] 收尾:生成报告、清理临时后台 …")
|
||
if browser:
|
||
try:
|
||
browser.close()
|
||
except Exception:
|
||
pass
|
||
if server:
|
||
try:
|
||
server.__exit__(None, None, None)
|
||
except Exception:
|
||
pass
|
||
try:
|
||
import importlib.util as _ilu
|
||
_sp = os.path.join(_PKG, "清浏览器缓存.py")
|
||
_spec = _ilu.spec_from_file_location("clr_cache", _sp)
|
||
_m = _ilu.module_from_spec(_spec); _spec.loader.exec_module(_m)
|
||
_m.clear() # 2026-10-20 主人定:每次体检收尾自动清空浏览器缓存
|
||
except Exception:
|
||
pass
|
||
html_path, _md = rep.write(os.path.join(_PKG, config.OUT_DIR))
|
||
n_ok, n_warn, n_fail, steps_bad, shots_bad = rep.counts()
|
||
print("\n" + "=" * 56)
|
||
if not rep.pages:
|
||
print(" 体检完成:本轮没有已启用页面(等主人点名)。")
|
||
elif n_fail:
|
||
print(" 体检完成:❌ 有问题!%d 个页面(步骤错 %d、照片不符 %d)。" % (n_fail, steps_bad, shots_bad))
|
||
elif n_warn:
|
||
print(" 体检完成:⚠️ 有 %d 页存在轻微差异(详见报告)。" % n_warn)
|
||
else:
|
||
print(" 体检完成:✅ 全部通过(%d 页)。" % n_ok)
|
||
print(" 报告:%s" % html_path)
|
||
print("=" * 56)
|
||
sys.exit(0 if not n_fail else 1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|