-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pyinstaller.js
More file actions
executable file
·98 lines (89 loc) · 2.9 KB
/
Copy pathgenerate_pyinstaller.js
File metadata and controls
executable file
·98 lines (89 loc) · 2.9 KB
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
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { execSync } from "node:child_process";
const projectPath = process.argv[2];
console.log("projectPath", projectPath);
function pythonCommand() {
const candidates = ["python3", "python"];
for (const cmd of candidates) {
try {
execSync(`${cmd} --version`, { stdio: "ignore" });
return cmd;
} catch {
// silent fail
}
}
throw new Error("Python not found");
}
function createVenv() {
console.log("🔧 Setting up Python virtual environment...");
const python = pythonCommand();
console.log(`→ Using Python: ${python}`);
const venvPath = path.join(projectPath, "venv");
if (fs.existsSync(venvPath)) {
console.log(`→ Found existing venv → ${venvPath}`);
} else {
console.log(`→ Creating virtual environment → ${venvPath}`);
try {
execSync(`${python} -m venv ${venvPath}`, { stdio: "inherit" });
} catch (err) {
console.error("Failed to create virtual environment");
console.error(err.message);
process.exit(1);
}
}
if (process.platform === "win32") {
return path.join(venvPath, "Scripts", "python.exe");
} else {
return path.join(venvPath, "bin", "python");
}
}
function deleteVenv() {
const venvPath = path.join(projectPath, "venv");
if (fs.existsSync(venvPath)) {
console.log(`→ Removing virtual environment → ${venvPath}`);
fs.rmSync(venvPath, { recursive: true, force: true });
}
}
function installDependecies(pythonExe) {
console.log(`→ Installing dependencies ...`);
const pipCommand = `pip install ${projectPath} pyinstaller`;
try {
console.log(`→ Running: ${pythonExe} -m ${pipCommand}`);
execSync(`${pythonExe} -m ${pipCommand}`, { stdio: "inherit" });
} catch (err) {
console.error("Failed to install requirements");
console.error(err.message);
process.exit(1);
}
console.log("✅ Python virtual environment setup complete");
}
function runPyInstaller(pythonExe) {
console.log(`→ Running PyInstaller ...`);
const specFiles = fs
.readdirSync(projectPath, { withFileTypes: true })
.filter((file) => file.isFile() && file.name.endsWith(".spec"))
.map((file) => path.join(projectPath, file.name));
if (specFiles.length !== 1) {
console.error("Expected 1 spec file, found " + specFiles.length);
process.exit(1);
}
const pyinstallerCommand = `PyInstaller ${specFiles[0]} --distpath ${process.cwd()} --clean`;
try {
console.log(`→ Running: ${pythonExe} -m ${pyinstallerCommand}`);
execSync(`${pythonExe} -m ${pyinstallerCommand}`, { stdio: "inherit" });
} catch (err) {
console.error("Failed to run pyinstaller");
console.error(err.message);
process.exit(1);
}
console.log("✅ PyInstaller complete");
}
function main() {
const pythonExe = createVenv();
installDependecies(pythonExe);
runPyInstaller(pythonExe);
deleteVenv();
}
main();