/** * 下载并静默安装 Python 3.13(Chaquopy 17 要求 buildPython 与内嵌版本主次号一致)。 * 用法: node scripts/install-python313.mjs */ import { spawnSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; const VERSION = '3.13.9'; const TARGET = 'C:\\Python313'; const CACHE = 'C:\\Android\\cache'; const installer = path.join(CACHE, `python-${VERSION}-amd64.exe`); const exe = path.join(TARGET, 'python.exe'); if (fs.existsSync(exe)) { const res = spawnSync(exe, ['-V'], { encoding: 'utf8' }); console.log(`已安装:${(res.stdout || res.stderr).trim()} → ${TARGET}`); process.exit(0); } if (!fs.existsSync(installer)) { const url = `https://www.python.org/ftp/python/${VERSION}/python-${VERSION}-amd64.exe`; console.log('下载', url); const res = await fetch(url, { signal: AbortSignal.timeout(900000) }); if (!res.ok) throw new Error(`HTTP ${res.status}`); fs.mkdirSync(CACHE, { recursive: true }); fs.writeFileSync(installer, Buffer.from(await res.arrayBuffer())); console.log(' ', (fs.statSync(installer).size / 1024 / 1024).toFixed(1), 'MB'); } console.log('静默安装到', TARGET); const args = [ '/quiet', 'InstallAllUsers=0', `TargetDir=${TARGET}`, 'Include_pip=1', 'Include_test=0', 'Include_launcher=0', 'PrependPath=0', 'Shortcuts=0', ]; const code = spawnSync(installer, args, { stdio: 'inherit' }).status; if (code !== 0) { // 安装器有时返回非 0 但实际装好了 console.log('安装器退出码', code, ',检查是否装成功…'); } if (!fs.existsSync(exe)) throw new Error('安装失败:没找到 ' + exe); const res = spawnSync(exe, ['-V'], { encoding: 'utf8' }); console.log('完成:', (res.stdout || res.stderr).trim(), '→', TARGET);