<# build.ps1 - Self-contained build script for "Automatic Thresholding" Reproduces the entire build from scratch on a clean Windows machine: 1. Installs Python 3.10.11 machine-wide (official python.org installer) 2. Installs the pinned Python dependencies + PyInstaller into the GLOBAL system site-packages (readable by any user) 3. Packages AutomaticThresholding.py into a single-file GUI .exe with PyInstaller, output to \install The app is a PySide2 (Qt5) desktop GUI. PySide2 5.15.2.1 (and the pinned numpy/scipy versions) only ship wheels up to CPython 3.10, so a dedicated Python 3.10 is installed rather than using any newer system Python. Safe to re-run: each step is idempotent / skips work already done. #> $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') $DownloadDir = (Join-Path (Split-Path $PSScriptRoot -Parent) 'downloads') $PyHome = 'C:\Python310' $PyExe = Join-Path $PyHome 'python.exe' $AppScript = Join-Path $RepoDir 'AutomaticThresholding.py' $IconFile = Join-Path $RepoDir 'icon.ico' $AppName = 'AutomaticThresholding' $PyVersion = '3.10.11' $PyInstaller = "python-$PyVersion-amd64.exe" $PyUrl = "https://www.python.org/ftp/python/$PyVersion/$PyInstaller" Write-Host "==> Automatic Thresholding build starting" New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null New-Item -ItemType Directory -Force -Path $DownloadDir | Out-Null # --------------------------------------------------------------------------- # 1. Install Python 3.10.11 machine-wide (if not already present) # --------------------------------------------------------------------------- if (Test-Path $PyExe) { Write-Host "==> Python already present at $PyExe" } else { Write-Host "==> Downloading Python $PyVersion ..." $installerPath = Join-Path $DownloadDir $PyInstaller if (-not (Test-Path $installerPath)) { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri $PyUrl -OutFile $installerPath -UseBasicParsing } Write-Host "==> Installing Python $PyVersion machine-wide to $PyHome ..." # NOTE: The python.org bundle is a WiX "burn" bootstrapper. Launching it # directly from a non-interactive shell can hang before it spawns its # child MSIs. Running it through Start-Process -Wait launches the process # tree correctly and blocks until every MSI finishes. $pyArgs = @( '/quiet', 'InstallAllUsers=1', # machine-wide -> global site-packages "TargetDir=$PyHome", 'PrependPath=0', 'Include_pip=1', 'Include_test=0', 'Include_launcher=0', 'Shortcuts=0' ) $proc = Start-Process -FilePath $installerPath -ArgumentList $pyArgs -Wait -PassThru if ($proc.ExitCode -ne 0) { throw "Python installer failed with exit code $($proc.ExitCode)" } if (-not (Test-Path $PyExe)) { throw "Python installation did not produce $PyExe" } Write-Host "==> Python installed." } & $PyExe --version # --------------------------------------------------------------------------- # 2. Install Python dependencies into GLOBAL system site-packages # (no --user, no venv -> readable by the unprivileged recreation user) # --------------------------------------------------------------------------- Write-Host "==> Upgrading pip ..." & $PyExe -m pip install --upgrade pip --no-warn-script-location Write-Host "==> Installing pinned dependencies + PyInstaller ..." $deps = @( 'numpy==1.23.4', 'scipy==1.9.3', 'PySide2==5.15.2.1', 'shiboken2==5.15.2.1', 'matplotlib==3.6.2', 'scikit-image==0.19.3', 'Pillow==9.3.0', 'imageio==2.22.4', 'tifffile==2022.10.10', 'PyWavelets==1.4.1', 'networkx==2.8.8', 'contourpy==1.0.6', 'cycler==0.11.0', 'fonttools==4.38.0', 'kiwisolver==1.4.4', 'packaging==21.3', 'pyparsing==3.0.9', 'python-dateutil==2.8.2', 'six==1.16.0', 'pyinstaller' ) & $PyExe -m pip install --no-warn-script-location @deps if ($LASTEXITCODE -ne 0) { throw "pip dependency install failed ($LASTEXITCODE)" } # --------------------------------------------------------------------------- # 3. Build the single-file GUI executable with PyInstaller # --------------------------------------------------------------------------- Write-Host "==> Building $AppName.exe with PyInstaller ..." $workPath = Join-Path $BuildDir 'pyi_work' $specPath = $BuildDir & $PyExe -m PyInstaller ` --noconfirm --clean ` --onefile --noconsole ` --icon $IconFile ` --name $AppName ` --distpath $InstallDir ` --workpath $workPath ` --specpath $specPath ` $AppScript if ($LASTEXITCODE -ne 0) { throw "PyInstaller build failed ($LASTEXITCODE)" } $exePath = Join-Path $InstallDir "$AppName.exe" if (-not (Test-Path $exePath)) { throw "Build finished but $exePath was not produced" } Write-Host "==> BUILD SUCCESS" Write-Host "==> Executable: $exePath"