-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathsysPythonManager.ts
More file actions
361 lines (309 loc) · 13.5 KB
/
sysPythonManager.ts
File metadata and controls
361 lines (309 loc) · 13.5 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import * as path from 'path';
import { EventEmitter, LogOutputChannel, MarkdownString, ProgressLocation, ThemeIcon, Uri, window } from 'vscode';
import {
CreateEnvironmentOptions,
CreateEnvironmentScope,
DidChangeEnvironmentEventArgs,
DidChangeEnvironmentsEventArgs,
EnvironmentChangeKind,
EnvironmentManager,
GetEnvironmentScope,
GetEnvironmentsScope,
IconPath,
PythonEnvironment,
PythonEnvironmentApi,
PythonProject,
RefreshEnvironmentsScope,
ResolveEnvironmentContext,
SetEnvironmentScope,
} from '../../api';
import { SysManagerStrings } from '../../common/localize';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { getLatest } from '../common/utils';
import {
clearSystemEnvCache,
getSystemEnvForGlobal,
getSystemEnvForWorkspace,
setSystemEnvForGlobal,
setSystemEnvForWorkspace,
setSystemEnvForWorkspaces,
} from './cache';
import { refreshPythons, resolveSystemPythonEnvironmentPath } from './utils';
import { installPythonWithUv, promptInstallPythonViaUv, selectPythonVersionToInstall } from './uvPythonInstaller';
export class SysPythonManager implements EnvironmentManager {
private collection: PythonEnvironment[] = [];
private readonly 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;
public readonly name: string;
public readonly displayName: string;
public readonly preferredPackageManagerId: string;
public readonly description: string | undefined;
public readonly tooltip: string | MarkdownString;
public readonly iconPath: IconPath;
constructor(
private readonly nativeFinder: NativePythonFinder,
private readonly api: PythonEnvironmentApi,
public readonly log: LogOutputChannel,
) {
this.name = 'system';
this.displayName = 'Global';
this.preferredPackageManagerId = 'ms-python.python:pip';
this.description = undefined;
this.tooltip = new MarkdownString(SysManagerStrings.sysManagerDescription, true);
this.iconPath = new ThemeIcon('globe');
}
private _initialized: Deferred<void> | undefined;
async initialize(): Promise<void> {
if (this._initialized) {
return this._initialized.promise;
}
this._initialized = createDeferred();
try {
await this.internalRefresh(false, SysManagerStrings.sysManagerDiscovering);
// If no Python environments were found, offer to install via uv
if (this.collection.length === 0) {
const pythonPath = await promptInstallPythonViaUv('activation', this.log);
if (pythonPath) {
const resolved = await resolveSystemPythonEnvironmentPath(
pythonPath,
this.nativeFinder,
this.api,
this,
);
if (resolved) {
this.collection.push(resolved);
this.globalEnv = resolved;
await setSystemEnvForGlobal(resolved.environmentPath.fsPath);
this._onDidChangeEnvironments.fire([
{ environment: resolved, kind: EnvironmentChangeKind.add },
]);
}
}
}
} finally {
this._initialized.resolve();
}
}
refresh(_scope: RefreshEnvironmentsScope): Promise<void> {
return this.internalRefresh(true, SysManagerStrings.sysManagerRefreshing);
}
private async internalRefresh(hardRefresh: boolean, title: string) {
await window.withProgress(
{
location: ProgressLocation.Window,
title,
},
async () => {
const discard = this.collection.map((c) => c);
// hit here is fine...
this.collection = await refreshPythons(hardRefresh, this.nativeFinder, this.api, this.log, this);
await this.loadEnvMap();
const args = [
...discard.map((e) => ({ environment: e, kind: EnvironmentChangeKind.remove })),
...this.collection.map((e) => ({ environment: e, kind: EnvironmentChangeKind.add })),
];
this._onDidChangeEnvironments.fire(args);
},
);
}
async getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]> {
await this.initialize();
if (scope === 'all' || scope === 'global') {
return Array.from(this.collection);
}
if (scope instanceof Uri) {
const env = this.fsPathToEnv.get(scope.fsPath);
if (env) {
return [env];
}
}
return [];
}
async get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
await this.initialize();
if (scope instanceof Uri) {
return this.fromEnvMap(scope) ?? this.globalEnv;
}
return this.globalEnv;
}
async set(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void> {
if (scope === undefined) {
this.globalEnv = environment ?? getLatest(this.collection);
if (environment) {
await setSystemEnvForGlobal(environment.environmentPath.fsPath);
}
}
if (scope instanceof Uri) {
const pw = this.api.getPythonProject(scope);
if (!pw) {
this.log.warn(
`Unable to set environment for ${scope.fsPath}: Not a python project, folder or PEP723 script.`,
this.api.getPythonProjects().map((p) => p.uri.fsPath),
);
return;
}
if (environment) {
this.fsPathToEnv.set(pw.uri.fsPath, environment);
} else {
this.fsPathToEnv.delete(pw.uri.fsPath);
}
await setSystemEnvForWorkspace(pw.uri.fsPath, environment?.environmentPath.fsPath);
}
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 setSystemEnvForWorkspaces(
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> {
// NOTE: `environmentPath` for envs in `this.collection` for system envs always points to the python
// executable. This is set when we create the PythonEnvironment object.
const found = this.findEnvironmentByPath(context.fsPath);
if (found) {
// If it is in the collection, then it is a venv, and it should already be fully resolved.
return found;
}
// This environment is unknown. Resolve it.
const resolved = await resolveSystemPythonEnvironmentPath(context.fsPath, this.nativeFinder, this.api, this);
if (resolved) {
// This is just like finding a new environment or creating a new one.
// Add it to collection, and trigger the added event.
// For all other env types we need to ensure that the environment is of the type managed by the manager.
// But System is a exception, this is the last resort for resolving. So we don't need to check.
// We will just add it and treat it as a non-activatable environment.
const exists = this.collection.some(
(e) => e.environmentPath.toString() === resolved.environmentPath.toString(),
);
if (!exists) {
// only add it if it is not already in the collection to avoid duplicates
this.collection.push(resolved);
}
this._onDidChangeEnvironments.fire([{ environment: resolved, kind: EnvironmentChangeKind.add }]);
}
return resolved;
}
/**
* Installs a global Python using uv.
* This method shows a QuickPick to select the Python version, then installs it.
*/
async create(
_scope: CreateEnvironmentScope,
_options?: CreateEnvironmentOptions,
): Promise<PythonEnvironment | undefined> {
// Show QuickPick to select Python version
const selectedVersion = await selectPythonVersionToInstall();
if (!selectedVersion) {
// User cancelled
return undefined;
}
const pythonPath = await installPythonWithUv(this.log, selectedVersion);
if (pythonPath) {
// Resolve the installed Python using NativePythonFinder instead of full refresh
const resolved = await resolveSystemPythonEnvironmentPath(pythonPath, this.nativeFinder, this.api, this);
if (resolved) {
// Add to collection, update global env, and fire change event
this.collection.push(resolved);
this.globalEnv = resolved;
await setSystemEnvForGlobal(resolved.environmentPath.fsPath);
this._onDidChangeEnvironments.fire([{ environment: resolved, kind: EnvironmentChangeKind.add }]);
return resolved;
}
}
return undefined;
}
async clearCache(): Promise<void> {
await clearSystemEnvCache();
}
private findEnvironmentByPath(fsPath: string): PythonEnvironment | undefined {
const normalized = path.normalize(fsPath); // /opt/homebrew/bin/python3.12
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;
});
}
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 this.globalEnv;
}
private async loadEnvMap() {
this.globalEnv = undefined;
this.fsPathToEnv.clear();
// Try to find a global environment
const fsPath = await getSystemEnvForGlobal();
if (fsPath) {
this.globalEnv = this.findEnvironmentByPath(fsPath);
// If the environment is not found, resolve the fsPath.
if (!this.globalEnv) {
this.globalEnv = await resolveSystemPythonEnvironmentPath(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 a global environment is still not set, try using the latest environment
if (!this.globalEnv) {
this.globalEnv = getLatest(this.collection);
}
// Try to find workspace environments
const paths = this.api.getPythonProjects().map((p) => p.uri.fsPath);
// Iterate over each path
for (const p of paths) {
const env = await getSystemEnvForWorkspace(p);
if (env) {
const found = this.findEnvironmentByPath(env);
if (found) {
this.fsPathToEnv.set(p, found);
} else {
// If not found, resolve the path.
const resolved = await resolveSystemPythonEnvironmentPath(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 {
this.log.error(`Failed to resolve python environment: ${env}`);
}
}
}
}
}
}