forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonEnvironment.ts
More file actions
163 lines (151 loc) · 6.47 KB
/
pythonEnvironment.ts
File metadata and controls
163 lines (151 loc) · 6.47 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
159
160
161
162
163
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { CondaEnvironmentInfo } from '../../pythonEnvironments/discovery/locators/services/conda';
import { buildPythonExecInfo, PythonExecInfo } from '../../pythonEnvironments/exec';
import { InterpreterInformation } from '../../pythonEnvironments/info';
import { getExecutablePath } from '../../pythonEnvironments/info/executable';
import { getInterpreterInfo } from '../../pythonEnvironments/info/interpreter';
import { traceError, traceInfo } from '../logger';
import { IFileSystem } from '../platform/types';
import * as internalPython from './internal/python';
import { ExecutionResult, IProcessService, ShellOptions, SpawnOptions } from './types';
class PythonEnvironment {
private cachedInterpreterInformation: InterpreterInformation | undefined | null = null;
constructor(
protected readonly pythonPath: string,
// "deps" is the externally defined functionality used by the class.
protected readonly deps: {
getPythonArgv(python: string): string[];
getObservablePythonArgv(python: string): string[];
isValidExecutable(python: string): Promise<boolean>;
// from ProcessService:
exec(file: string, args: string[]): Promise<ExecutionResult<string>>;
shellExec(command: string, timeout: number): Promise<ExecutionResult<string>>;
}
) {}
public getExecutionInfo(pythonArgs: string[] = []): PythonExecInfo {
const python = this.deps.getPythonArgv(this.pythonPath);
return buildPythonExecInfo(python, pythonArgs);
}
public getExecutionObservableInfo(pythonArgs: string[] = []): PythonExecInfo {
const python = this.deps.getObservablePythonArgv(this.pythonPath);
return buildPythonExecInfo(python, pythonArgs);
}
public async getInterpreterInformation(): Promise<InterpreterInformation | undefined> {
if (this.cachedInterpreterInformation === null) {
this.cachedInterpreterInformation = await this.getInterpreterInformationImpl();
}
return this.cachedInterpreterInformation;
}
public async getExecutablePath(): Promise<string> {
// If we've passed the python file, then return the file.
// This is because on mac if using the interpreter /usr/bin/python2.7 we can get a different value for the path
if (await this.deps.isValidExecutable(this.pythonPath)) {
return this.pythonPath;
}
const python = this.getExecutionInfo();
return getExecutablePath(python, this.deps.exec);
}
public async isModuleInstalled(moduleName: string): Promise<boolean> {
// prettier-ignore
const [args,] = internalPython.isModuleInstalled(moduleName);
const info = this.getExecutionInfo(args);
try {
await this.deps.exec(info.command, info.args);
} catch {
return false;
}
return true;
}
private async getInterpreterInformationImpl(): Promise<InterpreterInformation | undefined> {
try {
const python = this.getExecutionInfo();
return await getInterpreterInfo(python, this.deps.shellExec, { info: traceInfo, error: traceError });
} catch (ex) {
traceError(`Failed to get interpreter information for '${this.pythonPath}'`, ex);
}
}
}
function createDeps(
isValidExecutable: (filename: string) => Promise<boolean>,
pythonArgv: string[] | undefined,
observablePythonArgv: string[] | undefined,
// from ProcessService:
exec: (file: string, args: string[], options?: SpawnOptions) => Promise<ExecutionResult<string>>,
shellExec: (command: string, options?: ShellOptions) => Promise<ExecutionResult<string>>
) {
return {
getPythonArgv: (python: string) => pythonArgv || [python],
getObservablePythonArgv: (python: string) => observablePythonArgv || [python],
isValidExecutable,
exec: async (cmd: string, args: string[]) => exec(cmd, args, { throwOnStdErr: true }),
shellExec: async (text: string, timeout: number) => shellExec(text, { timeout })
};
}
export function createPythonEnv(
pythonPath: string,
// These are used to generate the deps.
procs: IProcessService,
fs: IFileSystem
): PythonEnvironment {
const deps = createDeps(
async (filename) => fs.fileExists(filename),
// We use the default: [pythonPath].
undefined,
undefined,
(file, args, opts) => procs.exec(file, args, opts),
(command, opts) => procs.shellExec(command, opts)
);
return new PythonEnvironment(pythonPath, deps);
}
export function createCondaEnv(
condaFile: string,
condaInfo: CondaEnvironmentInfo,
pythonPath: string,
// These are used to generate the deps.
procs: IProcessService,
fs: IFileSystem
): PythonEnvironment {
const runArgs = ['run'];
if (condaInfo.name === '') {
runArgs.push('-p', condaInfo.path);
} else {
runArgs.push('-n', condaInfo.name);
}
const pythonArgv = [condaFile, ...runArgs, 'python'];
const deps = createDeps(
async (filename) => fs.fileExists(filename),
pythonArgv,
// tslint:disable-next-line:no-suspicious-comment
// TODO: Use pythonArgv here once 'conda run' can be
// run without buffering output.
// See https://github.com/microsoft/vscode-python/issues/8473.
undefined,
(file, args, opts) => procs.exec(file, args, opts),
(command, opts) => procs.shellExec(command, opts)
);
return new PythonEnvironment(pythonPath, deps);
}
export function createWindowsStoreEnv(
pythonPath: string,
// These are used to generate the deps.
procs: IProcessService
): PythonEnvironment {
const deps = createDeps(
/**
* With windows store python apps, we have generally use the
* symlinked python executable. The actual file is not accessible
* by the user due to permission issues (& rest of exension fails
* when using that executable). Hence lets not resolve the
* executable using sys.executable for windows store python
* interpreters.
*/
async (_f: string) => true,
// We use the default: [pythonPath].
undefined,
undefined,
(file, args, opts) => procs.exec(file, args, opts),
(command, opts) => procs.shellExec(command, opts)
);
return new PythonEnvironment(pythonPath, deps);
}