VoyagerXHF's picture
Upload folder using huggingface_hub (part 108)
f7e6f7b verified
Raw History Blame Contribute Delete
8.88 kB
<#
build.ps1 - Self-contained build script for PyPlayer (a PyQt5 + VLC media player).
Reproduces the entire build from scratch:
1. Installs Python 3.11 (machine-wide, via Chocolatey) if missing.
2. Installs all Python dependencies machine-wide (requirements.txt + pinned
PyInstaller 5.13.2, which keeps the flat one-dir layout that the project's
runtime hook.py / constants.py expect).
3. Patches the project's executable/exclude.txt: it deletes Qt5Network.dll,
but in PyQt5 5.15.11 that DLL is a REQUIRED dependency of Qt5Multimedia.dll
and QtMultimedia.pyd (the app imports QtMultimedia/QtMultimediaWidgets), so
removing it makes the compiled app crash at startup. We keep it.
4. Drives the project's own executable/build.py (compile + post-processing)
non-interactively.
5. Copies the finished one-dir build to <task-root>\install.
Notes on dependency locations (recreation runs as an unprivileged user):
- Python 3.11 -> C:\Python311 (machine-wide, Chocolatey default).
- pip packages -> C:\Python311\Lib\site-packages (system site-packages; NO --user).
- No venv is created.
#>
$ErrorActionPreference = 'Stop'
# RB unified-0827: build_result.json is portable on OSS; materialize its path for this VM run.
$__rbBuildResultPath = Join-Path $PSScriptRoot 'build_result.json'
if (Test-Path $__rbBuildResultPath) {
$__rbBuildResult = Get-Content $__rbBuildResultPath -Raw -Encoding UTF8 | ConvertFrom-Json
if ($__rbBuildResult.executable -and -not [System.IO.Path]::IsPathRooted([string]$__rbBuildResult.executable)) {
$__rbBuildResult.executable = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot ([string]$__rbBuildResult.executable)))
$__rbBuildResult | ConvertTo-Json -Depth 10 | Set-Content $__rbBuildResultPath -Encoding UTF8
}
}
Set-StrictMode -Version Latest
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
$RepoDir = (Join-Path (Split-Path $PSScriptRoot -Parent) 'repo')
$InstallDir = (Join-Path (Split-Path $PSScriptRoot -Parent) 'install')
$BuildDir = (Join-Path (Split-Path $PSScriptRoot -Parent) 'build')
$ExeDir = Join-Path $RepoDir 'executable'
$PythonExe = 'C:\Python311\python.exe'
Write-Host '==================================================================='
Write-Host ' PyPlayer build script'
Write-Host '==================================================================='
Write-Host "Repo : $RepoDir"
Write-Host "Install : $InstallDir"
# ---------------------------------------------------------------------------
# 1. Ensure Python 3.11 is installed machine-wide
# ---------------------------------------------------------------------------
if (-not (Test-Path $PythonExe)) {
Write-Host "`n[1/6] Python 3.11 not found at $PythonExe - installing via Chocolatey..."
& choco install python311 -y --no-progress
if ($LASTEXITCODE -ne 0) { throw "choco install python311 failed (exit $LASTEXITCODE)" }
} else {
Write-Host "`n[1/6] Python 3.11 already present at $PythonExe"
}
if (-not (Test-Path $PythonExe)) {
# Fallback: try common alternate location / shim
$cand = @('C:\Python311\python.exe',
"$env:ProgramData\chocolatey\bin\python3.11.exe") |
Where-Object { Test-Path $_ } | Select-Object -First 1
if ($cand) { $PythonExe = $cand } else { throw 'Python 3.11 could not be located after install.' }
}
& $PythonExe --version
# ---------------------------------------------------------------------------
# 2. Install Python dependencies machine-wide (no venv, no --user)
# ---------------------------------------------------------------------------
Write-Host "`n[2/6] Installing Python dependencies (machine-wide site-packages)..."
& $PythonExe -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) { throw "pip upgrade failed (exit $LASTEXITCODE)" }
# PyInstaller is pinned to the last 5.x release: the project's runtime hook.py and
# constants.py assume PyInstaller 5.x's flat one-dir layout (files at the app root,
# not inside an _internal\ folder). 5.13.2 still satisfies requirements.txt (>=5.13.0).
& $PythonExe -m pip install "pyinstaller==5.13.2" -r (Join-Path $RepoDir 'requirements.txt')
if ($LASTEXITCODE -ne 0) { throw "pip install of requirements failed (exit $LASTEXITCODE)" }
# ---------------------------------------------------------------------------
# 3. Patch executable/exclude.txt so Qt5Network.dll is NOT deleted
# (required dependency of Qt5Multimedia.dll / QtMultimedia.pyd in PyQt5 5.15.x)
# ---------------------------------------------------------------------------
Write-Host "`n[3/6] Patching exclude.txt to keep Qt5Network.dll..."
$excludePath = Join-Path $ExeDir 'exclude.txt'
$lines = Get-Content -LiteralPath $excludePath
$patched = $lines | ForEach-Object {
if ($_ -match 'Qt5Network\.dll' -and $_.TrimStart() -notmatch '^(//|#|;)') {
"// $_ (kept: required by Qt5Multimedia in PyQt5 5.15.x)"
} else {
$_
}
}
Set-Content -LiteralPath $excludePath -Value $patched -Encoding ASCII
Write-Host ' exclude.txt patched.'
# ---------------------------------------------------------------------------
# 4. Clean previous build artifacts (build from scratch)
# ---------------------------------------------------------------------------
Write-Host "`n[4/6] Cleaning previous build artifacts..."
foreach ($d in @((Join-Path $ExeDir 'compiling'),
(Join-Path $ExeDir 'build'),
(Join-Path $ExeDir 'compiled'))) {
if (Test-Path $d) { Remove-Item -Recurse -Force $d }
}
if (Test-Path $InstallDir) { Remove-Item -Recurse -Force $InstallDir }
# $BuildDir is intentionally removed above so every run starts clean. Recreate
# it before writing the small Python driver used by the next stage.
New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null
# ---------------------------------------------------------------------------
# 5. Drive the project's executable/build.py non-interactively
# (build.py's __main__ has an interactive input() loop we must bypass)
# ---------------------------------------------------------------------------
Write-Host "`n[5/6] Compiling PyPlayer with PyInstaller (this can take a few minutes)..."
$driverPy = Join-Path $BuildDir '_run_build.py'
$driverSrc = @'
import os, sys, importlib.util
BUILD_PY = sys.argv[1]
spec = importlib.util.spec_from_file_location("pyplayer_build", BUILD_PY)
build = importlib.util.module_from_spec(spec)
spec.loader.exec_module(build) # top-level venv check is a no-op (no venv present)
print(">>> Running compile_windows()...", flush=True)
build.compile_windows() # PyInstaller compile + Windows post-processing
print(">>> Renaming compiling/release -> compiled...", flush=True)
os.renames(build.RELEASE_DIR, build.FINAL_RELEASE_DIR)
print(">>> Build finished:", build.FINAL_RELEASE_DIR, flush=True)
'@
Set-Content -LiteralPath $driverPy -Value $driverSrc -Encoding ASCII
Push-Location $ExeDir
try {
& $PythonExe $driverPy (Join-Path $ExeDir 'build.py')
if ($LASTEXITCODE -ne 0) { throw "build.py driver failed (exit $LASTEXITCODE)" }
} finally {
Pop-Location
}
$compiledDir = Join-Path $ExeDir 'compiled'
$compiledExe = Join-Path $compiledDir 'pyplayer.exe'
if (-not (Test-Path $compiledExe)) { throw "Expected executable not produced: $compiledExe" }
# Belt-and-suspenders: guarantee Qt5Network.dll is present in the output even if
# exclude.txt patching above was somehow bypassed.
$qtBin = Join-Path $compiledDir 'pyqt5\Qt5\bin'
$qtNetTarget = Join-Path $qtBin 'Qt5Network.dll'
if ((Test-Path $qtBin) -and (-not (Test-Path $qtNetTarget))) {
$qtNetSrc = 'C:\Python311\Lib\site-packages\PyQt5\Qt5\bin\Qt5Network.dll'
if (Test-Path $qtNetSrc) {
Write-Host ' Restoring Qt5Network.dll into build output...'
Copy-Item -LiteralPath $qtNetSrc -Destination $qtNetTarget -Force
}
}
# ---------------------------------------------------------------------------
# 6. Copy finished build to the install directory
# ---------------------------------------------------------------------------
Write-Host "`n[6/6] Installing to $InstallDir ..."
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
Copy-Item -Path (Join-Path $compiledDir '*') -Destination $InstallDir -Recurse -Force
$installedExe = Join-Path $InstallDir 'pyplayer.exe'
if (-not (Test-Path $installedExe)) { throw "Install failed: $installedExe not found" }
Write-Host "`n==================================================================="
Write-Host " BUILD SUCCEEDED"
Write-Host " Executable: $installedExe"
Write-Host "==================================================================="