Files
rvbox/scripts/windows/provision-python.ps1
T

76 lines
4.9 KiB
PowerShell

# Fixture-only Python bootstrap. No dependencies are installed on the Linux host.
param([ValidateSet('Install', 'Verify', 'VerifyClean')][string] $Mode = 'Install')
$ErrorActionPreference = 'Stop'
$Version = '3.13.16'
$Digest = 'fb4f9f5d438b2396da0086dc70b935c530cb578e37adc6d354f7ad2037fee83b'
$Python = 'C:\Python313\python.exe'
$Stage = 'C:\ProgramData\RVBoxFixture\python'
if ($Mode -eq 'VerifyClean') {
foreach ($rebootKey in @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending', 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired')) {
if (Test-Path -LiteralPath $rebootKey) { throw 'Windows servicing still requires a reboot; finish it before capturing the baseline.' }
}
if (Get-Service -Name RVBoxClient -ErrorAction SilentlyContinue) { throw 'RVBox is installed; refuse to capture a dirty baseline.' }
foreach ($path in @('C:\ProgramData\RVBox', 'C:\Program Files\RVBox', 'C:\Program Files (x86)\RVBox')) {
if (Test-Path -LiteralPath $path) { throw "RVBox artifacts remain at $path; restore a clean baseline first." }
}
foreach ($runKey in @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run')) {
if (Get-ItemProperty -LiteralPath $runKey -Name RVBoxTray -ErrorAction SilentlyContinue) {
throw 'RVBox tray registration remains; restore a clean baseline first.'
}
}
}
function Invoke-Python([string[]] $Arguments) {
& $Python @Arguments
if ($LASTEXITCODE -ne 0) { throw "Python verification failed ($LASTEXITCODE)" }
}
if ($Mode -eq 'Install' -and -not (Test-Path -LiteralPath $Python)) {
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw 'The fixture Python installer requires the high-token provisioning account.'
}
New-Item -ItemType Directory -Force -Path $Stage | Out-Null
$installer = Join-Path $Stage "python-$Version-amd64.exe"
# test-host downloads on Helium and copies the pinned offline installer.
# Never depend on the guest's HTTPS/proxy/certificate configuration.
if (-not (Test-Path -LiteralPath $installer)) { throw 'Staged offline Python installer is missing; rerun test-host provision-python.' }
if ((Get-FileHash -LiteralPath $installer -Algorithm SHA256).Hash.ToLowerInvariant() -ne $Digest) {
throw 'Python installer SHA-256 does not match the pinned python.org release.'
}
$signature = Get-AuthenticodeSignature -FilePath $installer
if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notlike '*Python Software Foundation*') {
throw "Python installer Authenticode verification failed: $($signature.Status)"
}
$arguments = @('/quiet', '/norestart', '/log', (Join-Path $Stage 'install.log'),
'InstallAllUsers=1', 'TargetDir=C:\Python313', 'PrependPath=1',
'Include_pip=1', 'Include_launcher=1', 'InstallLauncherAllUsers=1',
'Include_test=0', 'Include_doc=0', 'Include_tcltk=0', 'Shortcuts=0',
'Include_debug=0', 'Include_symbols=0')
$process = Start-Process -FilePath $installer -ArgumentList $arguments -PassThru
if (-not $process.WaitForExit(600000)) { throw 'Python installer exceeded its ten-minute deadline; inspect fixture logs before retrying.' }
if ($process.ExitCode -notin @(0, 3010)) { throw "Python installer failed ($($process.ExitCode)); see $Stage\install.log" }
}
if (-not (Test-Path -LiteralPath $Python)) { throw 'Pinned Python interpreter is missing.' }
Invoke-Python -Arguments @('-c', "import sys,ssl,sqlite3,json,venv; assert sys.version.split()[0] == '$Version'; print('python_version=' + sys.version.split()[0]); print('python_executable=' + sys.executable)")
Invoke-Python -Arguments @('-m', 'pip', '--version')
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
$machinePathEntries = @($machinePath -split ';' | ForEach-Object { $_.Trim().TrimEnd('\') })
if ($machinePathEntries -notcontains 'C:\Python313' -or $machinePathEntries -notcontains 'C:\Python313\Scripts') {
throw 'Python and Scripts directories are missing from machine PATH.'
}
# Prove venv and its bundled pip under the invoking identity. Remove the exact
# newly-created test directory even if verification fails; retain install logs.
$venvPath = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) ('RVBoxFixture-python-verify-' + [Guid]::NewGuid().ToString('N'))
try {
Invoke-Python -Arguments @('-m', 'venv', $venvPath)
& (Join-Path $venvPath 'Scripts\python.exe') -m pip --version
if ($LASTEXITCODE -ne 0) { throw 'venv pip verification failed.' }
} finally {
if (Test-Path -LiteralPath $venvPath) { Remove-Item -LiteralPath $venvPath -Recurse -Force }
}
Write-Output 'RVBOX_GUEST_OK'