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")
|
||||
}
|
||||
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])" })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user