-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathpython.ts
More file actions
155 lines (137 loc) · 4.67 KB
/
python.ts
File metadata and controls
155 lines (137 loc) · 4.67 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode'
import { traceError, traceLog } from './log'
import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension'
import path from 'path'
import { err, ok, Result } from '@bus/result'
import * as vscode from 'vscode'
export interface IInterpreterDetails {
path?: string[]
resource?: Uri
isVirtualEnvironment?: boolean
binPath?: string
}
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(async e => {
const environment = await api.environments.resolveEnvironment(e.path)
const isVirtualEnv = environment?.environment !== undefined
// Get the directory of the Python executable for virtual environments
const pythonDir = environment?.executable.uri
? path.dirname(environment.executable.uri.fsPath)
: undefined
onDidChangePythonInterpreterEvent.fire({
path: [e.path],
resource: e.resource?.uri,
isVirtualEnvironment: isVirtualEnv,
binPath: isVirtualEnv ? pythonDir : undefined,
})
}),
)
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)) {
const isVirtualEnv = environment.environment !== undefined
// Get the directory of the Python executable
const pythonDir = path.dirname(environment?.executable.uri.fsPath)
return {
path: [environment?.executable.uri.fsPath],
resource,
isVirtualEnvironment: isVirtualEnv,
// For virtual environments, we need to point directly to the bin directory
// rather than constructing it from the environment folder
binPath: isVirtualEnv ? pythonDir : undefined,
}
}
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
}
/**
* getPythonEnvVariables returns the environment variables for the current python interpreter.
*
* @returns The environment variables for the current python interpreter.
*/
export async function getPythonEnvVariables(): Promise<
Result<Record<string, string>, string>
> {
const api = await getPythonExtensionAPI()
if (!api) {
return err('Python extension API not found')
}
const workspaces = vscode.workspace.workspaceFolders
if (!workspaces) {
return ok({})
}
const out: Record<string, string> = {}
for (const workspace of workspaces) {
const envVariables = api.environments.getEnvironmentVariables(workspace.uri)
if (envVariables) {
for (const [key, value] of Object.entries(envVariables)) {
if (value) {
out[key] = value
}
}
}
}
return ok(out)
}