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
37 lines (35 loc) · 1.4 KB
/
executable.ts
File metadata and controls
37 lines (35 loc) · 1.4 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
// 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 { traceError } from '../../logging';
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): Promise<string | undefined> {
try {
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.toCommandArgumentForPythonExt()}` : `${c.toCommandArgumentForPythonExt()}`),
'',
);
const result = await shellExec(quoted, { timeout: 15000 });
const executable = parse(result.stdout.trim());
if (executable === '') {
throw new Error(`${quoted} resulted in empty stdout`);
}
return executable;
} catch (ex) {
traceError(ex);
return undefined;
}
}