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.
This commit is contained in:
Ashin Walpola
2026-11-27 10:58:30 +01:00
parent aef65394ea
commit d27856aba1
2 changed files with 11 additions and 4 deletions
+10 -3
View File
@@ -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])" })
}
}
+1 -1
View File
@@ -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") {