modify: 打包错误提示

This commit is contained in:
2026-08-05 14:48:57 +08:00
parent cc926c8bd3
commit 42aa007253
+33 -5
View File
@@ -96,11 +96,12 @@ def _run_cmd(
cwd: str | None = None, cwd: str | None = None,
capture: bool = True, capture: bool = True,
shell: bool | None = None, shell: bool | None = None,
echo_on_error: bool = True,
) -> subprocess.CompletedProcess: ) -> subprocess.CompletedProcess:
"""统一执行外部命令""" """统一执行外部命令"""
if shell is None: if shell is None:
shell = is_windows shell = is_windows
return subprocess.run( result = subprocess.run(
cmd, cmd,
cwd=cwd, cwd=cwd,
capture_output=capture, capture_output=capture,
@@ -109,6 +110,30 @@ def _run_cmd(
encoding="utf-8", encoding="utf-8",
errors="replace", errors="replace",
) )
# 命令失败时自动回显捕获的输出,便于定位失败原因
if echo_on_error and capture and result.returncode != 0:
_echo_captured_output(cmd, result)
return result
def _echo_captured_output(
cmd: list[str], result: subprocess.CompletedProcess
) -> None:
"""命令失败时回显被捕获的输出(截取末尾若干行,避免刷屏)"""
max_lines = 40
print(f"\n ── 命令执行失败 (exit={result.returncode}): {' '.join(cmd)} ──")
for stream_name, stream in (("stdout", result.stdout), ("stderr", result.stderr)):
if not stream or not stream.strip():
continue
lines = stream.rstrip("\n").splitlines()
tail = lines[-max_lines:]
header = f" [{stream_name}]"
if len(lines) > max_lines:
header += f" (共 {len(lines)} 行,显示末尾 {len(tail)} 行)"
print(header)
for line in tail:
print(f" {line}")
print(" ────────────────────────────────────────\n")
def _format_size(path: str | Path) -> str: def _format_size(path: str | Path) -> str:
@@ -270,7 +295,7 @@ def _stage_pyinstaller(config: Config) -> tuple[str, str]:
result = _run_cmd(cmd, cwd="build", capture=True) result = _run_cmd(cmd, cwd="build", capture=True)
if result.returncode != 0: if result.returncode != 0:
return f"❌ PyInstaller 失败", "" return f"❌ PyInstaller 失败 (exit={result.returncode}),详情见上方输出", ""
exe_path = Path("build/chongming") exe_path = Path("build/chongming")
if is_windows: if is_windows:
@@ -336,10 +361,13 @@ def _stage_nuitka(config: Config) -> tuple[str, str]:
if icon.exists(): if icon.exists():
cmd.append(f"--windows-icon-from-ico={icon}") cmd.append(f"--windows-icon-from-ico={icon}")
try: result = _run_cmd(cmd, cwd="build", capture=False)
_run_cmd(cmd, cwd="build", capture=False) if result.returncode != 0:
except subprocess.CalledProcessError:
# 降级重试(移除可能不兼容的参数) # 降级重试(移除可能不兼容的参数)
print(
f" ⚠️ Nuitka 首次编译失败 (exit={result.returncode}),"
f"尝试移除不兼容参数重试..."
)
fallback = [ fallback = [
arg for arg in cmd if arg not in ("--lto=yes", "--enable-plugin=upx") arg for arg in cmd if arg not in ("--lto=yes", "--enable-plugin=upx")
] ]