From d27856aba14fd5d025f328f54cfbc66872643a01 Mon Sep 17 00:00:00 2001 From: Ashin Walpola Date: Fri, 27 Nov 2026 10:58:30 +0100 Subject: [PATCH] Fix the bench scripts on Windows PowerShell 5.1 setup_workstation.ps1 stopped on the failed import of a missing Python package instead of installing it; start_overnight.ps1 stopped on the keep-awake call, whose 0x80000001 literal is read as a negative int. --- obu-firmware/test/bench/setup_workstation.ps1 | 13 ++++++++++--- obu-firmware/test/bench/start_overnight.ps1 | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/obu-firmware/test/bench/setup_workstation.ps1 b/obu-firmware/test/bench/setup_workstation.ps1 index 273febc..04301ab 100644 --- a/obu-firmware/test/bench/setup_workstation.ps1 +++ b/obu-firmware/test/bench/setup_workstation.ps1 @@ -27,12 +27,20 @@ function Winget-Install($id) { $env:PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [Environment]::GetEnvironmentVariable("PATH", "User") } function Find-Python { + $ErrorActionPreference = "Continue" # see Test-Import if (Get-Command py -ErrorAction SilentlyContinue) { $v = & py -3.11 --version 2>$null; if ($LASTEXITCODE -eq 0) { return @("py", "-3.11") } } $p = Join-Path $env:LOCALAPPDATA "Programs\Python\Python311\python.exe" if (Test-Path $p) { return @($p) } return $null } function Run-Python($py, [string[]]$a) { $rest = @(); if ($py.Count -gt 1) { $rest = $py[1..($py.Count - 1)] }; & $py[0] @rest @a } +# Under "Stop", Windows PowerShell 5.1 turns a native command's redirected stderr into a terminating error, so the traceback of +# a missing module would end the script instead of leading to the install. +function Test-Import($py, $module) { + $ErrorActionPreference = "Continue" + Run-Python $py @("-c", "import $module") 2>$null | Out-Null + return ($LASTEXITCODE -eq 0) +} # ---- 1. git ------------------------------------------------------------------------------------------------------- $haveGit = [bool](Get-Command git -ErrorAction SilentlyContinue) @@ -46,9 +54,8 @@ if (-not $py -and -not $CheckOnly) { Winget-Install "Python.Python.3.11"; $py = Add-Row "python 3.11" ([bool]$py) $(if ($py) { (Run-Python $py @("--version")) } else { "winget install -e --id Python.Python.3.11" }) if ($py) { foreach ($pair in @(@("pyserial", "serial"), @("asn1tools", "asn1tools"))) { - Run-Python $py @("-c", "import $($pair[1])") 2>$null - $have = ($LASTEXITCODE -eq 0) - if (-not $have -and -not $CheckOnly) { Run-Python $py @("-m", "pip", "install", "--user", $pair[0]); Run-Python $py @("-c", "import $($pair[1])") 2>$null; $have = ($LASTEXITCODE -eq 0) } + $have = Test-Import $py $pair[1] + if (-not $have -and -not $CheckOnly) { Run-Python $py @("-m", "pip", "install", "--user", $pair[0]); $have = Test-Import $py $pair[1] } Add-Row "pip: $($pair[0])" $have $(if ($have) { "importable" } else { "py -3.11 -m pip install --user $($pair[0])" }) } } diff --git a/obu-firmware/test/bench/start_overnight.ps1 b/obu-firmware/test/bench/start_overnight.ps1 index 07d73bf..01b69ea 100644 --- a/obu-firmware/test/bench/start_overnight.ps1 +++ b/obu-firmware/test/bench/start_overnight.ps1 @@ -29,7 +29,7 @@ function Run-Python([string[]]$a) { $rest = @(); if ($py.Count -gt 1) { $rest = # Keep Windows awake for as long as this PowerShell process lives. Add-Type -Namespace Win -Name Power -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);' -[void][Win.Power]::SetThreadExecutionState(0x80000001) # ES_CONTINUOUS | ES_SYSTEM_REQUIRED +[void][Win.Power]::SetThreadExecutionState([uint32]2147483649) # 0x80000001 = ES_CONTINUOUS | ES_SYSTEM_REQUIRED (PowerShell 5.1 reads that literal as a negative int) # Which board: the one Espressif native-USB port, unless named. if ($Port -eq "auto") {