( pythonExe: string, requirementsPath: string, win: BrowserWindow )
| 220 | } |
| 221 | |
| 222 | function installRequirements( |
| 223 | pythonExe: string, |
| 224 | requirementsPath: string, |
| 225 | win: BrowserWindow |
| 226 | ): Promise<void> { |
| 227 | const TOTAL_PACKAGES = 20 |
| 228 | return new Promise((resolve, reject) => { |
| 229 | console.log('[PythonSetup] Installing requirements with', pythonExe) |
| 230 | const proc = spawn( |
| 231 | pythonExe, |
| 232 | ['-m', 'pip', 'install', '-r', requirementsPath, '--no-warn-script-location', '--progress-bar', 'off'], |
| 233 | { stdio: ['ignore', 'pipe', 'pipe'], env: cleanPythonEnv() } |
| 234 | ) |
| 235 | let packagesInstalled = 0 |
| 236 | const onLine = (line: string) => { |
| 237 | console.log('[pip]', line) |
| 238 | let currentPackage: string | undefined |
| 239 | const collectMatch = line.match(/^Collecting (.+?)(?:\s|$)/) |
| 240 | if (collectMatch) { packagesInstalled++; currentPackage = collectMatch[1] } |
| 241 | const downloadMatch = line.match(/^Downloading (.+?)(?:\s|$)/) |
| 242 | if (downloadMatch) currentPackage = `Downloading ${downloadMatch[1]}…` |
| 243 | const percent = Math.round(20 + (packagesInstalled / TOTAL_PACKAGES) * 79) |
| 244 | win.webContents.send('setup:progress', { |
| 245 | step: 'packages', |
| 246 | percent: Math.min(percent, 99), |
| 247 | currentPackage, |
| 248 | }) |
| 249 | } |
| 250 | let buffer = '' |
| 251 | proc.stdout?.on('data', (d: Buffer) => { |
| 252 | buffer += d.toString() |
| 253 | const lines = buffer.split('\n') |
| 254 | buffer = lines.pop() ?? '' |
| 255 | lines.forEach((l) => onLine(l.trim())) |
| 256 | }) |
| 257 | proc.stderr?.on('data', (d: Buffer) => { |
| 258 | const text = d.toString().trim() |
| 259 | if (text) console.error('[pip]', text) |
| 260 | }) |
| 261 | proc.on('close', (code) => { |
| 262 | if (code === 0) { |
| 263 | win.webContents.send('setup:progress', { step: 'packages', percent: 100 }) |
| 264 | resolve() |
| 265 | } else { |
| 266 | reject(new Error(`pip install exited with code ${code}`)) |
| 267 | } |
| 268 | }) |
| 269 | }) |
| 270 | } |
| 271 | |
| 272 | // ─── Unix dev helper ───────────────────────────────────────────────────────── |
| 273 |
no test coverage detected