Repository navigation
Expand file tree
/
Copy pathrun.ps1
More file actions
287 lines (262 loc) · 9.93 KB
/
Copy pathrun.ps1
File metadata and controls
287 lines (262 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
$ErrorActionPreference = "Stop"
$AppArguments = @($args)
$MinimumPython = "3.10"
$ManagedPython = "3.12"
$UvInstallVersion = "0.11.32"
$ProjectRoot = $PSScriptRoot
$BootstrapRoot = if ($env:IELTS_CODEX_BOOTSTRAP_DIR) {
[System.IO.Path]::GetFullPath($env:IELTS_CODEX_BOOTSTRAP_DIR)
} else {
Join-Path $ProjectRoot ".ielts-bootstrap"
}
function Write-Info {
param([string]$Message)
Write-Host "IELTS Codex: $Message"
}
function Test-PythonCandidate {
param(
[string]$Executable,
[string[]]$PrefixArguments = @()
)
try {
& $Executable @PrefixArguments -c `
"import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)" `
*> $null
if ($LASTEXITCODE -eq 0) {
return [PSCustomObject]@{
Executable = $Executable
PrefixArguments = @($PrefixArguments)
}
}
} catch {
return $null
}
return $null
}
function Find-ManagedPython {
$pythonRoot = Join-Path $BootstrapRoot "python"
if (-not (Test-Path -LiteralPath $pythonRoot -PathType Container)) {
return $null
}
$localUv = Join-Path $BootstrapRoot "bin\uv.exe"
if (Test-Path -LiteralPath $localUv -PathType Leaf) {
$oldCache = $env:UV_CACHE_DIR
$oldInstallDir = $env:UV_PYTHON_INSTALL_DIR
$oldPreference = $env:UV_PYTHON_PREFERENCE
try {
$env:UV_CACHE_DIR = Join-Path $BootstrapRoot "cache"
$env:UV_PYTHON_INSTALL_DIR = $pythonRoot
$env:UV_PYTHON_PREFERENCE = "only-managed"
$paths = @(& $localUv python find $ManagedPython 2>$null)
if ($LASTEXITCODE -eq 0 -and $paths.Count -gt 0) {
$candidate = Test-PythonCandidate `
-Executable ([string]$paths[-1]).Trim()
if ($null -ne $candidate) {
return $candidate
}
}
} catch {
# Fall through to the directory scan for an interrupted bootstrap.
} finally {
$env:UV_CACHE_DIR = $oldCache
$env:UV_PYTHON_INSTALL_DIR = $oldInstallDir
$env:UV_PYTHON_PREFERENCE = $oldPreference
}
}
$relativeExecutables = @(
"python.exe",
"bin\python.exe",
"bin\python3.12.exe"
)
$installations = @(
Get-ChildItem -LiteralPath $pythonRoot -Directory -ErrorAction SilentlyContinue |
Sort-Object Name -Descending
)
foreach ($installation in $installations) {
foreach ($relativePath in $relativeExecutables) {
$executable = Join-Path $installation.FullName $relativePath
if (Test-Path -LiteralPath $executable -PathType Leaf) {
$candidate = Test-PythonCandidate -Executable $executable
if ($null -ne $candidate) {
return $candidate
}
}
}
}
return $null
}
function Find-Python {
$candidates = @(
[PSCustomObject]@{ Executable = "python"; PrefixArguments = @() },
[PSCustomObject]@{ Executable = "python3"; PrefixArguments = @() },
[PSCustomObject]@{ Executable = "py"; PrefixArguments = @("-3.14") },
[PSCustomObject]@{ Executable = "py"; PrefixArguments = @("-3.13") },
[PSCustomObject]@{ Executable = "py"; PrefixArguments = @("-3.12") },
[PSCustomObject]@{ Executable = "py"; PrefixArguments = @("-3.11") },
[PSCustomObject]@{ Executable = "py"; PrefixArguments = @("-3.10") }
)
foreach ($candidate in $candidates) {
$found = Test-PythonCandidate `
-Executable $candidate.Executable `
-PrefixArguments $candidate.PrefixArguments
if ($null -ne $found) {
return $found
}
}
return (Find-ManagedPython)
}
function Confirm-ManagedPython {
$prompt = "IELTS Codex: No Python $MinimumPython+ interpreter was found. " +
"Use Astral uv (downloading pinned uv if needed) to install project-local " +
"Python $ManagedPython into $BootstrapRoot? This will not change the " +
"system Python or PATH. [y/N]"
try {
$answer = Read-Host $prompt
} catch {
return $false
}
return $answer -match "^(?i:y|yes)$"
}
function Install-LocalUv {
$uvDirectory = Join-Path $BootstrapRoot "bin"
$uvExecutable = Join-Path $uvDirectory "uv.exe"
$installerUrl = "https://astral.sh/uv/$UvInstallVersion/install.ps1"
$installerFile = Join-Path `
([System.IO.Path]::GetTempPath()) `
("ielts-codex-uv-{0}.ps1" -f ([Guid]::NewGuid()).ToString("N"))
New-Item -ItemType Directory -Force -Path $uvDirectory | Out-Null
Write-Info "Downloading pinned uv $UvInstallVersion..."
try {
[Net.ServicePointManager]::SecurityProtocol = `
[Net.ServicePointManager]::SecurityProtocol -bor `
[Net.SecurityProtocolType]::Tls12
$oldProgress = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
try {
Invoke-WebRequest `
-UseBasicParsing `
-Uri $installerUrl `
-OutFile $installerFile
} finally {
$ProgressPreference = $oldProgress
}
$oldUnmanagedInstall = $env:UV_UNMANAGED_INSTALL
$oldNoModifyPath = $env:UV_NO_MODIFY_PATH
try {
$env:UV_UNMANAGED_INSTALL = $uvDirectory
$env:UV_NO_MODIFY_PATH = "1"
Write-Info "Installing uv $UvInstallVersion inside the project bootstrap directory..."
$childPowerShell = Join-Path $PSHOME "powershell.exe"
& $childPowerShell `
-NoLogo `
-NoProfile `
-NonInteractive `
-ExecutionPolicy Bypass `
-File $installerFile | Out-Host
$installerExitCode = $LASTEXITCODE
} finally {
$env:UV_UNMANAGED_INSTALL = $oldUnmanagedInstall
$env:UV_NO_MODIFY_PATH = $oldNoModifyPath
}
if ($installerExitCode -ne 0) {
throw "The pinned uv installer exited with code $installerExitCode."
}
} catch {
throw "Could not install pinned uv from $installerUrl. $($_.Exception.Message)"
} finally {
Remove-Item -LiteralPath $installerFile -Force -ErrorAction SilentlyContinue
}
if (-not (Test-Path -LiteralPath $uvExecutable -PathType Leaf)) {
throw "uv installation completed, but uv.exe was not found."
}
return $uvExecutable
}
function Resolve-Uv {
$uvCommand = Get-Command "uv" -CommandType Application -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($null -ne $uvCommand) {
Write-Info "Using existing uv executable at $($uvCommand.Source)."
return $uvCommand.Source
}
$localUv = Join-Path $BootstrapRoot "bin\uv.exe"
if (Test-Path -LiteralPath $localUv -PathType Leaf) {
return $localUv
}
return (Install-LocalUv)
}
function Install-ManagedPython {
$uvExecutable = Resolve-Uv
$pythonDirectory = Join-Path $BootstrapRoot "python"
$cacheDirectory = Join-Path $BootstrapRoot "cache"
New-Item -ItemType Directory -Force -Path $pythonDirectory | Out-Null
New-Item -ItemType Directory -Force -Path $cacheDirectory | Out-Null
$oldCache = $env:UV_CACHE_DIR
$oldInstallDir = $env:UV_PYTHON_INSTALL_DIR
$oldInstallBin = $env:UV_PYTHON_INSTALL_BIN
$oldPreference = $env:UV_PYTHON_PREFERENCE
try {
$env:UV_CACHE_DIR = $cacheDirectory
$env:UV_PYTHON_INSTALL_DIR = $pythonDirectory
$env:UV_PYTHON_INSTALL_BIN = "false"
Write-Info "Installing project-local Python $ManagedPython with uv..."
& $uvExecutable python install $ManagedPython | Out-Host
$installExitCode = $LASTEXITCODE
if ($installExitCode -ne 0) {
throw "uv could not install Python $ManagedPython."
}
$env:UV_PYTHON_PREFERENCE = "only-managed"
$paths = @(& $uvExecutable python find $ManagedPython)
$findExitCode = $LASTEXITCODE
if ($findExitCode -ne 0 -or $paths.Count -eq 0) {
throw "uv installed Python, but could not locate it."
}
$managedPath = ([string]$paths[-1]).Trim()
} finally {
$env:UV_CACHE_DIR = $oldCache
$env:UV_PYTHON_INSTALL_DIR = $oldInstallDir
$env:UV_PYTHON_INSTALL_BIN = $oldInstallBin
$env:UV_PYTHON_PREFERENCE = $oldPreference
}
$candidate = Test-PythonCandidate -Executable $managedPath
if ($null -eq $candidate) {
throw "uv finished, but no Python $MinimumPython+ interpreter was found."
}
return $candidate
}
function Invoke-IeltsCodex {
if ($env:IELTS_CODEX_PYTHON) {
$python = Test-PythonCandidate -Executable $env:IELTS_CODEX_PYTHON
if ($null -eq $python) {
throw "IELTS_CODEX_PYTHON must name a Python $MinimumPython+ interpreter."
}
} else {
$python = Find-Python
}
if ($null -eq $python) {
if ($env:IELTS_CODEX_NO_AUTO_INSTALL -eq "1") {
throw "Python $MinimumPython+ is required. Set IELTS_CODEX_PYTHON or install it first."
}
if (-not (Confirm-ManagedPython)) {
throw "Python $MinimumPython+ is required. Rerun run.bat and approve the uv-managed project-local Python."
}
$python = Install-ManagedPython
}
$prefixArguments = @($python.PrefixArguments)
$versionOutput = @(
& $python.Executable @prefixArguments -c `
"import sys; print(sys.version.split()[0])"
)
if ($LASTEXITCODE -ne 0 -or $versionOutput.Count -eq 0) {
throw "The selected Python interpreter could not start."
}
Write-Info "Using $(([string]$versionOutput[-1]).Trim())."
$entryPoint = Join-Path $ProjectRoot "ielts.py"
& $python.Executable @prefixArguments $entryPoint @AppArguments
exit $LASTEXITCODE
}
try {
Invoke-IeltsCodex
} catch {
[Console]::Error.WriteLine("IELTS Codex: {0}", $_.Exception.Message)
exit 1
}