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:
@@ -27,12 +27,20 @@ function Winget-Install($id) {
|
|||||||
$env:PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [Environment]::GetEnvironmentVariable("PATH", "User")
|
$env:PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [Environment]::GetEnvironmentVariable("PATH", "User")
|
||||||
}
|
}
|
||||||
function Find-Python {
|
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") } }
|
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"
|
$p = Join-Path $env:LOCALAPPDATA "Programs\Python\Python311\python.exe"
|
||||||
if (Test-Path $p) { return @($p) }
|
if (Test-Path $p) { return @($p) }
|
||||||
return $null
|
return $null
|
||||||
}
|
}
|
||||||
function Run-Python($py, [string[]]$a) { $rest = @(); if ($py.Count -gt 1) { $rest = $py[1..($py.Count - 1)] }; & $py[0] @rest @a }
|
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 -------------------------------------------------------------------------------------------------------
|
# ---- 1. git -------------------------------------------------------------------------------------------------------
|
||||||
$haveGit = [bool](Get-Command git -ErrorAction SilentlyContinue)
|
$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" })
|
Add-Row "python 3.11" ([bool]$py) $(if ($py) { (Run-Python $py @("--version")) } else { "winget install -e --id Python.Python.3.11" })
|
||||||
if ($py) {
|
if ($py) {
|
||||||
foreach ($pair in @(@("pyserial", "serial"), @("asn1tools", "asn1tools"))) {
|
foreach ($pair in @(@("pyserial", "serial"), @("asn1tools", "asn1tools"))) {
|
||||||
Run-Python $py @("-c", "import $($pair[1])") 2>$null
|
$have = Test-Import $py $pair[1]
|
||||||
$have = ($LASTEXITCODE -eq 0)
|
if (-not $have -and -not $CheckOnly) { Run-Python $py @("-m", "pip", "install", "--user", $pair[0]); $have = Test-Import $py $pair[1] }
|
||||||
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) }
|
|
||||||
Add-Row "pip: $($pair[0])" $have $(if ($have) { "importable" } else { "py -3.11 -m pip install --user $($pair[0])" })
|
Add-Row "pip: $($pair[0])" $have $(if ($have) { "importable" } else { "py -3.11 -m pip install --user $($pair[0])" })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
# 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);'
|
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.
|
# Which board: the one Espressif native-USB port, unless named.
|
||||||
if ($Port -eq "auto") {
|
if ($Port -eq "auto") {
|
||||||
|
|||||||
Reference in New Issue
Block a user