Skip to content

Commit 7087647

Browse files
committed
refactor(satellite): simplify Python entry point resolution logic
1 parent b6ffe90 commit 7087647

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

services/satellite/src/process/github-deployment.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -902,24 +902,34 @@ export class GitHubDeploymentHandler {
902902
if (command === 'python3') {
903903
// Running with system python3 interpreter - make script path relative
904904
const relativeEntryPoint = path.relative(deploymentDir, entryPoint);
905+
906+
// Activate venv by setting VIRTUAL_ENV environment variable
907+
// This allows system python3 to find packages in .venv/lib/python3.x/site-packages
908+
//
909+
// Note: In production (nsjail), deployment dir is mounted as /app
910+
// So we set VIRTUAL_ENV=/app/.venv which is accessible inside nsjail
911+
// In development, we use the actual deployment directory path
912+
const isProduction = process.env.NODE_ENV === 'production';
913+
const venvPath = isProduction ? '/app/.venv' : path.join(deploymentDir, '.venv');
914+
const venvBinPath = isProduction ? '/app/.venv/bin' : path.join(deploymentDir, '.venv/bin');
915+
916+
const activatedEnv = {
917+
...config.env,
918+
VIRTUAL_ENV: venvPath,
919+
// Prepend .venv/bin to PATH so python3 uses venv packages
920+
PATH: `${venvBinPath}:${process.env.PATH || '/usr/bin:/bin'}`
921+
};
922+
905923
updatedConfig = {
906924
...config,
907925
command: 'python3',
908926
args: [relativeEntryPoint],
909-
temp_dir: deploymentDir
910-
};
911-
} else if (command.includes('.venv/bin/python')) {
912-
// Running with venv Python - use relative paths for both command and script
913-
const relativeCommand = path.relative(deploymentDir, command);
914-
const relativeEntryPoint = path.relative(deploymentDir, entryPoint);
915-
updatedConfig = {
916-
...config,
917-
command: relativeCommand, // .venv/bin/python
918-
args: [relativeEntryPoint], // server.py
919-
temp_dir: deploymentDir
927+
temp_dir: deploymentDir,
928+
env: activatedEnv
920929
};
921930
} else {
922931
// Running directly (entry point is executable script from venv bin)
932+
// Used for Pattern 1 (Installable Package) where [project.scripts] creates executables
923933
const relativeCommand = path.relative(deploymentDir, entryPoint);
924934
updatedConfig = {
925935
...config,

services/satellite/src/utils/python-helpers.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,30 +185,24 @@ export async function resolvePythonEntryPoint(
185185
// Fallback: look for __main__.py
186186
const mainPath = path.join(tempDir, '__main__.py');
187187
if (await fileExists(mainPath)) {
188-
// Use venv Python if available
189-
const venvPython = path.join(tempDir, '.venv', 'bin', 'python');
190-
const command = await fileExists(venvPython) ? venvPython : 'python3';
191-
return { command, entryPoint: mainPath };
188+
// Use system python3 (security validated, venv packages available via working directory)
189+
return { command: 'python3', entryPoint: mainPath };
192190
}
193191

194192
// Try src/__main__.py (common pattern)
195193
const srcMainPath = path.join(tempDir, 'src', '__main__.py');
196194
if (await fileExists(srcMainPath)) {
197-
// Use venv Python if available
198-
const venvPython = path.join(tempDir, '.venv', 'bin', 'python');
199-
const command = await fileExists(venvPython) ? venvPython : 'python3';
200-
return { command, entryPoint: srcMainPath };
195+
// Use system python3 (security validated, venv packages available via working directory)
196+
return { command: 'python3', entryPoint: srcMainPath };
201197
}
202198

203199
// Try common script names (server.py, main.py, app.py, run.py)
204200
const commonScriptNames = ['server.py', 'main.py', 'app.py', 'run.py'];
205201
for (const scriptName of commonScriptNames) {
206202
const scriptPath = path.join(tempDir, scriptName);
207203
if (await fileExists(scriptPath)) {
208-
// Use venv Python if available
209-
const venvPython = path.join(tempDir, '.venv', 'bin', 'python');
210-
const command = await fileExists(venvPython) ? venvPython : 'python3';
211-
return { command, entryPoint: scriptPath };
204+
// Use system python3 (security validated, venv packages available via working directory)
205+
return { command: 'python3', entryPoint: scriptPath };
212206
}
213207
}
214208

0 commit comments

Comments
 (0)