#!/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 # ───────────────────────────────────────────────────────────────────────────── # 主扫描逻辑 # ───────────────────────────────────────────────────────────────────────────── 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(无 mapping/tpl) | {len(d1)} |", f"| D2 | 按钮死桩(未配编辑表单/该单据未配) | {len(d2)} |", f"| D3 | 长文本/富文本错配单行 input-text | {len(d3)} |", f"| D4 | 弹窗嵌整页(双确认取消风险) | {len(d4)} |", f"| D5 | 依赖 picker 后端(当前解析失败) | {len(d5)} |", f"| D6 | CRUD 页受后端不分页缺陷影响 | {len(d6)} |", 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-...)", ) args = parser.parse_args() print(f"[扫描目录] {args.schema_dir}") results = scan(args.schema_dir) print_summary(results) report_path = Path(args.report_out) report_path.parent.mkdir(parents=True, exist_ok=True) report_text = format_report(results, args.schema_dir) 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()