mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(cli): text-to-speech ignores a foreign PYTHONPATH when it looks for kokoro-onnx (#6769)
* fix(cli): text-to-speech ignores a foreign PYTHONPATH when it looks for kokoro-onnx * fix(cli): shorten the pythonEnv comment * fix(cli): text-to-speech keeps a deliberate PYTHONPATH unless the packages fail with it * fix(cli): drop the python.ts comments the names already say
This commit is contained in:
@@ -77,19 +77,38 @@ export function describeRejectedPythonOverride(): string | null {
|
||||
return `HYPERFRAMES_PYTHON="${override}" was rejected: ${validation.reason}`;
|
||||
}
|
||||
|
||||
/** True if `import <pkg>` succeeds — actually executes the module. */
|
||||
export function hasPythonPackage(python: string, pkg: string): boolean {
|
||||
function withoutPythonPath(): NodeJS.ProcessEnv {
|
||||
const { PYTHONPATH: _foreign, ...env } = process.env;
|
||||
return env;
|
||||
}
|
||||
|
||||
function runs(python: string, code: string, env: NodeJS.ProcessEnv): boolean {
|
||||
try {
|
||||
execFileSync(python, ["-c", `import ${pkg}`], {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
timeout: 10_000,
|
||||
});
|
||||
execFileSync(python, ["-c", code], { stdio: ["pipe", "pipe", "pipe"], timeout: 10_000, env });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function environmentThatRuns(python: string, code: string): NodeJS.ProcessEnv | null {
|
||||
return [process.env, withoutPythonPath()].find((env) => runs(python, code, env)) ?? null;
|
||||
}
|
||||
|
||||
export function workingPythonEnv(
|
||||
python: string,
|
||||
modules: string[],
|
||||
extra: NodeJS.ProcessEnv = {},
|
||||
): NodeJS.ProcessEnv | null {
|
||||
const env = environmentThatRuns(python, `import ${modules.join(", ")}`);
|
||||
return env && { ...env, ...extra };
|
||||
}
|
||||
|
||||
/** True if `import <pkg>` succeeds — actually executes the module. */
|
||||
export function hasPythonPackage(python: string, pkg: string): boolean {
|
||||
return environmentThatRuns(python, `import ${pkg}`) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if every module is installed, checked via `importlib.util.find_spec`
|
||||
* so heavy packages (torch) are never imported — fast enough for a preflight.
|
||||
@@ -100,13 +119,5 @@ export function hasPythonModules(modules: string[]): boolean {
|
||||
if (!python) return false;
|
||||
const list = JSON.stringify(modules);
|
||||
const probe = `import importlib.util,sys; sys.exit(0 if all(importlib.util.find_spec(m) for m in ${list}) else 1)`;
|
||||
try {
|
||||
execFileSync(python, ["-c", probe], {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
timeout: 10_000,
|
||||
});
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return environmentThatRuns(python, probe) !== null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user