forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpythonPathUpdaterService.ts
More file actions
95 lines (88 loc) · 3.86 KB
/
pythonPathUpdaterService.ts
File metadata and controls
95 lines (88 loc) · 3.86 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
import { inject, injectable } from 'inversify';
import { ConfigurationTarget, l10n, Uri, window } from 'vscode';
import { StopWatch } from '../../common/utils/stopWatch';
import { SystemVariables } from '../../common/variables/systemVariables';
import { traceError } from '../../logging';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { PythonInterpreterTelemetry } from '../../telemetry/types';
import { IComponentAdapter } from '../contracts';
import {
IRecommendedEnvironmentService,
IPythonPathUpdaterServiceFactory,
IPythonPathUpdaterServiceManager,
} from './types';
@injectable()
export class PythonPathUpdaterService implements IPythonPathUpdaterServiceManager {
constructor(
@inject(IPythonPathUpdaterServiceFactory)
private readonly pythonPathSettingsUpdaterFactory: IPythonPathUpdaterServiceFactory,
@inject(IComponentAdapter) private readonly pyenvs: IComponentAdapter,
@inject(IRecommendedEnvironmentService) private readonly preferredEnvService: IRecommendedEnvironmentService,
) {}
public async updatePythonPath(
pythonPath: string | undefined,
configTarget: ConfigurationTarget,
trigger: 'ui' | 'shebang' | 'load',
wkspace?: Uri,
): Promise<void> {
const stopWatch = new StopWatch();
const pythonPathUpdater = this.getPythonUpdaterService(configTarget, wkspace);
let failed = false;
try {
await pythonPathUpdater.updatePythonPath(pythonPath);
if (trigger === 'ui') {
this.preferredEnvService.trackUserSelectedEnvironment(pythonPath, wkspace);
}
} catch (err) {
failed = true;
const reason = err as Error;
const message = reason && typeof reason.message === 'string' ? (reason.message as string) : '';
window.showErrorMessage(l10n.t('Failed to set interpreter path. Error: {0}', message));
traceError(reason);
}
// do not wait for this to complete
this.sendTelemetry(stopWatch.elapsedTime, failed, trigger, pythonPath, wkspace).catch((ex) =>
traceError('Python Extension: sendTelemetry', ex),
);
}
private async sendTelemetry(
duration: number,
failed: boolean,
trigger: 'ui' | 'shebang' | 'load',
pythonPath: string | undefined,
wkspace?: Uri,
) {
const telemetryProperties: PythonInterpreterTelemetry = {
failed,
trigger,
};
if (!failed && pythonPath) {
const systemVariables = new SystemVariables(undefined, wkspace?.fsPath);
const interpreterInfo = await this.pyenvs.getInterpreterInformation(systemVariables.resolveAny(pythonPath));
if (interpreterInfo) {
telemetryProperties.pythonVersion = interpreterInfo.version?.raw;
}
}
sendTelemetryEvent(EventName.PYTHON_INTERPRETER, duration, telemetryProperties);
}
private getPythonUpdaterService(configTarget: ConfigurationTarget, wkspace?: Uri) {
switch (configTarget) {
case ConfigurationTarget.Global: {
return this.pythonPathSettingsUpdaterFactory.getGlobalPythonPathConfigurationService();
}
case ConfigurationTarget.Workspace: {
if (!wkspace) {
throw new Error('Workspace Uri not defined');
}
return this.pythonPathSettingsUpdaterFactory.getWorkspacePythonPathConfigurationService(wkspace!);
}
default: {
if (!wkspace) {
throw new Error('Workspace Uri not defined');
}
return this.pythonPathSettingsUpdaterFactory.getWorkspaceFolderPythonPathConfigurationService(wkspace!);
}
}
}
}