#!/usr/bin/env python3 """ EIP4 Schema 组件真实性审计基线扫描器 只读扫描,量化 6 类已确认缺陷,产出缺陷矩阵报告。 用法: python3 schema_reality_audit.py [--schema-dir ] [--report-out ] """ import argparse import json import os import re import sys from collections import defaultdict from datetime import datetime from pathlib import Path # ── 默认路径 ────────────────────────────────────────────────────────────────── DEFAULT_SCHEMA_DIR = ( "/Users/zhengwr/workspace/eip3/eip4-platform/platform-converter/转换产物/schema" ) DEFAULT_REPORT_OUT = ( "/Users/zhengwr/workspace/eip3/docs/F2-17-组件真实性审计基线.md" ) # ── 缺陷 1:人员列正则 ──────────────────────────────────────────────────────── PERSON_NAME_RE = re.compile( r"(?i)(USER_?ID|_USER$|CREATEBY|UPDATEBY|.*PEOPLE.*|.*PERSON.*|HANDLER|DRAFT)" ) PERSON_LABEL_KEYWORDS = ("人", "经办", "拟稿", "接收人", "负责人") # ── 缺陷 3:长文本字段正则 ──────────────────────────────────────────────────── LONGTEXT_NAME_RE = re.compile( r"(?i)(内容|描述|备注|说明|详情|正文|CONTENT|REMARK|MEMO|DESC|DETAIL)" ) # ───────────────────────────────────────────────────────────────────────────── # 通用递归工具 # ───────────────────────────────────────────────────────────────────────────── def walk_nodes(node): """深度优先遍历 JSON 节点,yield 每个 dict。""" if isinstance(node, dict): yield node for v in node.values(): yield from walk_nodes(v) elif isinstance(node, list): for item in node: yield from walk_nodes(item) def walk_strings(node): """递归 yield 所有字符串值。""" if isinstance(node, str): yield node elif isinstance(node, dict): for v in node.values(): yield from walk_strings(v) elif isinstance(node, list): for item in node: yield from walk_strings(item) def collect_form_fields(node): """从任意节点递归收集 form.body 里的 input-text 字段。""" results = [] for n in walk_nodes(node): if n.get("type") in ("form", "dialog") and "body" in n: for field in (n["body"] if isinstance(n["body"], list) else [n["body"]]): _collect_input_text(field, results) return results def _collect_input_text(node, out): if isinstance(node, dict): if node.get("type") == "input-text": out.append(node) for v in node.values(): _collect_input_text(v, out) elif isinstance(node, list): for item in node: _collect_input_text(item, out) # ───────────────────────────────────────────────────────────────────────────── # 各缺陷检测函数(返回 list[dict] 命中条目) # ───────────────────────────────────────────────────────────────────────────── def detect_defect1_person_column(schema) -> list: """缺陷 1:crud.columns 人员列裸显示 id。""" hits = [] for node in walk_nodes(schema): if node.get("type") != "crud": continue for col in node.get("columns", []): if not isinstance(col, dict): continue col_type = col.get("type", "text") # 已有 mapping 或 tpl 则认为已处理,跳过 if col_type in ("mapping", "tpl"): continue name = col.get("name", "") label = col.get("label", "") matched = False reason = "" if PERSON_NAME_RE.search(name): matched = True reason = f"name 命中正则: {name}" if not matched: for kw in PERSON_LABEL_KEYWORDS: if kw in str(label): matched = True reason = f"label 含关键字 '{kw}': {label}" break if matched: hits.append({"name": name, "label": label, "reason": reason}) return hits def detect_defect2_dead_button(schema) -> list: """缺陷 2:按钮死桩(未配编辑表单 / 该单据未配)。""" hits = [] for s in walk_strings(schema): if "未配编辑表单" in s or "该单据未配" in s: hits.append({"text": s[:120]}) return hits def detect_defect3_longtext_mismatch(schema) -> list: """缺陷 3:长文本/富文本错配单行 input-text。""" hits = [] for node in walk_nodes(schema): if node.get("type") != "input-text": continue name = node.get("name", "") label = node.get("label", "") combined = name + "|" + str(label) if LONGTEXT_NAME_RE.search(combined): hits.append({"name": name, "label": label}) return hits def detect_defect4_dialog_embed_page(schema) -> list: """缺陷 4:弹窗嵌整页(service + schemaApi 含 /api/page/schema/)。""" hits = [] for node in walk_nodes(schema): if node.get("type") != "service": continue schema_api = node.get("schemaApi", "") or node.get("schemaAPI", "") if isinstance(schema_api, dict): schema_api = schema_api.get("url", "") if isinstance(schema_api, str) and "/api/page/schema/" in schema_api: hits.append({"schemaApi": schema_api[:200]}) return hits def detect_defect5_picker(schema) -> list: """缺陷 5:依赖 picker 后端字段。同时收集 dialogid。""" hits = [] picker_guid_re = re.compile(r"/api/picker/([0-9a-f\-]{36})", re.IGNORECASE) for node in walk_nodes(schema): source = node.get("source", "") or "" if isinstance(source, dict): source = source.get("url", "") or "" if isinstance(source, str) and "/api/picker/" in source: m = picker_guid_re.search(source) dialog_id = m.group(1) if m else "unknown" hits.append({ "name": node.get("name", ""), "label": node.get("label", ""), "dialogId": dialog_id, "source": source[:200], }) return hits def detect_defect6_crud_no_pagination(schema) -> bool: """缺陷 6:body.type=='crud' 且有 api(全局后端不分页缺陷)。""" body = schema.get("body", {}) if isinstance(body, dict): if body.get("type") == "crud" and body.get("api"): return True elif isinstance(body, list): for item in body: if isinstance(item, dict) and item.get("type") == "crud" and item.get("api"): return True return False # ───────────────────────────────────────────────────────────────────────────── # 运行时已修项(审计盲区):静态产物里仍呈未修形态,但运行时已解决,不计真实缺口。 # 升级要点(EIP4-架构与缺口现状-2026-06-13 §4.3 / §7-2):D1/D4/D5/D6 改用 e2e 验证, # 静态计数仅供回归观察,不再当作覆盖缺口。D2/D3 仍是静态真缺口。 # ───────────────────────────────────────────────────────────────────────────── RUNTIME_RESOLVED = { "D1": "人员/引用列裸 id → 运行时 UserColumnResolver 解析任意引用实体的 _DISPLAY__(662828c+2a6c1d0)", "D4": "弹窗嵌整页 → 运行时 format=amis 剥 page 壳,弹窗只渲表单本体(a021f33)", "D5": "依赖 picker 后端 → SelectDialog 已弹窗化为 amis picker 多列对照,运行时 /api/picker 供列(9d92df2)", "D6": "CRUD 不分页 → 运行时 FlowController 分页翻译(_SEARCH 合成流)", } STATIC_REAL = { "D2": "按钮死桩(未配编辑表单/该单据未配)—— 仍是静态真缺口(诚实降级,待配 EDITFORM 或低代码补)", "D3": "长文本/富文本错配单行 input-text —— 富文本已大面修(37db794),残余按 e2e 复核", } # 覆盖缺口候选 uitype(§4.2「真丢失控件」)—— 频次统计重点关注,决定 §7-4 优先级。 LOSTLIKE_UITYPES = ("TwinColSelect", "PasswordField") # 高级模板结构标记(§4.2「高级模板类型缺失」)—— 精确探测 uitype/结构元素,**不可用子串**: # 早期子串口径(如 'editgrid')会误命中脚本注释文本(如「将添加按钮增加至EditGrid中」),把存量虚高。 # 实测精确口径(uitype= / )下,本客户库 1489 模板里这些高级结构**全为 0** # (editgrid/mastersubgrid/treegrid/subgrid=0,calendar/wf 仅个位且多为脚本文本)。 ADVANCED_TEMPLATE_MARKERS = { "EditGrid(子表)": (r'uitype="(?:Form)?EditGrid"', r"]"), "TreeGrid": (r'uitype="TreeGrid"', r"]"), "Calendar": (r'uitype="Calendar"', r"]"), "Wf/Workflow": (r'uitype="Wf"', r"]", r"]"), } _UITYPE_RE = re.compile(r'uitype\s*=\s*"([^"]+)"') _ROOT_TAG_RE = re.compile(r"<\s*([a-zA-Z][\w\-]*)") def _root_tag(xml: str) -> str: """取 XML 第一个元素标签名(root),忽略 声明与注释。""" s = re.sub(r"<\?.*?\?>", "", xml, flags=re.DOTALL) s = re.sub(r"", "", s, flags=re.DOTALL) m = _ROOT_TAG_RE.search(s) return m.group(1).lower() if m else "(none)" def uitype_template_frequency(dsn: str) -> dict: """ DB 视角的 uitype × 模板根类型 × 频次统计(§7-2)。 直接扫 view_template.xml(authoritative 存量真实用量),让 §4.2 的覆盖缺口能按真实频次排序。 返回: by_uitype: {uitype: {"occ": 出现次数, "tpls": 模板数}} by_root: {root_tag: 模板数} by_uitype_root:{(uitype, root): 模板数} advanced: {marker: 模板数} 高级模板结构存量 lostlike: {uitype: {"occ","tpls"}} §4.2 真丢失控件存量 total_templates, parsed """ import psycopg2 # 仅在 --db 模式按需导入 by_uitype = defaultdict(lambda: {"occ": 0, "tpls": 0}) by_root = defaultdict(int) by_uitype_root = defaultdict(int) advanced = defaultdict(int) total = 0 parsed = 0 conn = psycopg2.connect(dsn) try: conn.set_session(readonly=True, autocommit=True) cur = conn.cursor() cur.execute( "SELECT view_template_id, xml::text FROM eip3.view_template " "WHERE xml IS NOT NULL AND xml::text <> ''" ) for _tid, xml in cur.fetchall(): total += 1 if not xml: continue parsed += 1 root = _root_tag(xml) by_root[root] += 1 uitypes_in_tpl = set(_UITYPE_RE.findall(xml)) for ut in _UITYPE_RE.findall(xml): by_uitype[ut]["occ"] += 1 for ut in uitypes_in_tpl: by_uitype[ut]["tpls"] += 1 by_uitype_root[(ut, root)] += 1 for marker, pats in ADVANCED_TEMPLATE_MARKERS.items(): if any(re.search(p, xml, re.IGNORECASE | re.MULTILINE) for p in pats): advanced[marker] += 1 finally: conn.close() lostlike = {ut: by_uitype[ut] for ut in LOSTLIKE_UITYPES if ut in by_uitype} return { "by_uitype": dict(by_uitype), "by_root": dict(by_root), "by_uitype_root": dict(by_uitype_root), "advanced": dict(advanced), "lostlike": lostlike, "total_templates": total, "parsed": parsed, } def format_frequency_section(freq: dict) -> str: """把 uitype/模板频次统计渲染为 Markdown(接在主报告之后)。""" by_uitype = freq["by_uitype"] lines = [ "", "## 十、uitype × 模板类型 × 使用频次(DB 存量真实用量,§7-2)", "", f"> 源:`view_template.xml` 全量扫描({freq['parsed']}/{freq['total_templates']} 有 xml)。", f"> 用途:让 §4.2 的「高级模板类型」「真丢失控件」覆盖缺口按**存量真实频次**排序,而非按清单平铺。", "", "### 10.1 uitype 频次 Top(出现次数 / 涉及模板数)", "", "| uitype | 出现次数 | 模板数 |", "|--------|---------|--------|", ] ranked = sorted(by_uitype.items(), key=lambda kv: kv[1]["occ"], reverse=True) for ut, st in ranked[:25]: lines.append(f"| {ut} | {st['occ']} | {st['tpls']} |") lines += [ "", "### 10.2 模板根类型分布", "", "| 根元素 | 模板数 |", "|--------|--------|", ] for root, n in sorted(freq["by_root"].items(), key=lambda kv: kv[1], reverse=True): lines.append(f"| `{root}` | {n} |") lines += [ "", "### 10.3 高级模板结构存量(§4.2「高级模板类型缺失」按频次排序)", "", "| 结构标记 | 涉及模板数 |", "|---------|-----------|", ] for marker, n in sorted(freq["advanced"].items(), key=lambda kv: kv[1], reverse=True): lines.append(f"| {marker} | {n} |") lines += [ "", "### 10.4 §4.2「真丢失控件」存量(决定 §7-4 是否值得做)", "", "| 控件 uitype | 出现次数 | 模板数 | 结论 |", "|------------|---------|--------|------|", ] for ut in LOSTLIKE_UITYPES: st = freq["lostlike"].get(ut) if st: verdict = "有存量,需评估优先级" if st["tpls"] > 0 else "零存量,可不做" lines.append(f"| {ut} | {st['occ']} | {st['tpls']} | {verdict} |") else: lines.append(f"| {ut} | 0 | 0 | 零存量,可不做 |") lines.append("") return "\n".join(lines) # ───────────────────────────────────────────────────────────────────────────── # 主扫描逻辑 # ───────────────────────────────────────────────────────────────────────────── def get_page_type(schema) -> str: body = schema.get("body", {}) if isinstance(body, dict): return body.get("type", "unknown") if isinstance(body, list): types = [b.get("type", "") for b in body if isinstance(b, dict)] if "crud" in types: return "crud" if "form" in types: return "form" return "mixed" return "unknown" def scan(schema_dir: str) -> dict: schema_dir = Path(schema_dir) files = sorted(schema_dir.glob("*.json")) total = len(files) results = { "total": total, "crud_pages": 0, "form_pages": 0, "parse_errors": [], # 每类:{template_id: [hits...]} "d1": defaultdict(list), "d2": defaultdict(list), "d3": defaultdict(list), "d4": defaultdict(list), "d5": defaultdict(list), "d6": [], # list of template_ids "all_dialog_ids": set(), } for fp in files: tid = fp.stem try: with open(fp, "r", encoding="utf-8") as f: schema = json.load(f) except Exception as e: results["parse_errors"].append({"file": fp.name, "error": str(e)}) continue ptype = get_page_type(schema) if ptype == "crud": results["crud_pages"] += 1 elif ptype == "form": results["form_pages"] += 1 h1 = detect_defect1_person_column(schema) if h1: results["d1"][tid].extend(h1) h2 = detect_defect2_dead_button(schema) if h2: results["d2"][tid].extend(h2) h3 = detect_defect3_longtext_mismatch(schema) if h3: results["d3"][tid].extend(h3) h4 = detect_defect4_dialog_embed_page(schema) if h4: results["d4"][tid].extend(h4) h5 = detect_defect5_picker(schema) if h5: results["d5"][tid].extend(h5) for item in h5: if item["dialogId"] != "unknown": results["all_dialog_ids"].add(item["dialogId"]) if detect_defect6_crud_no_pagination(schema): results["d6"].append(tid) return results # ───────────────────────────────────────────────────────────────────────────── # 报告生成 # ───────────────────────────────────────────────────────────────────────────── def format_report(results: dict, schema_dir: str) -> str: now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") d1, d2, d3, d4, d5, d6 = ( results["d1"], results["d2"], results["d3"], results["d4"], results["d5"], results["d6"], ) all_dids = sorted(results["all_dialog_ids"]) lines = [ f"# F2-17 EIP4 前端组件真实性审计基线", f"", f"> 扫描时间: {now} ", f"> 扫描目录: `{schema_dir}` ", f"> 本报告由 `schema_reality_audit.py` 自动生成,只读扫描,可重复运行。", f"", f"## 一、汇总表", f"", f"| 指标 | 数值 |", f"|------|------|", f"| 总页数 | {results['total']} |", f"| CRUD 列表页 | {results['crud_pages']} |", f"| Form 表单页 | {results['form_pages']} |", f"| JSON 解析失败 | {len(results['parse_errors'])} |", f"", f"| 缺陷编号 | 缺陷描述 | 静态命中页数 | 类别 |", f"|---------|---------|---------|------|", f"| D1 | 人员/引用列裸显示 id | {len(d1)} | 🟢 运行时已修(审计盲区) |", f"| D2 | 按钮死桩(未配编辑表单/该单据未配) | {len(d2)} | 🔴 静态真缺口 |", f"| D3 | 长文本/富文本错配单行 input-text | {len(d3)} | 🟡 大面已修,残余 e2e 复核 |", f"| D4 | 弹窗嵌整页(双确认取消风险) | {len(d4)} | 🟢 运行时已修(审计盲区) |", f"| D5 | 依赖 picker 后端 | {len(d5)} | 🟢 已弹窗化(picker,审计盲区) |", f"| D6 | CRUD 页受后端不分页缺陷影响 | {len(d6)} | 🟢 运行时已修(审计盲区) |", f"", f"> **审计盲区说明(§4.3/§7-2)**:🟢 标记项的静态计数是产物快照,运行时已解决,**不计真实缺口**,改用 e2e 验。", ] for code, desc in RUNTIME_RESOLVED.items(): lines.append(f"> - {code}:{desc}") lines.append(f"") def section(title, data_dict, key_fn, top=10): lines.append(f"## {title}") lines.append(f"") lines.append(f"命中页数: **{len(data_dict)}**,以下展示前 {top} 个 templateId 及首条命中:") lines.append(f"") lines.append(f"| templateId | 命中详情 |") lines.append(f"|-----------|---------|") for tid, hits in list(data_dict.items())[:top]: detail = key_fn(hits[0]) if hits else "" # 截断避免表格过宽 detail = detail[:120].replace("|", "|").replace("\n", " ") lines.append(f"| `{tid}` | {detail} |") lines.append(f"") section( "二、D1 人员列裸显示 id", d1, lambda h: f"name={h.get('name','')} label={h.get('label','')} | {h.get('reason','')}", ) section( "三、D2 按钮死桩", d2, lambda h: h.get("text", ""), ) section( "四、D3 长文本字段错配", d3, lambda h: f"name={h.get('name','')} label={h.get('label','')}", ) section( "五、D4 弹窗嵌整页", d4, lambda h: f"schemaApi={h.get('schemaApi','')}", ) section( "六、D5 依赖 picker 后端", d5, lambda h: f"name={h.get('name','')} dialogId={h.get('dialogId','')}", ) lines.append(f"## 七、D6 受后端不分页缺陷影响的 CRUD 页(前 10)") lines.append(f"") lines.append(f"命中页数: **{len(d6)}**") lines.append(f"") lines.append(f"| templateId |") lines.append(f"|-----------|") for tid in d6[:10]: lines.append(f"| `{tid}` |") lines.append(f"") lines.append(f"## 八、Picker dialogId 去重清单") lines.append(f"") lines.append(f"共 **{len(all_dids)}** 个唯一 dialogId(后端修 picker 时需逐一验证),以下展示前 20 个:") lines.append(f"") lines.append(f"| # | dialogId |") lines.append(f"|---|---------|") for i, did in enumerate(all_dids[:20], 1): lines.append(f"| {i} | `{did}` |") lines.append(f"") if results["parse_errors"]: lines.append(f"## 九、JSON 解析失败文件") lines.append(f"") lines.append(f"| 文件名 | 错误 |") lines.append(f"|--------|------|") for e in results["parse_errors"]: lines.append(f"| {e['file']} | {str(e['error'])[:100]} |") lines.append(f"") return "\n".join(lines) def print_summary(results: dict): d1, d2, d3, d4, d5, d6 = ( results["d1"], results["d2"], results["d3"], results["d4"], results["d5"], results["d6"], ) print("=" * 56) print("EIP4 Schema 组件真实性审计 — 汇总") print("=" * 56) print(f" 总页数 : {results['total']}") print(f" CRUD 列表页 : {results['crud_pages']}") print(f" Form 表单页 : {results['form_pages']}") print(f" JSON 解析失败 : {len(results['parse_errors'])}") print("-" * 56) print(f" D1 人员列裸显示 id : {len(d1)} 页") print(f" D2 按钮死桩 : {len(d2)} 页") print(f" D3 长文本字段错配 : {len(d3)} 页") print(f" D4 弹窗嵌整页 : {len(d4)} 页") print(f" D5 依赖 picker : {len(d5)} 页") print(f" D6 CRUD 不分页受影响: {len(d6)} 页") print(f" Picker dialogId 数 : {len(results['all_dialog_ids'])}") print("=" * 56) # ───────────────────────────────────────────────────────────────────────────── # 入口 # ───────────────────────────────────────────────────────────────────────────── def main(): parser = argparse.ArgumentParser( description="EIP4 Schema 组件真实性审计基线扫描器(只读)" ) parser.add_argument( "--schema-dir", default=DEFAULT_SCHEMA_DIR, help="amis schema JSON 目录(默认: 转换产物/schema)", ) parser.add_argument( "--report-out", default=DEFAULT_REPORT_OUT, help="报告输出路径(Markdown,默认: docs/F2-17-...)", ) parser.add_argument( "--db", default=None, help="(§7-2)DB DSN,提供则附加 uitype×模板类型×频次统计(存量真实用量)。" "例: 'host=localhost port=5433 dbname=eip4dev user=zhengwr'", ) args = parser.parse_args() print(f"[扫描目录] {args.schema_dir}") results = scan(args.schema_dir) print_summary(results) report_text = format_report(results, args.schema_dir) if args.db: print(f"[频次统计] 连接 DB 扫 view_template.xml …") try: freq = uitype_template_frequency(args.db) report_text += "\n" + format_frequency_section(freq) print(f" uitype 种类: {len(freq['by_uitype'])} | 模板: {freq['parsed']}/{freq['total_templates']} " f"| 高级结构标记: {dict(sorted(freq['advanced'].items(), key=lambda kv: -kv[1]))}") print(f" 丢失控件存量: " + ", ".join( f"{ut}={freq['lostlike'].get(ut, {}).get('tpls', 0)}页" for ut in LOSTLIKE_UITYPES)) except Exception as e: print(f" [警告] 频次统计失败(跳过): {e}") report_path = Path(args.report_out) report_path.parent.mkdir(parents=True, exist_ok=True) with open(report_path, "w", encoding="utf-8") as f: f.write(report_text) print(f"\n[报告] {report_path}") if results["parse_errors"]: print(f"\n[警告] {len(results['parse_errors'])} 个文件解析失败:") for e in results["parse_errors"][:10]: print(f" {e['file']}: {e['error']}") if __name__ == "__main__": main()