()
| 174 | * if the first attempt fails, to rule out mirror-specific issues. |
| 175 | */ |
| 176 | export async function setupManagedPython(): Promise<void> { |
| 177 | const { bin: uvBin, source } = resolveUvBin(); |
| 178 | const uvEnv = await getUvMirrorEnv(); |
| 179 | const hasMirror = Object.keys(uvEnv).length > 0; |
| 180 | |
| 181 | logger.info( |
| 182 | `Setting up managed Python 3.12 ` + |
| 183 | `(uv=${uvBin}, source=${source}, arch=${process.arch}, mirror=${hasMirror})` |
| 184 | ); |
| 185 | |
| 186 | const baseEnv: Record<string, string | undefined> = { ...process.env }; |
| 187 | |
| 188 | // Attempt 1: with mirror (if applicable) |
| 189 | try { |
| 190 | await runPythonInstall(uvBin, { ...baseEnv, ...uvEnv }, hasMirror ? 'mirror' : 'default'); |
| 191 | } catch (firstError) { |
| 192 | logger.warn('Python install attempt 1 failed:', firstError); |
| 193 | |
| 194 | if (hasMirror) { |
| 195 | // Attempt 2: retry without mirror to rule out mirror issues |
| 196 | logger.info('Retrying Python install without mirror...'); |
| 197 | try { |
| 198 | await runPythonInstall(uvBin, baseEnv, 'no-mirror'); |
| 199 | } catch (secondError) { |
| 200 | logger.error('Python install attempt 2 (no mirror) also failed:', secondError); |
| 201 | throw secondError; |
| 202 | } |
| 203 | } else { |
| 204 | throw firstError; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // After installation, verify and log the Python path |
| 209 | const verifyShell = needsWinShell(uvBin); |
| 210 | try { |
| 211 | const findPath = await new Promise<string>((resolve) => { |
| 212 | const child = spawn(verifyShell ? quoteForCmd(uvBin) : uvBin, ['python', 'find', '3.12'], { |
| 213 | shell: verifyShell, |
| 214 | env: { ...process.env, ...uvEnv }, |
| 215 | windowsHide: true, |
| 216 | }); |
| 217 | let output = ''; |
| 218 | child.stdout?.on('data', (data) => { output += data; }); |
| 219 | child.on('close', () => resolve(output.trim())); |
| 220 | }); |
| 221 | |
| 222 | if (findPath) { |
| 223 | logger.info(`Managed Python 3.12 installed at: ${findPath}`); |
| 224 | } |
| 225 | } catch (err) { |
| 226 | logger.warn('Could not determine Python path after install:', err); |
| 227 | } |
| 228 | } |
no test coverage detected