summaryrefslogtreecommitdiffstats
path: root/ui/build_scripts/setup-embedded-python.js
blob: 0622915e76eba6430516b1fa4936ce1a00aedaf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const https = require('https');
const { promisify } = require('util');
const stream = require('stream');
const pipeline = promisify(stream.pipeline);
const extract = require('extract-zip');

const projectRoot = path.join(__dirname, '..', '..');
const pythonPath = path.join(projectRoot, 'python_embedded');
const dllPath = path.join(projectRoot, 'dll_empty');

const PYTHON_URL = 'https://www.python.org/ftp/python/3.10.11/python-3.10.11-embed-amd64.zip';
const PIP_URL = 'https://bootstrap.pypa.io/get-pip.py';

async function downloadFile(url, dest) {
    console.log(`Downloading ${url}...`);
    const file = fs.createWriteStream(dest);

    return new Promise((resolve, reject) => {
        https.get(url, (response) => {
            if (response.statusCode === 302 || response.statusCode === 301) {
                // Handle redirect
                return downloadFile(response.headers.location, dest).then(resolve).catch(reject);
            }

            response.pipe(file);
            file.on('finish', () => {
                file.close();
                console.log(`Downloaded to ${dest}`);
                resolve();
            });
        }).on('error', (err) => {
            fs.unlink(dest, () => {}); // Delete the file on error
            reject(err);
        });
    });
}

async function setupEmbeddedPython() {
    console.log('Setting up embedded Python...');

    // Delete existing directories
    if (fs.existsSync(pythonPath)) {
        fs.rmSync(pythonPath, { recursive: true, force: true });
        console.log('Deleted existing Python directory');
    }
    if (fs.existsSync(dllPath)) {
        fs.rmSync(dllPath, { recursive: true, force: true });
        console.log('Deleted existing dll directory');
    }

    // Create directories
    fs.mkdirSync(pythonPath, { recursive: true });
    fs.mkdirSync(dllPath, { recursive: true });
    console.log('Created Python and dll directories');

    // Download Python
    const pythonZip = path.join(projectRoot, 'python-3.10.11-embed-amd64.zip');
    if (!fs.existsSync(pythonZip)) {
        await downloadFile(PYTHON_URL, pythonZip);
    }

    // Extract Python
    console.log('Extracting Python...');
    await extract(pythonZip, { dir: pythonPath });
    console.log('Python extracted successfully');

    // Update python310._pth to include the app directory and enable site packages
    const pthFile = path.join(pythonPath, 'python310._pth');
    const pthContent = fs.readFileSync(pthFile, 'utf8');
    fs.writeFileSync(pthFile, pthContent + '\n../app\nimport site\n');
    console.log('Updated python310._pth');

    // Download get-pip.py
    const getPipPath = path.join(pythonPath, 'get-pip.py');
    await downloadFile(PIP_URL, getPipPath);

    // Install pip
    console.log('Installing pip...');
    try {
        execSync(`"${path.join(pythonPath, 'python.exe')}" "${getPipPath}"`, {
            stdio: 'inherit',
            cwd: pythonPath
        });
        console.log('pip installed successfully');
    } catch (error) {
        console.error('Failed to install pip:', error);
        process.exit(1);
    }

    // Clean up
    fs.unlinkSync(getPipPath);

    console.log('Embedded Python setup complete!');
}

// Run the setup
setupEmbeddedPython().catch(err => {
    console.error('Setup failed:', err);
    process.exit(1);
});