forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvExtApi.ts
More file actions
345 lines (303 loc) · 11.9 KB
/
envExtApi.ts
File metadata and controls
345 lines (303 loc) · 11.9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable class-methods-use-this */
import * as path from 'path';
import { Event, EventEmitter, Disposable, Uri } from 'vscode';
import { PythonEnvInfo, PythonEnvKind, PythonEnvType, PythonVersion } from '../pythonEnvironments/base/info';
import {
GetRefreshEnvironmentsOptions,
IDiscoveryAPI,
ProgressNotificationEvent,
ProgressReportStage,
PythonLocatorQuery,
TriggerRefreshOptions,
} from '../pythonEnvironments/base/locator';
import { PythonEnvCollectionChangedEvent } from '../pythonEnvironments/base/watcher';
import { getEnvExtApi } from './api.internal';
import { createDeferred, Deferred } from '../common/utils/async';
import { StopWatch } from '../common/utils/stopWatch';
import { traceError, traceLog, traceWarn } from '../logging';
import {
DidChangeEnvironmentsEventArgs,
EnvironmentChangeKind,
PythonEnvironment,
PythonEnvironmentApi,
} from './types';
import { FileChangeType } from '../common/platform/fileSystemWatcher';
import { Architecture, isWindows } from '../common/utils/platform';
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
import { Interpreters } from '../common/utils/localize';
function getKind(pythonEnv: PythonEnvironment): PythonEnvKind {
if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) {
return PythonEnvKind.System;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) {
return PythonEnvKind.Conda;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('venv')) {
return PythonEnvKind.Venv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenv')) {
return PythonEnvKind.VirtualEnv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('virtualenvwrapper')) {
return PythonEnvKind.VirtualEnvWrapper;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pyenv')) {
return PythonEnvKind.Pyenv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pipenv')) {
return PythonEnvKind.Pipenv;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('poetry')) {
return PythonEnvKind.Poetry;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('pixi')) {
return PythonEnvKind.Pixi;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('hatch')) {
return PythonEnvKind.Hatch;
}
if (pythonEnv.envId.managerId.toLowerCase().endsWith('activestate')) {
return PythonEnvKind.ActiveState;
}
return PythonEnvKind.Unknown;
}
function makeExecutablePath(prefix?: string): string {
if (!prefix) {
return process.platform === 'win32' ? 'python.exe' : 'python';
}
return process.platform === 'win32' ? path.join(prefix, 'python.exe') : path.join(prefix, 'python');
}
function getExecutable(pythonEnv: PythonEnvironment): string {
if (pythonEnv.execInfo?.run?.executable) {
return pythonEnv.execInfo?.run?.executable;
}
const basename = path.basename(pythonEnv.environmentPath.fsPath).toLowerCase();
if (isWindows() && basename.startsWith('python') && basename.endsWith('.exe')) {
return pythonEnv.environmentPath.fsPath;
}
if (!isWindows() && basename.startsWith('python')) {
return pythonEnv.environmentPath.fsPath;
}
return makeExecutablePath(pythonEnv.sysPrefix);
}
function getLocation(pythonEnv: PythonEnvironment): string {
if (pythonEnv.envId.managerId.toLowerCase().endsWith('conda')) {
return pythonEnv.sysPrefix;
}
return pythonEnv.environmentPath.fsPath;
}
function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
switch (kind) {
case PythonEnvKind.Poetry:
case PythonEnvKind.Pyenv:
case PythonEnvKind.VirtualEnv:
case PythonEnvKind.Venv:
case PythonEnvKind.VirtualEnvWrapper:
case PythonEnvKind.OtherVirtual:
case PythonEnvKind.Pipenv:
case PythonEnvKind.ActiveState:
case PythonEnvKind.Hatch:
case PythonEnvKind.Pixi:
return PythonEnvType.Virtual;
case PythonEnvKind.Conda:
return PythonEnvType.Conda;
case PythonEnvKind.System:
case PythonEnvKind.Unknown:
case PythonEnvKind.OtherGlobal:
case PythonEnvKind.Custom:
case PythonEnvKind.MicrosoftStore:
default:
return undefined;
}
}
function toPythonEnvInfo(pythonEnv: PythonEnvironment): PythonEnvInfo | undefined {
const kind = getKind(pythonEnv);
const arch = Architecture.x64;
const version: PythonVersion = parseVersion(pythonEnv.version);
const { name, displayName, sysPrefix } = pythonEnv;
const executable = getExecutable(pythonEnv);
const location = getLocation(pythonEnv);
return {
name,
location,
kind,
id: executable,
executable: {
filename: executable,
sysPrefix,
ctime: -1,
mtime: -1,
},
version: {
sysVersion: pythonEnv.version,
major: version.major,
minor: version.minor,
micro: version.micro,
},
arch,
distro: {
org: '',
},
source: [],
detailedDisplayName: displayName,
display: displayName,
type: getEnvType(kind),
};
}
function hasChanged(old: PythonEnvInfo, newEnv: PythonEnvInfo): boolean {
if (old.executable.filename !== newEnv.executable.filename) {
return true;
}
if (old.version.major !== newEnv.version.major) {
return true;
}
if (old.version.minor !== newEnv.version.minor) {
return true;
}
if (old.version.micro !== newEnv.version.micro) {
return true;
}
if (old.location !== newEnv.location) {
return true;
}
if (old.kind !== newEnv.kind) {
return true;
}
if (old.arch !== newEnv.arch) {
return true;
}
return false;
}
class EnvExtApis implements IDiscoveryAPI, Disposable {
private _onProgress: EventEmitter<ProgressNotificationEvent>;
private _onChanged: EventEmitter<PythonEnvCollectionChangedEvent>;
private _refreshPromise?: Deferred<void>;
private _envs: PythonEnvInfo[] = [];
refreshState: ProgressReportStage;
private _disposables: Disposable[] = [];
constructor(private envExtApi: PythonEnvironmentApi) {
this._onProgress = new EventEmitter<ProgressNotificationEvent>();
this._onChanged = new EventEmitter<PythonEnvCollectionChangedEvent>();
this.onProgress = this._onProgress.event;
this.onChanged = this._onChanged.event;
this.refreshState = ProgressReportStage.idle;
this._disposables.push(
this._onProgress,
this._onChanged,
this.envExtApi.onDidChangeEnvironments((e) => this.onDidChangeEnvironments(e)),
this.envExtApi.onDidChangeEnvironment((e) => {
this._onChanged.fire({
type: FileChangeType.Changed,
searchLocation: e.uri,
old: e.old ? toPythonEnvInfo(e.old) : undefined,
new: e.new ? toPythonEnvInfo(e.new) : undefined,
});
}),
);
}
onProgress: Event<ProgressNotificationEvent>;
onChanged: Event<PythonEnvCollectionChangedEvent>;
getRefreshPromise(_options?: GetRefreshEnvironmentsOptions): Promise<void> | undefined {
return this._refreshPromise?.promise;
}
triggerRefresh(_query?: PythonLocatorQuery, _options?: TriggerRefreshOptions): Promise<void> {
const stopwatch = new StopWatch();
traceLog('Native locator: Refresh started');
if (this.refreshState === ProgressReportStage.discoveryStarted && this._refreshPromise?.promise) {
return this._refreshPromise?.promise;
}
this.refreshState = ProgressReportStage.discoveryStarted;
this._onProgress.fire({ stage: this.refreshState });
this._refreshPromise = createDeferred();
const SLOW_DISCOVERY_THRESHOLD_MS = 25_000;
const slowDiscoveryTimer = setTimeout(() => {
traceWarn(Interpreters.envExtDiscoverySlow);
}, SLOW_DISCOVERY_THRESHOLD_MS);
setImmediate(async () => {
try {
await this.envExtApi.refreshEnvironments(undefined);
if (this._envs.length === 0) {
traceWarn(Interpreters.envExtDiscoveryNoEnvironments);
}
this._refreshPromise?.resolve();
} catch (error) {
traceError(Interpreters.envExtDiscoveryFailed, error);
this._refreshPromise?.reject(error);
} finally {
clearTimeout(slowDiscoveryTimer);
traceLog(`Native locator: Refresh finished in ${stopwatch.elapsedTime} ms`);
this.refreshState = ProgressReportStage.discoveryFinished;
this._refreshPromise = undefined;
this._onProgress.fire({ stage: this.refreshState });
}
});
return this._refreshPromise?.promise;
}
getEnvs(_query?: PythonLocatorQuery): PythonEnvInfo[] {
return this._envs;
}
private addEnv(pythonEnv: PythonEnvironment, searchLocation?: Uri): PythonEnvInfo | undefined {
const info = toPythonEnvInfo(pythonEnv);
if (info) {
const old = this._envs.find((item) => item.executable.filename === info.executable.filename);
if (old) {
this._envs = this._envs.filter((item) => item.executable.filename !== info.executable.filename);
this._envs.push(info);
if (hasChanged(old, info)) {
this._onChanged.fire({ type: FileChangeType.Changed, old, new: info, searchLocation });
}
} else {
this._envs.push(info);
this._onChanged.fire({ type: FileChangeType.Created, new: info, searchLocation });
}
}
return info;
}
private removeEnv(env: PythonEnvInfo | string): void {
if (typeof env === 'string') {
const old = this._envs.find((item) => item.executable.filename === env);
this._envs = this._envs.filter((item) => item.executable.filename !== env);
this._onChanged.fire({ type: FileChangeType.Deleted, old });
return;
}
this._envs = this._envs.filter((item) => item.executable.filename !== env.executable.filename);
this._onChanged.fire({ type: FileChangeType.Deleted, old: env });
}
async resolveEnv(envPath?: string): Promise<PythonEnvInfo | undefined> {
if (envPath === undefined) {
return undefined;
}
try {
const pythonEnv = await this.envExtApi.resolveEnvironment(Uri.file(envPath));
if (pythonEnv) {
return this.addEnv(pythonEnv);
}
} catch (error) {
traceError(
`Failed to resolve environment "${envPath}" via the Python Environments extension (ms-python.vscode-python-envs). Check the "Python Environments" output channel for details.`,
error,
);
}
return undefined;
}
dispose(): void {
this._disposables.forEach((d) => d.dispose());
}
onDidChangeEnvironments(e: DidChangeEnvironmentsEventArgs): void {
e.forEach((item) => {
if (item.kind === EnvironmentChangeKind.remove) {
this.removeEnv(item.environment.environmentPath.fsPath);
}
if (item.kind === EnvironmentChangeKind.add) {
this.addEnv(item.environment);
}
});
}
}
export async function createEnvExtApi(disposables: Disposable[]): Promise<EnvExtApis> {
const api = new EnvExtApis(await getEnvExtApi());
disposables.push(api);
return api;
}