-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathpython.ts
More file actions
150 lines (131 loc) · 5.2 KB
/
python.ts
File metadata and controls
150 lines (131 loc) · 5.2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/naming-convention */
import {
ActiveEnvironmentPathChangeEvent,
Environment,
EnvironmentPath,
PythonExtension,
Resource,
} from '@vscode/python-extension';
import { commands, EventEmitter, extensions, Uri, Event, Disposable } from 'vscode';
import { createDeferred } from './utils/async';
import { traceError, traceLog } from './log/logging';
interface IExtensionApi {
ready: Promise<void>;
settings: {
getExecutionDetails(resource?: Resource): { execCommand: string[] | undefined };
};
}
export interface IInterpreterDetails {
path?: string[];
resource?: Uri;
}
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
async function activateExtension() {
console.log('Activating Python extension...');
activateEnvsExtension();
const extension = extensions.getExtension('ms-python.python');
if (extension) {
if (!extension.isActive) {
await extension.activate();
}
}
console.log('Python extension activated.');
return extension;
}
async function activateEnvsExtension() {
const extension = extensions.getExtension('ms-python.vscode-python-envs');
if (extension) {
if (!extension.isActive) {
await extension.activate();
}
}
return extension;
}
async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
const extension = await activateExtension();
return extension?.exports as IExtensionApi;
}
async function getPythonExtensionEnviromentAPI(): Promise<PythonExtension> {
// Load the Python extension API
await activateExtension();
return await PythonExtension.api();
}
export async function initializePython(disposables: Disposable[]): Promise<void> {
try {
const api = await getPythonExtensionEnviromentAPI();
if (api) {
disposables.push(
api.environments.onDidChangeActiveEnvironmentPath((e: ActiveEnvironmentPathChangeEvent) => {
let resourceUri: Uri | undefined;
if (e.resource instanceof Uri) {
resourceUri = e.resource;
}
if (e.resource && 'uri' in e.resource) {
// WorkspaceFolder type
resourceUri = e.resource.uri;
}
onDidChangePythonInterpreterEvent.fire({ path: [e.path], resource: resourceUri });
}),
);
traceLog('Waiting for interpreter from python extension.');
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
}
} catch (error) {
traceError('Error initializing python: ', error);
}
}
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
await activateExtension();
return await commands.executeCommand(command, ...rest);
}
export async function getSettingsPythonPath(resource?: Uri): Promise<string[] | undefined> {
const api = await getPythonExtensionAPI();
return api?.settings.getExecutionDetails(resource).execCommand;
}
export async function getEnvironmentVariables(resource?: Resource) {
const api = await getPythonExtensionEnviromentAPI();
return api.environments.getEnvironmentVariables(resource);
}
export async function resolveEnvironment(env: Environment | EnvironmentPath | string) {
const api = await getPythonExtensionEnviromentAPI();
return api.environments.resolveEnvironment(env);
}
export async function getActiveEnvironmentPath(resource?: Resource) {
const api = await getPythonExtensionEnviromentAPI();
return api.environments.getActiveEnvironmentPath(resource);
}
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
const api = await getPythonExtensionEnviromentAPI();
const environment = await api.environments.resolveEnvironment(api.environments.getActiveEnvironmentPath(resource));
const rawExecPath = environment?.executable.uri?.fsPath;
if (rawExecPath) {
let execPath = rawExecPath;
if (rawExecPath.includes(' ') && !(rawExecPath.startsWith('"') && rawExecPath.endsWith('"'))) {
execPath = `"${rawExecPath}"`;
}
return { path: [execPath], resource };
}
return { path: undefined, resource };
}
export async function hasInterpreters() {
const api = await getPythonExtensionEnviromentAPI();
const onAddedToCollection = createDeferred();
api.environments.onDidChangeEnvironments(async () => {
if (api.environments.known) {
onAddedToCollection.resolve();
}
});
const initialEnvs = api.environments.known;
if (initialEnvs.length > 0) {
return true;
}
await Promise.race([onAddedToCollection.promise, api?.environments.refreshEnvironments()]);
return api.environments.known.length > 0;
}
export async function getInterpreters() {
const api = await getPythonExtensionEnviromentAPI();
return api.environments.known || [];
}