forked from microsoft/vscode-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.ts
More file actions
267 lines (241 loc) · 11.1 KB
/
python.ts
File metadata and controls
267 lines (241 loc) · 11.1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/naming-convention */
import { Environment, EnvironmentPath, ResolvedEnvironment, Resource } from '@vscode/python-extension';
import { commands, EventEmitter, extensions, Uri, Event, Disposable, Extension } from 'vscode';
import { traceError, traceLog } from './log/logging';
import { PythonEnvironment, PythonEnvironmentApi, PythonEnvsExtension } from '../envExtApi';
import {
legacyGetActiveEnvironmentPath,
legacyGetEnvironmentVariables,
legacyGetInterpreterDetails,
legacyGetSettingsPythonPath,
legacyInitializePython,
legacyResolveEnvironment,
} from './legacyPython';
import { useEnvExtension } from './utilities';
import { EnvironmentVariables } from './variables/types';
export interface IInterpreterDetails {
path?: string[];
resource?: Uri;
}
/** Event emitter for Python interpreter changes */
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
async function activateExtensions() {
traceLog('Value during activateExtensions of useEnvExtension(): ', useEnvExtension());
await activatePythonExtension();
await activateEnvsExtension();
}
async function activatePythonExtension() {
const extension = extensions.getExtension('ms-python.python');
if (extension) {
if (!extension.isActive) {
console.log('Activating Python extension...');
await extension.activate();
}
}
return extension;
}
/**
* Activates the Python environments extension.
* @returns The activated Python environments extension instance
*/
async function activateEnvsExtension(): Promise<Extension<any> | undefined> {
const extension = extensions.getExtension('ms-python.vscode-python-envs');
if (extension) {
if (!extension.isActive) {
console.log('Activating Python Environments extension...');
await extension.activate();
}
}
return extension;
}
export async function getPythonEnvironmentExtensionAPI(): Promise<PythonEnvironmentApi> {
await activateEnvsExtension();
return await PythonEnvsExtension.api();
}
export async function initializePython(disposables: Disposable[]): Promise<void> {
traceLog(`initializePython: usingEnvExt='${useEnvExtension()}'`);
if (!useEnvExtension()) {
await legacyInitializePython(disposables, onDidChangePythonInterpreterEvent);
} else {
try {
const api = await getPythonEnvironmentExtensionAPI();
if (api) {
disposables.push(
api.onDidChangeEnvironments(async () => {
const details = await getInterpreterDetails();
traceLog(`initializePython:onDidChangeEnvironments fired executable='${details.path?.[0]}'`);
onDidChangePythonInterpreterEvent.fire(details);
traceLog('Python environments changed event processed.');
}),
);
traceLog('Waiting for interpreter from python environments extension.');
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
traceLog('initializePython: Initial interpreter details fired (env extension path)');
}
} catch (error) {
traceError('Error initializing python: ', error);
}
}
}
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
await activateExtensions();
traceLog(`runPythonExtensionCommand: executing command='${command}' argsCount='${rest.length}'`);
return await commands.executeCommand(command, ...rest);
}
/**
* Returns all the details needed to execute code within the selected environment,
* corresponding to the specified resource taking into account any workspace-specific settings
* for the workspace to which this resource belongs.
* @param resource Optional workspace resource to get settings for
* @returns Array of command components or undefined if not available
*/
export async function getSettingsPythonPath(resource?: Uri): Promise<string[] | undefined> {
// this one is only called if getInterpreterDetails(workspaceFolder) doesn't return somethinig with r.path
if (!useEnvExtension()) {
return legacyGetSettingsPythonPath(resource);
} else {
const api = await getPythonEnvironmentExtensionAPI();
let pyEnv = await api.getEnvironment(resource);
if (!pyEnv) {
traceLog(`getSettingsPythonPath: No environment for resource='${resource?.fsPath}'`);
return undefined;
}
// Resolve environment if execution info is not available
if (!pyEnv.execInfo) {
pyEnv = await api.resolveEnvironment(pyEnv.environmentPath);
traceLog(
`getSettingsPythonPath: Resolved environment execInfo for '${
pyEnv?.environmentPath.fsPath || 'undefined'
}'`,
);
}
// Extract execution command from resolved environment
const execInfo = pyEnv?.execInfo;
if (!execInfo) {
traceLog('getSettingsPythonPath: Missing execInfo after resolution');
return undefined;
}
const runConfig = execInfo.run;
traceLog(
`getSettingsPythonPath: Using executable='${runConfig.executable}' args='${
runConfig.args?.join(' ') || ''
}'`,
);
return runConfig.args ? [runConfig.executable, ...runConfig.args] : [runConfig.executable];
}
} // should I make this more async? rn it just becomes sync
export async function getEnvironmentVariables(resource?: Resource): Promise<EnvironmentVariables> {
if (!useEnvExtension()) {
return legacyGetEnvironmentVariables(resource);
} else {
const api = await getPythonEnvironmentExtensionAPI();
// Convert resource to Uri or undefined
const resourceUri =
resource instanceof Uri ? resource : resource && 'uri' in resource ? resource.uri : undefined;
return api.getEnvironmentVariables(resourceUri);
}
}
export async function resolveEnvironment(
env: Environment | EnvironmentPath | string,
): Promise<PythonEnvironment | undefined> {
if (!useEnvExtension()) {
const legacyResolvedEnv: ResolvedEnvironment | undefined = await legacyResolveEnvironment(env);
// if its a legacy path, convert to new python environment
const pythonVersion = legacyResolvedEnv?.version
? `${legacyResolvedEnv.version.major}.${legacyResolvedEnv.version.minor}.${legacyResolvedEnv.version.micro}`
: 'Unknown';
const execUri = legacyResolvedEnv?.executable.uri;
if (execUri === undefined) {
traceLog('resolveEnvironment: legacy path invalid (no executable uri)');
// Should return undefined for invalid environment
return undefined;
}
if (legacyResolvedEnv) {
traceLog(`resolveEnvironment: legacy resolved executable='${execUri.fsPath}' version='${pythonVersion}'`);
const pythonEnv: PythonEnvironment = {
envId: {
id: execUri.fsPath,
managerId: legacyResolvedEnv.environment?.type ?? 'Venv',
},
name: legacyResolvedEnv.environment?.name ?? `Python ${pythonVersion ?? 'Unknown'}`,
displayName: legacyResolvedEnv.environment?.name ?? `Python ${pythonVersion ?? 'Unknown'}`,
displayPath: execUri.fsPath,
version: pythonVersion,
environmentPath: execUri,
execInfo: {
run: {
executable: execUri.fsPath,
args: [],
},
},
sysPrefix: legacyResolvedEnv.executable.sysPrefix ?? '',
};
return pythonEnv;
}
} else {
const api = await getPythonEnvironmentExtensionAPI();
// Handle different input types for the new API
if (typeof env === 'string') {
// Convert string path to Uri for the new API
traceLog(`resolveEnvironment: new API resolving from string='${env}'`);
return api.resolveEnvironment(Uri.file(env));
} else if (typeof env === 'object' && 'path' in env) {
// EnvironmentPath has a uri property
traceLog(`resolveEnvironment: new API resolving from EnvironmentPath='${env.path}'`);
return api.resolveEnvironment(Uri.file(env.path));
} else {
traceLog('resolveEnvironment: new API unsupported env input');
return undefined;
}
}
}
export async function getActiveEnvironmentPath(
resource?: Resource,
): Promise<PythonEnvironment | EnvironmentPath | undefined> {
if (!useEnvExtension()) {
const envPath: EnvironmentPath = await legacyGetActiveEnvironmentPath(resource);
traceLog(`getActiveEnvironmentPath: legacy active path='${envPath.path}'`);
return envPath;
} else {
const api = await getPythonEnvironmentExtensionAPI();
// Convert resource to Uri | undefined from Resource | undefined
const resourceUri =
resource instanceof Uri ? resource : resource && 'uri' in resource ? resource.uri : undefined;
const env = await api.getEnvironment(resourceUri);
traceLog(
`getActiveEnvironmentPath: new API envPath='${env?.environmentPath.fsPath}' resource='${resourceUri?.fsPath}'`,
);
return env;
}
}
/**
* Gets Python interpreter details using the Python Envs extension API.
*
* This function retrieves the active Python environment for a given resource using the
* legacy @vscode/python-extension API. It resolves the environment to get the executable
* path and handles path quoting for paths containing spaces.
*
* @param resource Optional URI to specify the workspace/folder context for interpreter selection
* @returns Promise resolving to interpreter details containing the executable path and resource
*/
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
if (!useEnvExtension()) {
return legacyGetInterpreterDetails(resource);
} else {
const api = await getPythonEnvironmentExtensionAPI();
// A promise that resolves to the current Python environment, or undefined if none is set.
const env: PythonEnvironment | undefined = await api.getEnvironment(resource);
// resolve the environment to get full details
const resolvedEnv = env ? await api.resolveEnvironment(env?.environmentPath) : undefined;
const executablePath = resolvedEnv?.execInfo.run.executable;
const a: IInterpreterDetails = {
path: executablePath ? [executablePath] : undefined,
resource,
};
traceLog(`getInterpreterDetails: resource='${resource?.fsPath}' executable='${executablePath}'`);
return a;
}
}