|
| 1 | +import { rmSync, existsSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { spawnSync } from 'node:child_process'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 7 | +const buildVenv = path.join(root, '.build-venv'); |
| 8 | +const pythonDist = path.join(root, 'python_dist'); |
| 9 | +const webWork = path.join(root, '.tmp-pyinstaller-web'); |
| 10 | +const proxyWork = path.join(root, '.tmp-pyinstaller-proxy'); |
| 11 | +const isWindows = process.platform === 'win32'; |
| 12 | +const venvPython = isWindows |
| 13 | + ? path.join(buildVenv, 'Scripts', 'python.exe') |
| 14 | + : path.join(buildVenv, 'bin', 'python'); |
| 15 | +const systemPython = process.env.PYTHON || (isWindows ? 'python' : 'python3'); |
| 16 | + |
| 17 | +function run(command, args, options = {}) { |
| 18 | + const result = spawnSync(command, args, { |
| 19 | + cwd: root, |
| 20 | + stdio: 'inherit', |
| 21 | + shell: false, |
| 22 | + ...options, |
| 23 | + }); |
| 24 | + |
| 25 | + if (result.error) { |
| 26 | + throw result.error; |
| 27 | + } |
| 28 | + if (result.status !== 0) { |
| 29 | + throw new Error(`${command} ${args.join(' ')} failed with exit code ${result.status}`); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function removeWorkspacePath(target) { |
| 34 | + const resolvedRoot = path.resolve(root); |
| 35 | + const resolvedTarget = path.resolve(target); |
| 36 | + const relative = path.relative(resolvedRoot, resolvedTarget); |
| 37 | + |
| 38 | + if (relative.startsWith('..') || path.isAbsolute(relative)) { |
| 39 | + throw new Error(`Refusing to remove path outside workspace: ${resolvedTarget}`); |
| 40 | + } |
| 41 | + |
| 42 | + rmSync(resolvedTarget, { recursive: true, force: true }); |
| 43 | +} |
| 44 | + |
| 45 | +if (!existsSync(venvPython)) { |
| 46 | + run(systemPython, ['-m', 'venv', buildVenv]); |
| 47 | +} |
| 48 | + |
| 49 | +run(venvPython, ['-m', 'pip', 'install', '--upgrade', 'pip']); |
| 50 | +run(venvPython, ['-m', 'pip', 'install', '-r', path.join(root, 'requirements.txt'), 'pyinstaller']); |
| 51 | + |
| 52 | +removeWorkspacePath(pythonDist); |
| 53 | +removeWorkspacePath(webWork); |
| 54 | +removeWorkspacePath(proxyWork); |
| 55 | + |
| 56 | +run(venvPython, [ |
| 57 | + '-m', |
| 58 | + 'PyInstaller', |
| 59 | + '--noconfirm', |
| 60 | + '--clean', |
| 61 | + '--distpath', |
| 62 | + pythonDist, |
| 63 | + '--workpath', |
| 64 | + webWork, |
| 65 | + '--specpath', |
| 66 | + webWork, |
| 67 | + '--name', |
| 68 | + 'hash-web-server', |
| 69 | + path.join(root, 'web_server.py'), |
| 70 | +]); |
| 71 | + |
| 72 | +run(venvPython, [ |
| 73 | + '-m', |
| 74 | + 'PyInstaller', |
| 75 | + '--noconfirm', |
| 76 | + '--clean', |
| 77 | + '--distpath', |
| 78 | + pythonDist, |
| 79 | + '--workpath', |
| 80 | + proxyWork, |
| 81 | + '--specpath', |
| 82 | + proxyWork, |
| 83 | + '--name', |
| 84 | + 'hash-proxy-server', |
| 85 | + path.join(root, 'proxy_server.py'), |
| 86 | +]); |
0 commit comments