#!/usr/bin/env python3 """ run_batch_regression.py — F2-4 全量批量回归 harness 范围:FULL_AUTO(507) + NO_S3(815) 共 1,322 条 端点:POST /api/script/execute-template {templateId,seq,item} 登录 /api/auth/login {account,password} 取 JWT item 策略: - 数据库 192.168.20.68:5435 若可达,按 template_id → view_templateentity → 抽 1 行真实数据 - 不可达则 item={} (空对象),结果在 bucket 中用 "no_item_data" 注记 安全排除(写路径): - 源码正文(去头注释后)含 excuteFlow/.save/.update/.delete 调用模式 → 跳过执行, bucket="excluded_write_path" 闭包动词专项(F2-3): - dynamic_button 规则对应的 4 条 FULL_AUTO 脚本逐条跑 - 断言 commands 含 addFieldButton/onClick 嵌套结构 输出: - tools/s3_rewriter/output/batch_regression_{timestamp}.json - docs/P1/F2-4批量回归报告.md """ import json import os import re import sys import time import urllib.request import urllib.error from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed from datetime import datetime from pathlib import Path # ───────────────────────────────────────────── # 配置 # ───────────────────────────────────────────── BASE_URL = "http://localhost:8093" LOGIN_ACCOUNT = "admin" LOGIN_PASSWORD = "111111" REWRITTEN_ROOT = Path(__file__).parent / "output" / "rewritten" CONCURRENCY = 6 # ≤6 并发 # 写路径安全排除正则(匹配脚本正文中的写操作调用) WRITE_PATTERNS = re.compile( r'\bexcuteFlow\s*\(' r'|(? str: lines = raw.split("\n") start = 0 while start < len(lines): t = lines[start].strip() if not t or t.startswith("//"): start += 1 else: break return "\n".join(lines[start:]).strip() # ───────────────────────────────────────────── # 认证 # ───────────────────────────────────────────── def get_jwt() -> str: payload = json.dumps({"account": LOGIN_ACCOUNT, "password": LOGIN_PASSWORD}).encode() req = urllib.request.Request( f"{BASE_URL}/api/auth/login", data=payload, headers={"Content-Type": "application/json"}, method="POST", ) with urllib.request.urlopen(req, timeout=15) as resp: data = json.loads(resp.read()) if not data.get("success"): raise RuntimeError(f"登录失败: {data.get('msg')}") print(f" JWT 获取成功 (userId={data.get('userId')}, expires={data.get('expireSeconds')}s)") return data["token"] # ───────────────────────────────────────────── # 数据库 item 抽取(可选) # ───────────────────────────────────────────── def try_get_db_item(tid: str, db_conn) -> dict: """ 尝试从 view_templateentity 反查主实体表名,再抽 1 行真实数据。 失败返回 {}。 """ if db_conn is None: return {} try: cur = db_conn.cursor() # 查主实体(第一个实体) cur.execute( "SELECT entity_name FROM eip3.view_templateentity " "WHERE view_template_id=%s ORDER BY sort_num LIMIT 1", (tid,) ) row = cur.fetchone() if not row: return {} table_name = row[0].strip().upper() if not re.match(r'^[A-Z][A-Z0-9_]{1,63}$', table_name): return {} cur.execute(f"SELECT * FROM eip3.{table_name} LIMIT 1") cols = [desc[0].upper() for desc in cur.description] data_row = cur.fetchone() if not data_row: return {} return dict(zip(cols, [str(v) if v is not None else None for v in data_row])) except Exception: return {} def connect_db(): try: import psycopg2 conn = psycopg2.connect( host="192.168.20.68", port=5435, dbname="postgres", user="ceshi", password="ceshi", connect_timeout=5, options="-c search_path=eip3", ) print(" 数据库连接成功,将抽取真实行数据作 item") return conn except Exception as e: print(f" 数据库不可达 ({e}),item 将使用 {{}}") return None # ───────────────────────────────────────────── # 脚本发现 # ───────────────────────────────────────────── def discover_scripts(labels=("FULL_AUTO", "NO_S3")): """遍历 rewritten/ 目录,返回 (tid, seq, label, file_path) 列表。""" items = [] label_set = set(labels) for tid_dir in sorted(REWRITTEN_ROOT.iterdir()): if not tid_dir.is_dir(): continue tid = tid_dir.name for fpath in sorted(tid_dir.iterdir()): if not fpath.name.endswith(".groovy"): continue # 文件名格式: .