forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeprecatedProposedApi.ts
More file actions
166 lines (159 loc) · 7.25 KB
/
deprecatedProposedApi.ts
File metadata and controls
166 lines (159 loc) · 7.25 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConfigurationTarget, EventEmitter } from 'vscode';
import { arePathsSame } from './common/platform/fs-paths';
import { IExtensions, IInterpreterPathService, Resource } from './common/types';
import {
EnvironmentsChangedParams,
ActiveEnvironmentChangedParams,
EnvironmentDetailsOptions,
EnvironmentDetails,
DeprecatedProposedAPI,
} from './deprecatedProposedApiTypes';
import { IInterpreterService } from './interpreter/contracts';
import { IServiceContainer } from './ioc/types';
import { traceVerbose } from './logging';
import { PythonEnvInfo } from './pythonEnvironments/base/info';
import { getEnvPath } from './pythonEnvironments/base/info/env';
import { GetRefreshEnvironmentsOptions, IDiscoveryAPI } from './pythonEnvironments/base/locator';
import { sendTelemetryEvent } from './telemetry';
import { EventName } from './telemetry/constants';
const onDidInterpretersChangedEvent = new EventEmitter<EnvironmentsChangedParams[]>();
/**
* @deprecated Will be removed soon.
*/
export function reportInterpretersChanged(e: EnvironmentsChangedParams[]): void {
onDidInterpretersChangedEvent.fire(e);
}
const onDidActiveInterpreterChangedEvent = new EventEmitter<ActiveEnvironmentChangedParams>();
/**
* @deprecated Will be removed soon.
*/
export function reportActiveInterpreterChangedDeprecated(e: ActiveEnvironmentChangedParams): void {
onDidActiveInterpreterChangedEvent.fire(e);
}
function getVersionString(env: PythonEnvInfo): string[] {
const ver = [`${env.version.major}`, `${env.version.minor}`, `${env.version.micro}`];
if (env.version.release) {
ver.push(`${env.version.release}`);
if (env.version.sysVersion) {
ver.push(`${env.version.release}`);
}
}
return ver;
}
/**
* Returns whether the path provided matches the environment.
* @param path Path to environment folder or path to interpreter that uniquely identifies an environment.
* @param env Environment to match with.
*/
function isEnvSame(path: string, env: PythonEnvInfo) {
return arePathsSame(path, env.location) || arePathsSame(path, env.executable.filename);
}
export function buildDeprecatedProposedApi(
discoveryApi: IDiscoveryAPI,
serviceContainer: IServiceContainer,
): DeprecatedProposedAPI {
const interpreterPathService = serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const extensions = serviceContainer.get<IExtensions>(IExtensions);
const warningLogged = new Set<string>();
function sendApiTelemetry(apiName: string, warnLog = true) {
extensions
.determineExtensionFromCallStack()
.then((info) => {
sendTelemetryEvent(EventName.PYTHON_ENVIRONMENTS_API, undefined, {
apiName,
extensionId: info.extensionId,
});
traceVerbose(`Extension ${info.extensionId} accessed ${apiName}`);
if (warnLog && !warningLogged.has(info.extensionId)) {
console.warn(
`${info.extensionId} extension is using deprecated python APIs which will be removed soon.`,
);
warningLogged.add(info.extensionId);
}
})
.ignoreErrors();
}
const proposed: DeprecatedProposedAPI = {
environment: {
async getExecutionDetails(resource?: Resource) {
sendApiTelemetry('deprecated.getExecutionDetails');
const env = await interpreterService.getActiveInterpreter(resource);
return env ? { execCommand: [env.path] } : { execCommand: undefined };
},
async getActiveEnvironmentPath(resource?: Resource) {
sendApiTelemetry('deprecated.getActiveEnvironmentPath');
const env = await interpreterService.getActiveInterpreter(resource);
if (!env) {
return undefined;
}
return getEnvPath(env.path, env.envPath);
},
async getEnvironmentDetails(
path: string,
options?: EnvironmentDetailsOptions,
): Promise<EnvironmentDetails | undefined> {
sendApiTelemetry('deprecated.getEnvironmentDetails');
let env: PythonEnvInfo | undefined;
if (options?.useCache) {
env = discoveryApi.getEnvs().find((v) => isEnvSame(path, v));
}
if (!env) {
env = await discoveryApi.resolveEnv(path);
if (!env) {
return undefined;
}
}
return {
interpreterPath: env.executable.filename,
envFolderPath: env.location.length ? env.location : undefined,
version: getVersionString(env),
environmentType: [env.kind],
metadata: {
sysPrefix: env.executable.sysPrefix,
bitness: env.arch,
project: env.searchLocation,
},
};
},
getEnvironmentPaths() {
sendApiTelemetry('deprecated.getEnvironmentPaths');
const paths = discoveryApi.getEnvs().map((e) => getEnvPath(e.executable.filename, e.location));
return Promise.resolve(paths);
},
setActiveEnvironment(path: string, resource?: Resource): Promise<void> {
sendApiTelemetry('deprecated.setActiveEnvironment');
return interpreterPathService.update(resource, ConfigurationTarget.WorkspaceFolder, path);
},
async refreshEnvironment() {
sendApiTelemetry('deprecated.refreshEnvironment');
await discoveryApi.triggerRefresh();
const paths = discoveryApi.getEnvs().map((e) => getEnvPath(e.executable.filename, e.location));
return Promise.resolve(paths);
},
getRefreshPromise(options?: GetRefreshEnvironmentsOptions): Promise<void> | undefined {
sendApiTelemetry('deprecated.getRefreshPromise');
return discoveryApi.getRefreshPromise(options);
},
get onDidChangeExecutionDetails() {
sendApiTelemetry('deprecated.onDidChangeExecutionDetails', false);
return interpreterService.onDidChangeInterpreterConfiguration;
},
get onDidEnvironmentsChanged() {
sendApiTelemetry('deprecated.onDidEnvironmentsChanged', false);
return onDidInterpretersChangedEvent.event;
},
get onDidActiveEnvironmentChanged() {
sendApiTelemetry('deprecated.onDidActiveEnvironmentChanged', false);
return onDidActiveInterpreterChangedEvent.event;
},
get onRefreshProgress() {
sendApiTelemetry('deprecated.onRefreshProgress', false);
return discoveryApi.onProgress;
},
},
};
return proposed;
}