feat: 修复混淆路径过滤与错误分类误报,并加速打包流程

- 同步 discovery.py 修复:相对 scan_dir 路径过滤,防止 ./build 目录下 Pods/ 第三方库保护失效
- 同步 obfuscate_symbols.py 多进程并行混淆写盘,build_service 启用 PYTHONUNBUFFERED=1 与 python3 -u 实时输出进度
- build_service 移除全新 derived_data 下多余的 xcodebuild clean,macOS APFS 优先使用 cp -c -R 零拷贝克隆源码
- Ad_Hoc 包按「分支 + 版本号」稳定复用 L1/LLVM seed 命中 obj_cache,App_Store 保持每次唯一
- 收紧 _classify_build_error 中 provisioning 关键词并补充协议遵循失败匹配,避免编译错误被误报为描述文件问题
- 更新与补充单元测试(226 passed)
This commit is contained in:
shenlei
2026-11-24 14:26:44 +09:00
parent b133b6c28d
commit dee9e3a4f2
5 changed files with 282 additions and 53 deletions
+83 -39
View File
@@ -173,16 +173,17 @@ _ERROR_RULES = [
"Code Sign error", "errSecInternalComponent", "CSSMERR_TP_NOT_TRUSTED",
"iPhone Distribution: no identity found", "iPhone Developer: no identity found"],
"certificate", "签名证书问题:证书过期、未安装或不匹配,请检查钥匙串中的证书"),
(["Provisioning profile", "No matching provisioning profiles",
"embedded.mobileprovision", "PROVISIONING_PROFILE_SPECIFIER",
"provisioning profile", "No matching",
"requires a provisioning profile"],
(["No matching provisioning profiles", "requires a provisioning profile",
"No profiles for", "provisioning profile has expired",
"doesn't include signing certificate", "doesn't match the entitlements",
"doesn't support the"],
"provisioning", "描述文件问题:描述文件过期、不匹配或未安装,请检查 Apple Developer 后台"),
(["Undefined symbols", "linker command failed", "ld: symbol(s) not found",
"ld: framework not found for"],
"compilation", "链接错误:符号未定义或框架缺失,请检查代码引用"),
(["Swift Compiler Error", "Use of undeclared identifier",
"cannot find type", "cannot find symbol in scope", "no such module"],
"cannot find type", "cannot find symbol in scope", "no such module",
"does not conform to protocol"],
"compilation", "Swift 编译错误:请检查代码语法和类型引用"),
([" error:", "fatal error:"],
"compilation", "代码编译错误:请检查代码是否有语法或类型错误"),
@@ -673,6 +674,29 @@ async def run_build_task(task_id: str):
db.close()
def _dir_contains_excluded(src: Path) -> bool:
for _, dirs, files in os.walk(src):
if ".git" in dirs or ".git" in files or "DerivedData" in dirs:
return True
return False
def _copy_dir_fast(src: Path, dst: Path) -> None:
"""macOS APFS 上优先用 cp -c -R(Copy-on-Write 零拷贝)克隆目录,含排除项或失败时回退 shutil.copytree。"""
if sys.platform == "darwin" and not _dir_contains_excluded(src):
proc = subprocess.run(
["cp", "-c", "-R", str(src), str(dst)],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if proc.returncode == 0:
return
if dst.exists():
shutil.rmtree(dst, ignore_errors=True)
shutil.copytree(src, dst, symlinks=True, ignore=shutil.ignore_patterns(".git", "DerivedData"))
async def copy_source_code(task_id: str, task, source_dir: Path, definition: dict = None) -> Path:
"""从分支源码目录拷贝代码到打包目录"""
await log_streamer.emit_step(task_id, "拷贝代码")
@@ -684,20 +708,33 @@ async def copy_source_code(task_id: str, task, source_dir: Path, definition: dic
if build_dir.exists():
shutil.rmtree(build_dir)
# 未指定拷贝条目时拷贝整个仓库(不含 Git 数据和编译缓存)
# 未指定拷贝条目时拷贝整个仓库(不含 Git 数据、DerivedData 与根 build 目录)
copy_items = definition.get("copy_items") or []
if not copy_items:
await log_streamer.emit(task_id, "拷贝整个仓库")
root = str(source_dir)
if sys.platform == "darwin":
build_dir.mkdir(parents=True)
for entry in source_dir.iterdir():
if entry.name in (".git", "DerivedData", "build"):
continue
dst = build_dir / entry.name
if entry.is_symlink():
os.symlink(os.readlink(entry), dst)
elif entry.is_dir():
_copy_dir_fast(entry, dst)
elif entry.is_file():
shutil.copy2(entry, dst)
else:
root = str(source_dir)
def _ignore(directory, names):
ignored = {name for name in names if name in (".git", "DerivedData")}
# 仓库根目录的 build 与打包产物目录同名,不拷贝
if directory == root and "build" in names:
ignored.add("build")
return ignored
def _ignore(directory, names):
ignored = {name for name in names if name in (".git", "DerivedData")}
# 仓库根目录的 build 与打包产物目录同名,不拷贝
if directory == root and "build" in names:
ignored.add("build")
return ignored
shutil.copytree(source_dir, build_dir, symlinks=True, ignore=_ignore)
shutil.copytree(source_dir, build_dir, symlinks=True, ignore=_ignore)
await log_streamer.emit(task_id, "代码拷贝完成")
return build_dir
@@ -717,7 +754,7 @@ async def copy_source_code(task_id: str, task, source_dir: Path, definition: dic
dst = build_dir / item
if src.is_dir():
await log_streamer.emit(task_id, f"拷贝目录: {item}")
shutil.copytree(src, dst, symlinks=True, ignore=shutil.ignore_patterns(".git", "DerivedData"))
_copy_dir_fast(src, dst)
elif src.is_file():
await log_streamer.emit(task_id, f"拷贝文件: {item}")
shutil.copy2(src, dst)
@@ -1004,6 +1041,31 @@ def _resolve_obfuscation_dir(build_dir: Path) -> Path:
return AUTOMATION_DIR / "obfuscation"
def _sanitize_seed_part(raw: str, fallback: str) -> str:
cleaned = re.sub(r"[^A-Za-z0-9]+", "_", raw).strip("_")
return cleaned or fallback
def _resolve_release_seed(task_id: str, config_data: dict) -> str:
"""计算 L1 混淆盐值与 LLVM IR seed。
- 显式传入环境变量或配置 IG_RELEASE_SEED 时优先使用;
- App_Store 上架包每次使用唯一 task_id,保证每次上架符号表不同;
- Ad_Hoc 等非上架包按「分支 + 版本号」生成稳定 seed(仅含单下划线和字母数字,
不含 `__` 以防破坏 `-DIG_OBF_CTL_...__$(IG_RELEASE_SEED)__...=1` 宏分隔),
让同分支同版本连续测试包复用 L1 映射并命中 LLVM obj_cache。
"""
explicit = os.environ.get("IG_RELEASE_SEED", "").strip() or str(config_data.get("IG_RELEASE_SEED") or "").strip()
if explicit:
return explicit
build_version = str(config_data.get("BUILD_VERSION") or "0")
if config_data.get("BUILD_TYPE") == "App_Store":
return f"{task_id}_{build_version}"
branch = _sanitize_seed_part(str(config_data.get("SOURCE_BRANCH") or ""), "dev")
ver = _sanitize_seed_part(build_version, "0")
return f"adhoc_{branch}_{ver}"
async def run_obfuscation(task_id: str, task, config_data: dict, build_dir: Path):
"""执行代码混淆"""
await log_streamer.emit_step(task_id, "代码混淆")
@@ -1019,7 +1081,8 @@ async def run_obfuscation(task_id: str, task, config_data: dict, build_dir: Path
env = os.environ.copy()
env["TARGET_NAME"] = config_data.get("SCHEME", "")
env["SKIP_OBF_PHASE"] = "1"
seed = os.environ.get("IG_RELEASE_SEED") or config_data.get("IG_RELEASE_SEED") or f"{task_id}_{config_data.get('BUILD_VERSION', '0')}"
env["PYTHONUNBUFFERED"] = "1"
seed = _resolve_release_seed(task_id, config_data)
env["OBF_SALT"] = seed
# 自检
self_check_script = obf_root / "scripts" / "obfuscation_self_check.sh"
@@ -1043,10 +1106,10 @@ async def run_obfuscation(task_id: str, task, config_data: dict, build_dir: Path
await log_streamer.emit(task_id, "混淆自检通过")
# 执行混淆
# 执行混淆(-u 关闭子进程 stdout 缓冲,保证网页日志实时刷出进度)
obfuscate_script = obf_root / "obfuscate_symbols.py"
process = await asyncio.create_subprocess_exec(
"python3", str(obfuscate_script), "--archive-mode", "--salt", seed,
"python3", "-u", str(obfuscate_script), "--archive-mode", "--salt", seed,
cwd=str(build_dir),
env=env,
stdout=asyncio.subprocess.PIPE,
@@ -1088,7 +1151,7 @@ async def _prepare_iosguard_build_env(task_id: str, config_data: dict, build_dir
all_off = "1" if os.environ.get("IG_ALL_GUARD_OFF") == "1" else "0"
raw_mode = (os.environ.get("IG_OBF_MODE") or config_data.get("IG_OBF_MODE") or DEFAULT_IG_OBF_MODE).strip() or "full"
effective_mode = "off" if all_off == "1" else raw_mode
seed = os.environ.get("IG_RELEASE_SEED") or config_data.get("IG_RELEASE_SEED") or f"{task_id}_{config_data.get('BUILD_VERSION', '0')}"
seed = _resolve_release_seed(task_id, config_data)
obj_cache_override = os.environ.get("IG_OBF_OBJ_CACHE_DIR", "").strip()
sdk_cache_dir = Path(os.environ.get("IG_SDK_CACHE_DIR") or (IG_CACHE_DIR / "sdk_cache"))
@@ -1397,26 +1460,7 @@ async def build_project(
if scheme in ("readoor31", "readoor31OtherPay"):
await _verify_dist_channel_flag(task_id, container_args, scheme, build_dir, build_env)
# 清理
await log_streamer.emit(task_id, "清理项目...")
process = await asyncio.create_subprocess_exec(
"xcodebuild", "clean",
*container_args,
"-scheme", scheme,
"-configuration", "Release",
"-derivedDataPath", str(export_path / "derived_data"),
cwd=str(build_dir),
env=build_env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
)
async for line in process.stdout:
decoded = line.decode("utf-8", errors="replace").strip()
if decoded:
await log_streamer.emit(task_id, decoded)
await process.wait()
# Archive
# Archive(每次任务的 derived_data 为全新空目录,跳过多余的 xcodebuild clean)
await log_streamer.emit(task_id, "开始 Archive...")
archive_started_at = time.monotonic()
archive_output = []