forked from mrsone40/.github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.ts
More file actions
80 lines (68 loc) · 2.95 KB
/
python.ts
File metadata and controls
80 lines (68 loc) · 2.95 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/naming-convention */
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode';
import { traceError, traceLog } from './log/logging';
import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension';
export interface IInterpreterDetails {
path?: string[];
resource?: Uri;
}
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
let _api: PythonExtension | undefined;
async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> {
if (_api) {
return _api;
}
_api = await PythonExtension.api();
return _api;
}
export async function initializePython(disposables: Disposable[]): Promise<void> {
try {
const api = await getPythonExtensionAPI();
if (api) {
disposables.push(
api.environments.onDidChangeActiveEnvironmentPath((e) => {
onDidChangePythonInterpreterEvent.fire({ path: [e.path], resource: e.resource?.uri });
}),
);
traceLog('Waiting for interpreter from python extension.');
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
}
} catch (error) {
traceError('Error initializing python: ', error);
}
}
export async function resolveInterpreter(interpreter: string[]): Promise<ResolvedEnvironment | undefined> {
const api = await getPythonExtensionAPI();
return api?.environments.resolveEnvironment(interpreter[0]);
}
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
const api = await getPythonExtensionAPI();
const environment = await api?.environments.resolveEnvironment(
api?.environments.getActiveEnvironmentPath(resource),
);
if (environment?.executable.uri && checkVersion(environment)) {
return { path: [environment?.executable.uri.fsPath], resource };
}
return { path: undefined, resource };
}
export async function getDebuggerPath(): Promise<string | undefined> {
const api = await getPythonExtensionAPI();
return api?.debug.getDebuggerPackagePath();
}
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
await getPythonExtensionAPI();
return await commands.executeCommand(command, ...rest);
}
export function checkVersion(resolved: ResolvedEnvironment | undefined): boolean {
const version = resolved?.version;
if (version?.major === 3 && version?.minor >= 8) {
return true;
}
traceError(`Python version ${version?.major}.${version?.minor} is not supported.`);
traceError(`Selected python path: ${resolved?.executable.uri?.fsPath}`);
traceError('Supported versions are 3.8 and above.');
return false;
}