-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare-win-python.js
More file actions
158 lines (145 loc) · 4.27 KB
/
Copy pathprepare-win-python.js
File metadata and controls
158 lines (145 loc) · 4.27 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Pre-install backend requirements into win-python site-packages
* so Windows installers ship a ready-to-run backend (no first-run pip).
*
* Uses requirements-prod.txt (no pytest/asyncpg/dev tools).
* Prunes test/cache junk after install to shrink the embed tree.
*/
const { spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const root = path.resolve(__dirname, '..');
const python = path.join(root, 'win-python', 'python.exe');
const reqProd = path.join(root, 'backend', 'requirements-prod.txt');
const reqFallback = path.join(root, 'backend', 'requirements.txt');
const req = fs.existsSync(reqProd) ? reqProd : reqFallback;
const sitePackages = path.join(root, 'win-python', 'Lib', 'site-packages');
if (process.platform !== 'win32') {
console.log('[prepare-win-python] skip (not Windows)');
process.exit(0);
}
if (!fs.existsSync(python)) {
console.warn('[prepare-win-python] win-python/python.exe not found — skip');
process.exit(0);
}
if (!fs.existsSync(req)) {
console.error('[prepare-win-python] requirements file missing:', req);
process.exit(1);
}
function hasModule(mod) {
const result = spawnSync(
python,
['-c', `import ${mod}`],
{
encoding: 'utf-8',
env: {
...process.env,
PYTHONNOUSERSITE: '1',
PYTHONPATH: '',
},
},
);
return result.status === 0;
}
function rmrf(target) {
try {
fs.rmSync(target, { recursive: true, force: true });
} catch {
// ignore
}
}
function pruneSitePackages() {
if (!fs.existsSync(sitePackages)) return;
for (const name of fs.readdirSync(sitePackages)) {
const lower = name.toLowerCase();
if (
lower === 'pytest' ||
lower === '_pytest' ||
lower.startsWith('pytest') ||
lower.startsWith('asgi_lifespan') ||
lower === 'asyncpg' ||
lower.startsWith('asyncpg-') ||
lower.endsWith('.pdb') ||
lower.endsWith('.chm') ||
name === '__pycache__'
) {
console.log(`[prepare-win-python] prune ${name}`);
rmrf(path.join(sitePackages, name));
}
}
const walkPrune = (dir, depth = 0) => {
if (depth > 4 || !fs.existsSync(dir)) return;
let entries;
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const ent of entries) {
const full = path.join(dir, ent.name);
if (ent.isDirectory()) {
if (
ent.name === 'tests' ||
ent.name === 'test' ||
ent.name === '__pycache__' ||
ent.name === 'testing'
) {
rmrf(full);
} else {
walkPrune(full, depth + 1);
}
}
}
};
walkPrune(sitePackages, 0);
}
const requiredModules = ['uvicorn', 'fastapi', 'mcp'];
const missing = requiredModules.filter((m) => !hasModule(m));
const forceReinstall = process.env.TEVARN_FORCE_PIP === '1';
if (!forceReinstall && missing.length === 0 && fs.existsSync(path.join(sitePackages, 'uvicorn'))) {
console.log('[prepare-win-python] critical deps present (uvicorn/fastapi/mcp), prune only');
pruneSitePackages();
process.exit(0);
}
console.log(
`[prepare-win-python] Installing prod deps into win-python from ${path.basename(req)} (missing: ${missing.join(', ') || 'force'}) ...`,
);
const result = spawnSync(
python,
['-m', 'pip', 'install', '-r', req, '--no-warn-script-location', '--disable-pip-version-check'],
{
stdio: 'inherit',
cwd: path.dirname(python),
env: {
...process.env,
PYTHONNOUSERSITE: '1',
PYTHONPATH: '',
},
},
);
if (result.status !== 0) {
console.error('[prepare-win-python] pip install failed');
process.exit(result.status || 1);
}
if (!hasModule('mcp')) {
console.log('[prepare-win-python] mcp still missing, installing mcp>=1.12.0 ...');
const mcpResult = spawnSync(
python,
['-m', 'pip', 'install', 'mcp>=1.12.0', '--no-warn-script-location', '--disable-pip-version-check'],
{
stdio: 'inherit',
cwd: path.dirname(python),
env: {
...process.env,
PYTHONNOUSERSITE: '1',
PYTHONPATH: '',
},
},
);
if (mcpResult.status !== 0) {
console.error('[prepare-win-python] mcp install failed');
process.exit(mcpResult.status || 1);
}
}
pruneSitePackages();
console.log('[prepare-win-python] done');