|
| 1 | +#!/usr/bin/env node |
| 2 | +const { execSync, spawnSync } = require('child_process'); |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +const venvPath = path.join(__dirname, '..', '.venv'); |
| 7 | +const reqPath = path.join(__dirname, '..', 'requirements.txt'); |
| 8 | + |
| 9 | +try { |
| 10 | + if (!fs.existsSync(venvPath)) { |
| 11 | + console.log('Creating Python virtual environment...'); |
| 12 | + // Try python3 first, fallback to python |
| 13 | + try { |
| 14 | + execSync('python3 -m venv .venv', { |
| 15 | + cwd: path.join(__dirname, '..'), |
| 16 | + stdio: 'inherit' |
| 17 | + }); |
| 18 | + } catch (e) { |
| 19 | + execSync('python -m venv .venv', { |
| 20 | + cwd: path.join(__dirname, '..'), |
| 21 | + stdio: 'inherit' |
| 22 | + }); |
| 23 | + } |
| 24 | + } else { |
| 25 | + console.log('Python virtual environment already exists.'); |
| 26 | + } |
| 27 | + |
| 28 | + const pipCmd = process.platform === 'win32' |
| 29 | + ? path.join(venvPath, 'Scripts', 'pip') |
| 30 | + : path.join(venvPath, 'bin', 'pip'); |
| 31 | + |
| 32 | + console.log('Installing Python dependencies...'); |
| 33 | + const pipArgs = ['install', '-r', reqPath]; |
| 34 | + const result = spawnSync(pipCmd, pipArgs, { |
| 35 | + cwd: path.join(__dirname, '..'), |
| 36 | + stdio: 'inherit' |
| 37 | + }); |
| 38 | + |
| 39 | + if (result.error || result.status !== 0) { |
| 40 | + throw new Error(`pip install failed with status ${result.status}`); |
| 41 | + } |
| 42 | + console.log('Installation complete.'); |
| 43 | +} catch (error) { |
| 44 | + console.error('Failed to setup Python environment:', error.message); |
| 45 | + process.exit(1); |
| 46 | +} |
0 commit comments