forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutable.ts
More file actions
32 lines (30 loc) · 1.21 KB
/
executable.ts
File metadata and controls
32 lines (30 loc) · 1.21 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { getExecutable } from '../../common/process/internal/python';
import { ShellExecFunc } from '../../common/process/types';
import { copyPythonExecInfo, PythonExecInfo } from '../exec';
/**
* Find the filename for the corresponding Python executable.
*
* Effectively, we look up `sys.executable`.
*
* @param python - the information to use when running Python
* @param shellExec - the function to use to run Python
*/
export async function getExecutablePath(
python: PythonExecInfo,
shellExec: ShellExecFunc,
timeout?: number,
): Promise<string> {
const [args, parse] = getExecutable();
const info = copyPythonExecInfo(python, args);
const argv = [info.command, ...info.args];
// Concat these together to make a set of quoted strings
const quoted = argv.reduce((p, c) => (p ? `${p} ${c.toCommandArgument()}` : `${c.toCommandArgument()}`), '');
const result = await shellExec(quoted, { timeout: timeout ?? 15000 });
const executable = parse(result.stdout.trim());
if (executable === '') {
throw new Error(`${quoted} resulted in empty stdout`);
}
return executable;
}