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
@@ -4630,6 +4630,43 @@ _GLOBAL_WORKER_AHO = None
_GLOBAL_WORKER_SELECTOR_MAPPING = None
_GLOBAL_WORKER_SWIFT_SIGNATURE_MAPPING = None
_GLOBAL_WORKER_DIRECT_METHOD_MAPPING = None
_GLOBAL_WORKER_VERBOSE = False
def _set_fork_worker_globals(
mapping,
mapping_re,
mapping_aho,
objc_selector_mapping,
swift_signature_mapping,
direct_method_mapping,
verbose: bool,
):
global _GLOBAL_WORKER_MAPPING, _GLOBAL_WORKER_MAPPING_RE, _GLOBAL_WORKER_AHO
global _GLOBAL_WORKER_SELECTOR_MAPPING, _GLOBAL_WORKER_SWIFT_SIGNATURE_MAPPING
global _GLOBAL_WORKER_DIRECT_METHOD_MAPPING, _GLOBAL_WORKER_VERBOSE
_GLOBAL_WORKER_MAPPING = mapping
_GLOBAL_WORKER_MAPPING_RE = mapping_re
_GLOBAL_WORKER_AHO = mapping_aho
_GLOBAL_WORKER_SELECTOR_MAPPING = objc_selector_mapping
_GLOBAL_WORKER_SWIFT_SIGNATURE_MAPPING = swift_signature_mapping
_GLOBAL_WORKER_DIRECT_METHOD_MAPPING = direct_method_mapping
_GLOBAL_WORKER_VERBOSE = bool(verbose)
def _fork_archive_plan_and_write_worker(path: str):
"""archive_mode 下由 fork 子进程直接完成规划与原子写盘,绕过 GIL 并将写盘分散到多进程。"""
_, changed, stats, new_text, _ = plan_file_change(
path,
_GLOBAL_WORKER_MAPPING,
_GLOBAL_WORKER_MAPPING_RE,
objc_selector_mapping=_GLOBAL_WORKER_SELECTOR_MAPPING,
swift_signature_mapping=_GLOBAL_WORKER_SWIFT_SIGNATURE_MAPPING,
mapping_aho=_GLOBAL_WORKER_AHO,
method_direct_mapping=_GLOBAL_WORKER_DIRECT_METHOD_MAPPING,
)
if changed:
atomic_write_text(path, new_text, encoding="utf-8")
return path, True, (stats if _GLOBAL_WORKER_VERBOSE else None)
return path, False, None
def _pool_init(mapping_json_str: str, selector_mapping_json_str: str, swift_signature_mapping_json_str: str, direct_method_mapping_json_str: str):
global _GLOBAL_WORKER_MAPPING, _GLOBAL_WORKER_MAPPING_RE, _GLOBAL_WORKER_AHO, _GLOBAL_WORKER_SELECTOR_MAPPING, _GLOBAL_WORKER_SWIFT_SIGNATURE_MAPPING, _GLOBAL_WORKER_DIRECT_METHOD_MAPPING
@@ -5496,7 +5533,36 @@ def main():
print(f"🔁 总目标文件 {len(all_targets)},需处理 {len(files_to_process)}(基于缓存)")
if args.workers > 1 and len(files_to_process) >= 20:
already_written_in_workers = False
if args.archive_mode and not args.generate_patch_dir and args.workers > 1 and len(files_to_process) >= 20:
_set_fork_worker_globals(
mapping,
mapping_re,
mapping_aho,
objc_selector_mapping,
swift_signature_mapping,
method_direct_mapping,
args.verbose_changes,
)
try:
ctx = multiprocessing.get_context("fork")
with ctx.Pool(processes=args.workers) as pool:
for idx, (path, changed, stats) in enumerate(
pool.imap_unordered(_fork_archive_plan_and_write_worker, files_to_process, chunksize=8),
start=1,
):
if changed:
planned_changes[path] = {"stats": stats or {}}
total_changed += 1
if idx % 50 == 0:
print(f" ... 进度 {idx}/{len(files_to_process)}", flush=True)
already_written_in_workers = True
except (ValueError, OSError):
# 平台不支持 fork 时回退下方线程池分支
planned_changes.clear()
total_changed = 0
if not already_written_in_workers and args.workers > 1 and len(files_to_process) >= 20:
# Avoid returning full file contents over multiprocessing pipes in archive mode.
# Use threads and flush per-file patches to disk when --generate-patch-dir is used
# to reduce memory pressure and allow inspecting progress live.
@@ -5549,7 +5615,7 @@ def main():
planned_changes[path]['original_text'] = original_text
total_changed += 1
if idx % 50 == 0:
print(f" ... 进度 {idx}/{len(files_to_process)}")
print(f" ... 进度 {idx}/{len(files_to_process)}", flush=True)
if write_patches_live:
# finalize manifest
try:
@@ -5561,7 +5627,7 @@ def main():
_log_stage_duration("替换规划", planning_stage_started_at)
_log_stage_duration("本次执行总耗时", total_started_at)
return
else:
elif not already_written_in_workers:
for idx, path in enumerate(files_to_process, start=1):
path, changed, stats, new_text, original_text = plan_file_change(path, mapping, mapping_re, objc_selector_mapping, swift_signature_mapping, mapping_aho, method_direct_mapping)
if changed:
@@ -5573,7 +5639,7 @@ def main():
planned_changes[path]['original_text'] = original_text
total_changed += 1
if idx % 50 == 0:
print(f" ... 进度 {idx}/{len(files_to_process)}")
print(f" ... 进度 {idx}/{len(files_to_process)}", flush=True)
# update cache for processed files
for p in files_to_process:
@@ -5613,7 +5679,9 @@ def main():
write_stage_started_at = time.perf_counter()
changed_files = sorted(planned_changes.keys())
if args.archive_mode:
if already_written_in_workers:
print(" (已在多进程规划阶段完成原子写盘)")
elif args.archive_mode:
direct_write(planned_changes)
else:
transactional_write(planned_changes)
@@ -15,6 +15,33 @@ def should_exclude_path(path: str, exclude_path_fragments) -> bool:
return any(fragment in path for fragment in exclude_path_fragments)
def _rel_path_within_scan_dir(path: str, scan_dir: str) -> str:
"""将 path 转为相对 scan_dir 的路径,避免上层工作目录名(如 build/)触发误排除。"""
try:
rel = os.path.relpath(os.path.abspath(path), os.path.abspath(scan_dir))
except ValueError:
return path
if rel == ".":
return ""
if rel == ".." or rel.startswith(".." + os.sep):
return path
return rel
def _rel_path_for_filter(path: str, scan_dirs: list) -> str:
abs_path = os.path.abspath(path)
best_rel = None
best_len = -1
for scan_dir in scan_dirs:
abs_scan = os.path.abspath(scan_dir)
prefix = abs_scan.rstrip(os.sep) + os.sep
if abs_path == abs_scan or abs_path.startswith(prefix):
if len(abs_scan) > best_len:
best_len = len(abs_scan)
best_rel = _rel_path_within_scan_dir(abs_path, abs_scan)
return best_rel if best_rel is not None else path
def _run_command_capture_stdout(args: list) -> str:
try:
completed = subprocess.run(
@@ -40,7 +67,8 @@ def _discover_files_via_os_walk(scan_dirs: list, allowed_exts: tuple, exclude_pa
directory for directory in dirs
if not any(fragment.strip("/") in directory for fragment in exclude_path_fragments)
]
if path_matches_dir_names(root, skip_dir_names):
rel_root = _rel_path_within_scan_dir(root, scan_dir)
if path_matches_dir_names(rel_root, skip_dir_names):
continue
for filename in filenames:
ext = os.path.splitext(filename)[1]
@@ -50,9 +78,10 @@ def _discover_files_via_os_walk(scan_dirs: list, allowed_exts: tuple, exclude_pa
if basename_prefixes and not any(name_no_ext.startswith(prefix) for prefix in basename_prefixes):
continue
full_path = os.path.normpath(os.path.join(root, filename))
if should_exclude_path(full_path, exclude_path_fragments):
rel_file = _rel_path_within_scan_dir(full_path, scan_dir)
if should_exclude_path(rel_file, exclude_path_fragments):
continue
if path_matches_dir_names(full_path, skip_dir_names):
if path_matches_dir_names(rel_file, skip_dir_names):
continue
files.append(full_path)
return sorted(set(files))
@@ -77,9 +106,12 @@ def _discover_files_via_rg(scan_dirs: list, allowed_exts: tuple, exclude_path_fr
files = []
for line in output.splitlines():
path = os.path.normpath(line.strip())
if not path or should_exclude_path(path, exclude_path_fragments):
if not path:
continue
if path_matches_dir_names(path, skip_dir_names):
rel_path = _rel_path_for_filter(path, scan_dirs)
if should_exclude_path(rel_path, exclude_path_fragments):
continue
if path_matches_dir_names(rel_path, skip_dir_names):
continue
if basename_prefixes:
name_no_ext = os.path.splitext(os.path.basename(path))[0]