-
Notifications
You must be signed in to change notification settings - Fork 293
/
pythonApi.ts
457 lines (421 loc) · 18.9 KB
/
pythonApi.ts
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// eslint-disable-next-line
/* eslint-disable comma-dangle */
// eslint-disable-next-line
/* eslint-disable max-classes-per-file */
// eslint-disable-next-line
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
// eslint-disable-next-line
/* eslint-disable class-methods-use-this */
// eslint-disable-next-line
/* eslint-disable consistent-return */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable, named } from 'inversify';
import { CancellationToken, Disposable, Event, EventEmitter, Memento, Uri, workspace } from 'vscode';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../common/application/types';
import { isCI } from '../common/constants';
import { trackPackageInstalledIntoInterpreter } from '../common/installer/productInstaller';
import { ProductNames } from '../common/installer/productNames';
import { InterpreterUri } from '../common/installer/types';
import { traceInfo, traceInfoIf } from '../common/logger';
import {
GLOBAL_MEMENTO,
IDisposableRegistry,
IExtensions,
IMemento,
InstallerResponse,
Product,
ProductInstallStatus,
Resource
} from '../common/types';
import { createDeferred } from '../common/utils/async';
import * as localize from '../common/utils/localize';
import { isResource, noop } from '../common/utils/misc';
import { PythonExtension, Telemetry } from '../datascience/constants';
import { InterpreterPackages } from '../datascience/telemetry/interpreterPackages';
import { IEnvironmentActivationService } from '../interpreter/activation/types';
import { IInterpreterQuickPickItem, IInterpreterSelector } from '../interpreter/configuration/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IWindowsStoreInterpreter } from '../interpreter/locators/types';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { areInterpreterPathsSame } from '../pythonEnvironments/info/interpreter';
import { captureTelemetry, sendTelemetryEvent } from '../telemetry';
import {
ILanguageServer,
ILanguageServerProvider,
IPythonApiProvider,
IPythonDebuggerPathProvider,
IPythonExtensionChecker,
IPythonInstaller,
JupyterProductToInstall,
PythonApi
} from './types';
/* eslint-disable max-classes-per-file */
@injectable()
export class PythonApiProvider implements IPythonApiProvider {
private readonly api = createDeferred<PythonApi>();
private readonly didActivatePython = new EventEmitter<void>();
public get onDidActivatePythonExtension() {
return this.didActivatePython.event;
}
private initialized?: boolean;
private hooksRegistered?: boolean;
constructor(
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(IPythonExtensionChecker) private extensionChecker: IPythonExtensionChecker,
@inject(IWorkspaceService) private workspace: IWorkspaceService
) {
const previouslyInstalled = this.extensionChecker.isPythonExtensionInstalled;
if (!previouslyInstalled) {
this.extensions.onDidChange(
async () => {
if (this.extensionChecker.isPythonExtensionInstalled) {
await this.registerHooks();
}
},
this,
this.disposables
);
}
this.disposables.push(this.didActivatePython);
}
public getApi(): Promise<PythonApi> {
this.init().catch(noop);
return this.api.promise;
}
public setApi(api: PythonApi): void {
// Never allow accessing python API (we dont want to ever use the API and run code in untrusted API).
// Don't assume Python API will always be disabled in untrusted worksapces.
if (this.api.resolved || !this.workspace.isTrusted) {
return;
}
this.api.resolve(api);
// Log experiment status here. Python extension is definitely loaded at this point.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pythonConfig = workspace.getConfiguration('python', (null as any) as Uri);
const experimentsSection = pythonConfig.get('experiments');
traceInfo(`Experiment status for python is ${JSON.stringify(experimentsSection)}`);
}
private async init() {
if (this.initialized) {
return;
}
this.initialized = true;
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtension);
if (!pythonExtension) {
await this.extensionChecker.showPythonExtensionInstallRequiredPrompt();
} else {
await this.registerHooks();
}
}
private async registerHooks() {
if (this.hooksRegistered) {
return;
}
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtension);
if (!pythonExtension) {
return;
}
this.hooksRegistered = true;
if (!pythonExtension.isActive) {
await pythonExtension.activate();
this.didActivatePython.fire();
}
pythonExtension.exports.jupyter.registerHooks();
}
}
@injectable()
export class PythonExtensionChecker implements IPythonExtensionChecker {
private extensionChangeHandler: Disposable | undefined;
private waitingOnInstallPrompt?: Promise<void>;
constructor(
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IApplicationShell) private readonly appShell: IApplicationShell,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService
) {
// If the python extension is not installed listen to see if anything does install it
if (!this.isPythonExtensionInstalled) {
this.extensionChangeHandler = this.extensions.onDidChange(this.extensionsChangeHandler.bind(this));
}
}
public get isPythonExtensionInstalled() {
return this.extensions.getExtension(PythonExtension) !== undefined;
}
public get isPythonExtensionActive() {
return this.extensions.getExtension(PythonExtension)?.isActive === true;
}
public async showPythonExtensionInstallRequiredPrompt(): Promise<void> {
// If workspace is not trusted, then don't show prompt
if (!this.workspace.isTrusted) {
return;
}
if (this.waitingOnInstallPrompt) {
return this.waitingOnInstallPrompt;
}
// Ask user if they want to install and then wait for them to actually install it.
const yes = localize.Common.bannerLabelYes();
const no = localize.Common.bannerLabelNo();
sendTelemetryEvent(Telemetry.PythonExtensionNotInstalled, undefined, { action: 'displayed' });
const answer = await this.appShell.showErrorMessage(localize.DataScience.pythonExtensionRequired(), yes, no);
if (answer === yes) {
sendTelemetryEvent(Telemetry.PythonExtensionNotInstalled, undefined, { action: 'download' });
await this.installPythonExtension();
} else {
sendTelemetryEvent(Telemetry.PythonExtensionNotInstalled, undefined, { action: 'dismissed' });
}
}
private async installPythonExtension() {
// Have the user install python
void this.commandManager.executeCommand('extension.open', PythonExtension);
}
private async extensionsChangeHandler(): Promise<void> {
// On extension change see if python was installed, if so unhook our extension change watcher and
// notify the user that they might need to restart notebooks or interactive windows
if (this.isPythonExtensionInstalled && this.extensionChangeHandler) {
this.extensionChangeHandler.dispose();
this.extensionChangeHandler = undefined;
this.appShell
.showInformationMessage(localize.DataScience.pythonExtensionInstalled(), localize.Common.ok())
.then(noop, noop);
}
}
}
@injectable()
export class LanguageServerProvider implements ILanguageServerProvider {
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
public getLanguageServer(resource?: InterpreterUri): Promise<ILanguageServer | undefined> {
return this.apiProvider.getApi().then((api) => api.getLanguageServer(resource));
}
}
@injectable()
export class WindowsStoreInterpreter implements IWindowsStoreInterpreter {
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
public isWindowsStoreInterpreter(pythonPath: string): Promise<boolean> {
return this.apiProvider.getApi().then((api) => api.isWindowsStoreInterpreter(pythonPath));
}
}
@injectable()
export class PythonDebuggerPathProvider implements IPythonDebuggerPathProvider {
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
public getDebuggerPath(): Promise<string> {
return this.apiProvider.getApi().then((api) => api.getDebuggerPath());
}
}
const ProductMapping: { [key in Product]: JupyterProductToInstall } = {
[Product.ipykernel]: JupyterProductToInstall.ipykernel,
[Product.jupyter]: JupyterProductToInstall.jupyter,
[Product.kernelspec]: JupyterProductToInstall.kernelspec,
[Product.nbconvert]: JupyterProductToInstall.nbconvert,
[Product.notebook]: JupyterProductToInstall.notebook,
[Product.pandas]: JupyterProductToInstall.pandas
};
/* eslint-disable max-classes-per-file */
@injectable()
export class PythonInstaller implements IPythonInstaller {
private readonly _onInstalled = new EventEmitter<{ product: Product; resource?: InterpreterUri }>();
public get onInstalled(): Event<{ product: Product; resource?: InterpreterUri }> {
return this._onInstalled.event;
}
constructor(
@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider,
@inject(InterpreterPackages) private readonly interpreterPackages: InterpreterPackages,
@inject(IMemento) @named(GLOBAL_MEMENTO) private readonly memento: Memento
) {}
public async install(
product: Product,
resource?: InterpreterUri,
cancel?: CancellationToken,
reInstallAndUpdate?: boolean
): Promise<InstallerResponse> {
if (resource && !isResource(resource)) {
this.interpreterPackages.trackPackages(resource);
}
let action: 'installed' | 'failed' | 'disabled' | 'ignored' = 'installed';
try {
const api = await this.apiProvider.getApi();
const result = await api.install(ProductMapping[product], resource, cancel, reInstallAndUpdate);
trackPackageInstalledIntoInterpreter(this.memento, product, resource).catch(noop);
if (result === InstallerResponse.Installed) {
this._onInstalled.fire({ product, resource });
}
switch (result) {
case InstallerResponse.Installed:
action = 'installed';
break;
case InstallerResponse.Ignore:
action = 'ignored';
break;
case InstallerResponse.Disabled:
action = 'disabled';
break;
default:
break;
}
return result;
} catch (ex) {
action = 'failed';
throw ex;
} finally {
sendTelemetryEvent(Telemetry.PythonModuleInstal, undefined, {
action,
moduleName: ProductNames.get(product)!
});
}
}
public async isProductVersionCompatible(
product: Product,
semVerRequirement: string,
resource?: PythonEnvironment
): Promise<ProductInstallStatus> {
const api = await this.apiProvider.getApi();
return api.isProductVersionCompatible(product, semVerRequirement, resource);
}
}
// eslint-disable-next-line max-classes-per-file
@injectable()
export class EnvironmentActivationService implements IEnvironmentActivationService {
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
public async getActivatedEnvironmentVariables(
resource: Resource,
interpreter?: PythonEnvironment
): Promise<NodeJS.ProcessEnv | undefined> {
return this.apiProvider
.getApi()
.then((api) => api.getActivatedEnvironmentVariables(resource, interpreter, false));
}
}
// eslint-disable-next-line max-classes-per-file
@injectable()
export class InterpreterSelector implements IInterpreterSelector {
constructor(@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider) {}
public async getSuggestions(resource: Resource): Promise<IInterpreterQuickPickItem[]> {
return this.apiProvider.getApi().then((api) => api.getSuggestions(resource));
}
}
// eslint-disable-next-line max-classes-per-file
@injectable()
export class InterpreterService implements IInterpreterService {
private readonly didChangeInterpreter = new EventEmitter<void>();
private eventHandlerAdded?: boolean;
private interpreterListCachePromise: Promise<PythonEnvironment[]> | undefined = undefined;
constructor(
@inject(IPythonApiProvider) private readonly apiProvider: IPythonApiProvider,
@inject(IPythonExtensionChecker) private extensionChecker: IPythonExtensionChecker,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(IWorkspaceService) private workspace: IWorkspaceService
) {
if (this.extensionChecker.isPythonExtensionInstalled) {
if (!this.extensionChecker.isPythonExtensionActive) {
// This event may not fire. It only fires if we're the reason for python extension
// activation. VS code does not fire such an event itself if something else activates
this.apiProvider.onDidActivatePythonExtension(
this.hookupOnDidChangeInterpreterEvent,
this,
this.disposables
);
}
}
this.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, disposables);
}
public get onDidChangeInterpreter(): Event<void> {
this.hookupOnDidChangeInterpreterEvent();
return this.didChangeInterpreter.event;
}
@captureTelemetry(Telemetry.InterpreterListingPerf)
public getInterpreters(resource?: Uri): Promise<PythonEnvironment[]> {
this.hookupOnDidChangeInterpreterEvent();
// Cache result as it only changes when the interpreter list changes or we add more workspace folders
if (!this.interpreterListCachePromise) {
this.interpreterListCachePromise = this.getInterpretersImpl(resource);
}
return this.interpreterListCachePromise;
}
private workspaceCachedActiveInterpreter = new Map<string, Promise<PythonEnvironment | undefined>>();
@captureTelemetry(Telemetry.ActiveInterpreterListingPerf)
public getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined> {
this.hookupOnDidChangeInterpreterEvent();
const workspaceId = this.workspace.getWorkspaceFolderIdentifier(resource);
let promise = this.workspaceCachedActiveInterpreter.get(workspaceId);
if (!promise) {
promise = this.apiProvider.getApi().then((api) => api.getActiveInterpreter(resource));
if (promise) {
this.workspaceCachedActiveInterpreter.set(workspaceId, promise);
// If there was a problem in getting the details, remove the cached info.
promise.catch(() => {
if (this.workspaceCachedActiveInterpreter.get(workspaceId) === promise) {
this.workspaceCachedActiveInterpreter.delete(workspaceId);
}
});
if (isCI) {
promise
.then((item) =>
traceInfo(`Active Interpreter in Python API for ${resource?.toString()} is ${item?.path}`)
)
.catch(noop);
}
}
}
return promise;
}
public async getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<undefined | PythonEnvironment> {
this.hookupOnDidChangeInterpreterEvent();
try {
return await this.apiProvider.getApi().then((api) => api.getInterpreterDetails(pythonPath, resource));
} catch {
// If the python extension cannot get the details here, don't fail. Just don't use them.
return undefined;
}
}
private onDidChangeWorkspaceFolders() {
this.interpreterListCachePromise = undefined;
}
private async getInterpretersImpl(resource?: Uri): Promise<PythonEnvironment[]> {
// Python uses the resource to look up the workspace folder. For Jupyter
// we want all interpreters regardless of workspace folder so call this multiple times
const folders = this.workspace.workspaceFolders;
const all = folders
? await Promise.all(folders.map((f) => this.apiProvider.getApi().then((api) => api.getInterpreters(f.uri))))
: await Promise.all([this.apiProvider.getApi().then((api) => api.getInterpreters(undefined))]);
// Remove dupes
const result: PythonEnvironment[] = [];
all.flat().forEach((p) => {
if (!result.find((r) => areInterpreterPathsSame(r.path, p.path))) {
result.push(p);
}
});
traceInfoIf(isCI, `Interpreter list for ${resource?.toString()} is ${result.map((i) => i.path).join('\n')}`);
return result;
}
private hookupOnDidChangeInterpreterEvent() {
// Only do this once.
if (this.eventHandlerAdded) {
return;
}
// Python may not be installed or active
if (!this.extensionChecker.isPythonExtensionInstalled) {
return;
}
if (!this.extensionChecker.isPythonExtensionActive) {
return;
}
this.apiProvider
.getApi()
.then((api) => {
if (!this.eventHandlerAdded) {
this.eventHandlerAdded = true;
api.onDidChangeInterpreter(
() => {
this.interpreterListCachePromise = undefined;
this.workspaceCachedActiveInterpreter.clear();
this.didChangeInterpreter.fire();
},
this,
this.disposables
);
}
})
.catch(noop);
}
}