-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpyenvManager.ts
More file actions
329 lines (284 loc) · 11.8 KB
/
pyenvManager.ts
File metadata and controls
329 lines (284 loc) · 11.8 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
import * as path from 'path';
import { Disposable, EventEmitter, MarkdownString, ProgressLocation, Uri } from 'vscode';
import {
DidChangeEnvironmentEventArgs,
DidChangeEnvironmentsEventArgs,
EnvironmentChangeKind,
EnvironmentManager,
GetEnvironmentScope,
GetEnvironmentsScope,
IconPath,
PythonEnvironment,
PythonEnvironmentApi,
PythonProject,
RefreshEnvironmentsScope,
ResolveEnvironmentContext,
SetEnvironmentScope,
} from '../../api';
import { PyenvStrings } from '../../common/localize';
import { traceError, traceInfo } from '../../common/logging';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { withProgress } from '../../common/window.apis';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { getLatest } from '../common/utils';
import {
clearPyenvCache,
getPyenvForGlobal,
getPyenvForWorkspace,
PYENV_VERSIONS,
refreshPyenv,
resolvePyenvPath,
setPyenvForGlobal,
setPyenvForWorkspace,
setPyenvForWorkspaces,
} from './pyenvUtils';
export class PyEnvManager implements EnvironmentManager, Disposable {
private collection: PythonEnvironment[] = [];
private fsPathToEnv: Map<string, PythonEnvironment> = new Map();
private globalEnv: PythonEnvironment | undefined;
private readonly _onDidChangeEnvironment = new EventEmitter<DidChangeEnvironmentEventArgs>();
public readonly onDidChangeEnvironment = this._onDidChangeEnvironment.event;
private readonly _onDidChangeEnvironments = new EventEmitter<DidChangeEnvironmentsEventArgs>();
public readonly onDidChangeEnvironments = this._onDidChangeEnvironments.event;
constructor(private readonly nativeFinder: NativePythonFinder, private readonly api: PythonEnvironmentApi) {
this.name = 'pyenv';
this.displayName = 'PyEnv';
this.preferredPackageManagerId = 'ms-python.python:pip';
this.tooltip = new MarkdownString(PyenvStrings.pyenvManager, true);
}
name: string;
displayName: string;
preferredPackageManagerId: string;
description?: string;
tooltip: string | MarkdownString;
iconPath?: IconPath;
public dispose() {
this.collection = [];
this.fsPathToEnv.clear();
}
private _initialized: Deferred<void> | undefined;
async initialize(): Promise<void> {
if (this._initialized) {
return this._initialized.promise;
}
this._initialized = createDeferred();
await withProgress(
{
location: ProgressLocation.Window,
title: PyenvStrings.pyenvDiscovering,
},
async () => {
this.collection = await refreshPyenv(false, this.nativeFinder, this.api, this);
await this.loadEnvMap();
this._onDidChangeEnvironments.fire(
this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
);
},
);
this._initialized.resolve();
}
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {
await this.initialize();
if (scope === 'all') {
return Array.from(this.collection);
}
if (scope === 'global') {
return this.collection.filter((env) => env.group === PYENV_VERSIONS);
}
if (scope instanceof Uri) {
const env = this.fromEnvMap(scope);
if (env) {
return [env];
}
}
return [];
}
async refresh(context: RefreshEnvironmentsScope): Promise<void> {
if (context === undefined) {
await withProgress(
{
location: ProgressLocation.Window,
title: PyenvStrings.pyenvRefreshing,
},
async () => {
traceInfo('Refreshing Pyenv Environments');
const discard = this.collection.map((c) => c);
this.collection = await refreshPyenv(true, this.nativeFinder, this.api, this);
await this.loadEnvMap();
const args = [
...discard.map((env) => ({ kind: EnvironmentChangeKind.remove, environment: env })),
...this.collection.map((env) => ({ kind: EnvironmentChangeKind.add, environment: env })),
];
this._onDidChangeEnvironments.fire(args);
},
);
}
}
async get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
await this.initialize();
if (scope instanceof Uri) {
let env = this.fsPathToEnv.get(scope.fsPath);
if (env) {
return env;
}
const project = this.api.getPythonProject(scope);
if (project) {
env = this.fsPathToEnv.get(project.uri.fsPath);
if (env) {
return env;
}
}
}
return this.globalEnv;
}
async set(scope: SetEnvironmentScope, environment?: PythonEnvironment | undefined): Promise<void> {
if (scope === undefined) {
await setPyenvForGlobal(environment?.environmentPath?.fsPath);
} else if (scope instanceof Uri) {
const folder = this.api.getPythonProject(scope);
const fsPath = folder?.uri?.fsPath ?? scope.fsPath;
if (fsPath) {
if (environment) {
this.fsPathToEnv.set(fsPath, environment);
} else {
this.fsPathToEnv.delete(fsPath);
}
await setPyenvForWorkspace(fsPath, environment?.environmentPath?.fsPath);
}
} else if (Array.isArray(scope) && scope.every((u) => u instanceof Uri)) {
const projects: PythonProject[] = [];
scope
.map((s) => this.api.getPythonProject(s))
.forEach((p) => {
if (p) {
projects.push(p);
}
});
const before: Map<string, PythonEnvironment | undefined> = new Map();
projects.forEach((p) => {
before.set(p.uri.fsPath, this.fsPathToEnv.get(p.uri.fsPath));
if (environment) {
this.fsPathToEnv.set(p.uri.fsPath, environment);
} else {
this.fsPathToEnv.delete(p.uri.fsPath);
}
});
await setPyenvForWorkspaces(
projects.map((p) => p.uri.fsPath),
environment?.environmentPath?.fsPath,
);
projects.forEach((p) => {
const b = before.get(p.uri.fsPath);
if (b?.envId.id !== environment?.envId.id) {
this._onDidChangeEnvironment.fire({ uri: p.uri, old: b, new: environment });
}
});
}
}
async resolve(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined> {
await this.initialize();
if (context instanceof Uri) {
const env = await resolvePyenvPath(context.fsPath, this.nativeFinder, this.api, this);
if (env) {
const _collectionEnv = this.findEnvironmentByPath(env.environmentPath.fsPath);
if (_collectionEnv) {
return _collectionEnv;
}
this.collection.push(env);
this._onDidChangeEnvironments.fire([{ kind: EnvironmentChangeKind.add, environment: env }]);
return env;
}
return undefined;
}
}
async clearCache(): Promise<void> {
await clearPyenvCache();
}
private async loadEnvMap() {
this.globalEnv = undefined;
this.fsPathToEnv.clear();
// Try to find a global environment
const fsPath = await getPyenvForGlobal();
if (fsPath) {
this.globalEnv = this.findEnvironmentByPath(fsPath);
// If the environment is not found, resolve the fsPath. Could be portable conda.
if (!this.globalEnv) {
this.globalEnv = await resolvePyenvPath(fsPath, this.nativeFinder, this.api, this);
// If the environment is resolved, add it to the collection
if (this.globalEnv) {
this.collection.push(this.globalEnv);
}
}
}
if (!this.globalEnv) {
this.globalEnv = getLatest(this.collection.filter((e) => e.group === PYENV_VERSIONS));
}
// Find any pyenv environments that might be associated with the current projects
// These are environments whose parent dirs are project dirs.
const pathSorted = this.collection
.filter((e) => this.api.getPythonProject(e.environmentPath))
.sort((a, b) => {
if (a.environmentPath.fsPath !== b.environmentPath.fsPath) {
return a.environmentPath.fsPath.length - b.environmentPath.fsPath.length;
}
return a.environmentPath.fsPath.localeCompare(b.environmentPath.fsPath);
});
// Try to find workspace environments
const paths = this.api.getPythonProjects().map((p) => p.uri.fsPath);
for (const p of paths) {
const env = await getPyenvForWorkspace(p);
if (env) {
const found = this.findEnvironmentByPath(env);
if (found) {
this.fsPathToEnv.set(p, found);
} else {
// If not found, resolve the pyenv path. Could be portable pyenv.
const resolved = await resolvePyenvPath(env, this.nativeFinder, this.api, this);
if (resolved) {
// If resolved add it to the collection
this.fsPathToEnv.set(p, resolved);
this.collection.push(resolved);
} else {
traceError(`Failed to resolve pyenv environment: ${env}`);
}
}
} else {
// If there is not an environment already assigned by user to this project
// then see if there is one in the collection
if (pathSorted.length === 1) {
this.fsPathToEnv.set(p, pathSorted[0]);
} else {
// If there is more than one environment then we need to check if the project
// is a subfolder of one of the environments
const found = pathSorted.find((e) => {
const t = this.api.getPythonProject(e.environmentPath)?.uri.fsPath;
return t && path.normalize(t) === p;
});
if (found) {
this.fsPathToEnv.set(p, found);
}
}
}
}
}
private fromEnvMap(uri: Uri): PythonEnvironment | undefined {
// Find environment directly using the URI mapping
const env = this.fsPathToEnv.get(uri.fsPath);
if (env) {
return env;
}
// Find environment using the Python project for the Uri
const project = this.api.getPythonProject(uri);
if (project) {
return this.fsPathToEnv.get(project.uri.fsPath);
}
return undefined;
}
private findEnvironmentByPath(fsPath: string): PythonEnvironment | undefined {
const normalized = path.normalize(fsPath);
return this.collection.find((e) => {
const n = path.normalize(e.environmentPath.fsPath);
return n === normalized || path.dirname(n) === normalized || path.dirname(path.dirname(n)) === normalized;
});
}
}