51 lines
2.8 KiB
PowerShell
51 lines
2.8 KiB
PowerShell
# Shared Windows launcher. Also supports: ./scripts/dingtalk-python.ps1 check --at '姓名'
|
|
$ErrorActionPreference = 'Stop'
|
|
if ($PSVersionTable.PSVersion -lt [version]'7.3') { throw '请使用 PowerShell 7.3+ 或独立 Python 入口。' }
|
|
$PSNativeCommandArgumentPassing = 'Standard'
|
|
|
|
function Resolve-NoticePython {
|
|
$candidates = @()
|
|
if ($env:DINGTALK_PYTHON) {
|
|
# An explicit choice must work; never silently substitute another runtime.
|
|
$candidates = @($env:DINGTALK_PYTHON)
|
|
} else {
|
|
if ($env:USERPROFILE) {
|
|
$bundled = Join-Path $env:USERPROFILE '.cache/codex-runtimes/codex-primary-runtime/dependencies/python/python.exe'
|
|
if (Test-Path -LiteralPath $bundled -PathType Leaf) { $candidates += $bundled }
|
|
}
|
|
$candidates += @(Get-Command python, python3 -All -CommandType Application -ErrorAction SilentlyContinue |
|
|
Select-Object -ExpandProperty Source)
|
|
# py is probed, not trusted: it can also resolve to Store Python.
|
|
$candidates += @(Get-Command py -All -CommandType Application -ErrorAction SilentlyContinue |
|
|
Select-Object -ExpandProperty Source)
|
|
}
|
|
foreach ($candidate in ($candidates | Select-Object -Unique)) {
|
|
if ($candidate -match '(?i)[\\/]WindowsApps[\\/]|PythonSoftwareFoundation\.Python\.') { continue }
|
|
$prefix = if ([IO.Path]::GetFileName($candidate) -eq 'py.exe') { @('-3') } else { @() }
|
|
try {
|
|
$probe = & $candidate @prefix -I -c 'import json,sys; print(json.dumps({"exe":sys.executable,"supported":sys.version_info >= (3,9)}))' 2>$null
|
|
if ($LASTEXITCODE -ne 0) { continue }
|
|
$info = ($probe -join "`n") | ConvertFrom-Json
|
|
if ($info.supported -and $info.exe -and
|
|
$info.exe -notmatch '(?i)[\\/]WindowsApps[\\/]|PythonSoftwareFoundation\.Python\.' -and
|
|
(Test-Path -LiteralPath $info.exe -PathType Leaf)) { return $info.exe }
|
|
} catch { continue }
|
|
}
|
|
throw '未找到独立 Python 3.9+。请安装 python.org 版本或将 DINGTALK_PYTHON 设为独立 python.exe 的路径;不使用商店版,也不自动安装。'
|
|
}
|
|
|
|
$pythonExe = Resolve-NoticePython
|
|
$previousArgs = [Environment]::GetEnvironmentVariable('DINGTALK_PROGRESS_ARGV_B64', 'Process')
|
|
$previousEncoding = [Environment]::GetEnvironmentVariable('PYTHONIOENCODING', 'Process')
|
|
try {
|
|
$encodedArgs = ConvertTo-Json -InputObject @($args) -Compress
|
|
$env:DINGTALK_PROGRESS_ARGV_B64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($encodedArgs))
|
|
$env:PYTHONIOENCODING = 'utf-8'
|
|
& $pythonExe "$PSScriptRoot/dingtalk_notify.py"
|
|
$exitCode = $LASTEXITCODE
|
|
} finally {
|
|
[Environment]::SetEnvironmentVariable('DINGTALK_PROGRESS_ARGV_B64', $previousArgs, 'Process')
|
|
[Environment]::SetEnvironmentVariable('PYTHONIOENCODING', $previousEncoding, 'Process')
|
|
}
|
|
exit $exitCode
|