|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Downloads MNE-Python and its pure-Python dependencies from PyPI as wheel |
| 4 | + * files for offline use with Pyodide. Binary dependencies (numpy, scipy, |
| 5 | + * matplotlib, pandas) are already included via the `pyodide` npm package and |
| 6 | + * do NOT need to be downloaded here. |
| 7 | + * |
| 8 | + * Downloaded wheels are saved to: |
| 9 | + * src/renderer/utils/pyodide/src/packages/ |
| 10 | + * |
| 11 | + * A manifest.json is written there so the web worker knows which filenames |
| 12 | + * to pass to micropip.install() at startup. |
| 13 | + * |
| 14 | + * Usage: node internals/scripts/InstallMNE.mjs |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from 'fs'; |
| 18 | +import https from 'https'; |
| 19 | +import path from 'path'; |
| 20 | +import chalk from 'chalk'; |
| 21 | + |
| 22 | +const PACKAGES_DIR = path.resolve( |
| 23 | + 'src/renderer/utils/pyodide/src/packages' |
| 24 | +); |
| 25 | +const MANIFEST_FILE = path.join(PACKAGES_DIR, 'manifest.json'); |
| 26 | + |
| 27 | +/** |
| 28 | + * Pure-Python packages required by MNE that are not bundled with Pyodide. |
| 29 | + * Each entry is resolved against the PyPI JSON API to find the latest |
| 30 | + * pure-Python wheel (py3-none-any or py2.py3-none-any). |
| 31 | + */ |
| 32 | +const PACKAGES_TO_DOWNLOAD = [ |
| 33 | + 'mne', |
| 34 | + 'pooch', |
| 35 | + 'tqdm', |
| 36 | + 'platformdirs', |
| 37 | +]; |
| 38 | + |
| 39 | +// --------------------------------------------------------------------------- |
| 40 | +// Network helpers |
| 41 | +// --------------------------------------------------------------------------- |
| 42 | + |
| 43 | +function httpsGet(url) { |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + const req = https.get(url, { headers: { 'User-Agent': 'BrainWaves-installer/1.0' } }, (res) => { |
| 46 | + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 47 | + resolve(httpsGet(res.headers.location)); |
| 48 | + return; |
| 49 | + } |
| 50 | + if (res.statusCode !== 200) { |
| 51 | + reject(new Error(`HTTP ${res.statusCode} for ${url}`)); |
| 52 | + return; |
| 53 | + } |
| 54 | + let body = ''; |
| 55 | + res.setEncoding('utf8'); |
| 56 | + res.on('data', (chunk) => { body += chunk; }); |
| 57 | + res.on('end', () => resolve(body)); |
| 58 | + res.on('error', reject); |
| 59 | + }); |
| 60 | + req.on('error', reject); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function downloadBinary(url, dest) { |
| 65 | + return new Promise((resolve, reject) => { |
| 66 | + const doGet = (reqUrl) => { |
| 67 | + https.get(reqUrl, { headers: { 'User-Agent': 'BrainWaves-installer/1.0' } }, (res) => { |
| 68 | + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 69 | + doGet(res.headers.location); |
| 70 | + return; |
| 71 | + } |
| 72 | + if (res.statusCode !== 200) { |
| 73 | + reject(new Error(`HTTP ${res.statusCode} for ${reqUrl}`)); |
| 74 | + return; |
| 75 | + } |
| 76 | + const file = fs.createWriteStream(dest); |
| 77 | + res.pipe(file); |
| 78 | + file.on('finish', () => file.close(resolve)); |
| 79 | + file.on('error', (err) => { fs.unlink(dest, () => {}); reject(err); }); |
| 80 | + }).on('error', (err) => { fs.unlink(dest, () => {}); reject(err); }); |
| 81 | + }; |
| 82 | + doGet(url); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +// PyPI helpers |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | + |
| 90 | +/** |
| 91 | + * Returns the best pure-Python wheel for the latest release of `packageName`. |
| 92 | + * Preference: py3-none-any > py2.py3-none-any > *-none-any |
| 93 | + */ |
| 94 | +async function resolvePureWheel(packageName) { |
| 95 | + const raw = await httpsGet(`https://pypi.org/pypi/${packageName}/json`); |
| 96 | + const data = JSON.parse(raw); |
| 97 | + const version = data.info.version; |
| 98 | + const urls = data.urls; // files for the latest release |
| 99 | + |
| 100 | + const wheels = urls.filter((f) => f.filename.endsWith('.whl')); |
| 101 | + |
| 102 | + const ranked = [ |
| 103 | + wheels.find((f) => f.filename.endsWith('-py3-none-any.whl')), |
| 104 | + wheels.find((f) => f.filename.endsWith('-py2.py3-none-any.whl')), |
| 105 | + wheels.find((f) => f.filename.includes('-none-any.whl')), |
| 106 | + ].filter(Boolean); |
| 107 | + |
| 108 | + if (ranked.length === 0) { |
| 109 | + throw new Error( |
| 110 | + `No pure-Python wheel found for ${packageName} ${version}. ` + |
| 111 | + `Binary packages must come from the Pyodide npm bundle.` |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return { version, wheel: ranked[0] }; |
| 116 | +} |
| 117 | + |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +// Main |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | + |
| 122 | +async function installPackage(packageName, manifest) { |
| 123 | + process.stdout.write(chalk.blue(` ${packageName}: `)); |
| 124 | + |
| 125 | + let version, wheel; |
| 126 | + try { |
| 127 | + ({ version, wheel } = await resolvePureWheel(packageName)); |
| 128 | + } catch (err) { |
| 129 | + console.log(chalk.red(`FAILED — ${err.message}`)); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + const dest = path.join(PACKAGES_DIR, wheel.filename); |
| 134 | + |
| 135 | + if (fs.existsSync(dest)) { |
| 136 | + console.log(chalk.gray(`${version} already present, skipping`)); |
| 137 | + manifest[packageName] = { version, filename: wheel.filename }; |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + try { |
| 142 | + await downloadBinary(wheel.url, dest); |
| 143 | + console.log(chalk.green(`${version} downloaded`)); |
| 144 | + manifest[packageName] = { version, filename: wheel.filename }; |
| 145 | + } catch (err) { |
| 146 | + console.log(chalk.red(`FAILED — ${err.message}`)); |
| 147 | + if (fs.existsSync(dest)) fs.unlinkSync(dest); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +async function main() { |
| 152 | + fs.mkdirSync(PACKAGES_DIR, { recursive: true }); |
| 153 | + |
| 154 | + // Preserve any previously downloaded packages in the manifest. |
| 155 | + let manifest = {}; |
| 156 | + if (fs.existsSync(MANIFEST_FILE)) { |
| 157 | + try { |
| 158 | + manifest = JSON.parse(fs.readFileSync(MANIFEST_FILE, 'utf8')); |
| 159 | + } catch { |
| 160 | + manifest = {}; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + console.log(chalk.blue.bold('Downloading MNE-Python wheels from PyPI…')); |
| 165 | + for (const pkg of PACKAGES_TO_DOWNLOAD) { |
| 166 | + await installPackage(pkg, manifest); |
| 167 | + } |
| 168 | + |
| 169 | + fs.writeFileSync(MANIFEST_FILE, JSON.stringify(manifest, null, 2)); |
| 170 | + console.log(chalk.green.bold('\nAll MNE wheels ready.')); |
| 171 | + console.log(chalk.gray(`Manifest → ${MANIFEST_FILE}`)); |
| 172 | +} |
| 173 | + |
| 174 | +main().catch((err) => { |
| 175 | + console.error(chalk.red('Fatal error:'), err); |
| 176 | + process.exit(1); |
| 177 | +}); |
0 commit comments