浏览代码

Improve PyInstaller detection in Qt build scripts

LiuYang 5 月之前
父节点
当前提交
53cc05f863
共有 2 个文件被更改,包括 33 次插入12 次删除
  1. 18 5
      scripts/build_qt_exe.ps1
  2. 15 7
      scripts/build_qt_exe.sh

+ 18 - 5
scripts/build_qt_exe.ps1

@@ -4,14 +4,23 @@ $RootDir = Split-Path -Parent $PSScriptRoot
 Set-Location $RootDir
 
 $PythonBin = if ($env:PYTHON_BIN) { $env:PYTHON_BIN } else { ".\.venv\Scripts\python.exe" }
-$PyInstallerBin = if ($env:PYINSTALLER_BIN) { $env:PYINSTALLER_BIN } else { ".\.venv\Scripts\pyinstaller.exe" }
 
 if (-not (Test-Path $PythonBin)) {
     Write-Error "Python executable not found: $PythonBin"
 }
 
-if (-not (Test-Path $PyInstallerBin)) {
-    Write-Error "PyInstaller executable not found: $PyInstallerBin`nInstall it with: uv pip install --python .\.venv\Scripts\python.exe pyinstaller"
+if ($env:PYINSTALLER_BIN) {
+    $PyInstallerBin = $env:PYINSTALLER_BIN
+    if (-not (Test-Path $PyInstallerBin)) {
+        Write-Error "PyInstaller executable not found: $PyInstallerBin"
+    }
+    $PyInstallerDescription = $PyInstallerBin
+} else {
+    & $PythonBin -c "import PyInstaller" *> $null
+    if ($LASTEXITCODE -ne 0) {
+        Write-Error "PyInstaller is not installed in: $PythonBin`nInstall it with: uv pip install --python .\.venv\Scripts\python.exe pyinstaller"
+    }
+    $PyInstallerDescription = "$PythonBin -m PyInstaller"
 }
 
 $Version = (& $PythonBin -c "from pathlib import Path; import tomllib; print(tomllib.loads(Path('pyproject.toml').read_text(encoding='utf-8'))['project']['version'])").Trim()
@@ -19,7 +28,7 @@ $ArchName = if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "x86"
 $MainOutput = "dist/mlog-tool-qt.exe"
 $VersionedOutput = "dist/mlog-tool-qt-v$Version-windows-$ArchName.exe"
 
-Write-Host "Building Qt executable with $PyInstallerBin"
+Write-Host "Building Qt executable with $PyInstallerDescription"
 
 $ResetQtPlatform = $false
 if (-not $env:QT_QPA_PLATFORM) {
@@ -28,7 +37,11 @@ if (-not $env:QT_QPA_PLATFORM) {
 }
 
 try {
-    & $PyInstallerBin --noconfirm --clean mlog_tool_qt.spec
+    if ($env:PYINSTALLER_BIN) {
+        & $PyInstallerBin --noconfirm --clean mlog_tool_qt.spec
+    } else {
+        & $PythonBin -m PyInstaller --noconfirm --clean mlog_tool_qt.spec
+    }
     if ($LASTEXITCODE -ne 0) {
         exit $LASTEXITCODE
     }

+ 15 - 7
scripts/build_qt_exe.sh

@@ -5,17 +5,25 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
 cd "$ROOT_DIR"
 
 PYTHON_BIN="${PYTHON_BIN:-./.venv/bin/python}"
-PYINSTALLER_BIN="${PYINSTALLER_BIN:-./.venv/bin/pyinstaller}"
 
 if [[ ! -x "$PYTHON_BIN" ]]; then
   echo "Python executable not found: $PYTHON_BIN" >&2
   exit 1
 fi
 
-if [[ ! -x "$PYINSTALLER_BIN" ]]; then
-  echo "PyInstaller executable not found: $PYINSTALLER_BIN" >&2
-  echo "Install it with: uv pip install --python ./.venv/bin/python pyinstaller" >&2
-  exit 1
+if [[ -n "${PYINSTALLER_BIN:-}" ]]; then
+  if [[ ! -x "$PYINSTALLER_BIN" ]]; then
+    echo "PyInstaller executable not found: $PYINSTALLER_BIN" >&2
+    exit 1
+  fi
+  PYINSTALLER_CMD=("$PYINSTALLER_BIN")
+else
+  if ! "$PYTHON_BIN" -c "import PyInstaller" >/dev/null 2>&1; then
+    echo "PyInstaller is not installed in: $PYTHON_BIN" >&2
+    echo "Install it with: uv pip install --python ./.venv/bin/python pyinstaller" >&2
+    exit 1
+  fi
+  PYINSTALLER_CMD=("$PYTHON_BIN" -m PyInstaller)
 fi
 
 VERSION="$("$PYTHON_BIN" -c "from pathlib import Path; import tomllib; print(tomllib.loads(Path('pyproject.toml').read_text(encoding='utf-8'))['project']['version'])")"
@@ -23,8 +31,8 @@ OS_NAME="$(uname -s | tr '[:upper:]' '[:lower:]')"
 ARCH_NAME="$(uname -m)"
 VERSIONED_OUTPUT="dist/mlog-tool-qt-v${VERSION}-${OS_NAME}-${ARCH_NAME}"
 
-echo "Building Qt executable with $PYINSTALLER_BIN"
-QT_QPA_PLATFORM="${QT_QPA_PLATFORM:-offscreen}" "$PYINSTALLER_BIN" --noconfirm --clean mlog_tool_qt.spec
+echo "Building Qt executable with ${PYINSTALLER_CMD[*]}"
+QT_QPA_PLATFORM="${QT_QPA_PLATFORM:-offscreen}" "${PYINSTALLER_CMD[@]}" --noconfirm --clean mlog_tool_qt.spec
 
 cp dist/mlog-tool-qt "$VERSIONED_OUTPUT"