- 同步 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)
180 lines
5.8 KiB
Python
180 lines
5.8 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def path_matches_dir_names(path: str, dir_names: tuple) -> bool:
|
|
if not dir_names:
|
|
return False
|
|
wanted = {name.lower() for name in dir_names}
|
|
return any(part.lower() in wanted for part in Path(path).parts)
|
|
|
|
|
|
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(
|
|
args,
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
except Exception:
|
|
return ""
|
|
|
|
if completed.returncode not in (0, 1):
|
|
return ""
|
|
return completed.stdout
|
|
|
|
|
|
def _discover_files_via_os_walk(scan_dirs: list, allowed_exts: tuple, exclude_path_fragments, skip_dir_names: tuple = (), basename_prefixes: tuple = ()) -> list:
|
|
files = []
|
|
allowed_exts = set(allowed_exts)
|
|
for scan_dir in scan_dirs:
|
|
for root, dirs, filenames in os.walk(scan_dir):
|
|
dirs[:] = [
|
|
directory for directory in dirs
|
|
if not any(fragment.strip("/") in directory for fragment in exclude_path_fragments)
|
|
]
|
|
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]
|
|
if ext not in allowed_exts:
|
|
continue
|
|
name_no_ext = os.path.splitext(filename)[0]
|
|
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))
|
|
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(rel_file, skip_dir_names):
|
|
continue
|
|
files.append(full_path)
|
|
return sorted(set(files))
|
|
|
|
|
|
def _discover_files_via_rg(scan_dirs: list, allowed_exts: tuple, exclude_path_fragments, skip_dir_names: tuple = (), basename_prefixes: tuple = ()) -> list:
|
|
if not scan_dirs:
|
|
return []
|
|
rg = shutil.which("rg")
|
|
if not rg:
|
|
return []
|
|
|
|
cmd = [rg, "--files", "--no-ignore", "--hidden"]
|
|
for ext in allowed_exts:
|
|
cmd.extend(["-g", f"*{ext}"])
|
|
cmd.extend(scan_dirs)
|
|
|
|
output = _run_command_capture_stdout(cmd)
|
|
if not output:
|
|
return []
|
|
|
|
files = []
|
|
for line in output.splitlines():
|
|
path = os.path.normpath(line.strip())
|
|
if not path:
|
|
continue
|
|
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]
|
|
if not any(name_no_ext.startswith(prefix) for prefix in basename_prefixes):
|
|
continue
|
|
files.append(path)
|
|
|
|
return sorted(set(files))
|
|
|
|
|
|
def discover_files(scan_dirs: list, allowed_exts: tuple, exclude_path_fragments, skip_dir_names: tuple = (), basename_prefixes: tuple = ()) -> list:
|
|
if not scan_dirs:
|
|
return []
|
|
files = _discover_files_via_rg(
|
|
scan_dirs,
|
|
allowed_exts,
|
|
exclude_path_fragments,
|
|
skip_dir_names=skip_dir_names,
|
|
basename_prefixes=basename_prefixes,
|
|
)
|
|
if files:
|
|
return files
|
|
return _discover_files_via_os_walk(
|
|
scan_dirs,
|
|
allowed_exts,
|
|
exclude_path_fragments,
|
|
skip_dir_names=skip_dir_names,
|
|
basename_prefixes=basename_prefixes,
|
|
)
|
|
|
|
|
|
def _chunked(items: list, chunk_size: int):
|
|
for index in range(0, len(items), chunk_size):
|
|
yield items[index:index + chunk_size]
|
|
|
|
|
|
def prefilter_files_with_rg(files: list, pattern: str, chunk_size: int = 256) -> list:
|
|
files = sorted(set(files))
|
|
if not files:
|
|
return []
|
|
|
|
rg = shutil.which("rg")
|
|
if not rg:
|
|
return files
|
|
|
|
matched = set()
|
|
for chunk in _chunked(files, chunk_size):
|
|
try:
|
|
completed = subprocess.run(
|
|
[rg, "-l", "--no-messages", "-e", pattern, *chunk],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
except Exception:
|
|
return files
|
|
|
|
if completed.returncode not in (0, 1):
|
|
return files
|
|
for line in completed.stdout.splitlines():
|
|
path = os.path.normpath(line.strip())
|
|
if path:
|
|
matched.add(path)
|
|
|
|
return sorted(matched)
|