forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipenvUtils.ts
More file actions
251 lines (216 loc) · 8.07 KB
/
pipenvUtils.ts
File metadata and controls
251 lines (216 loc) · 8.07 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
// Utility functions for Pipenv environment management
import * as path from 'path';
import { Uri } from 'vscode';
import which from 'which';
import {
EnvironmentManager,
PythonCommandRunConfiguration,
PythonEnvironment,
PythonEnvironmentApi,
PythonEnvironmentInfo,
} from '../../api';
import { ENVS_EXTENSION_ID } from '../../common/constants';
import { traceError, traceInfo } from '../../common/logging';
import { getWorkspacePersistentState } from '../../common/persistentState';
import { getSettingWorkspaceScope } from '../../features/settings/settingHelpers';
import {
isNativeEnvInfo,
NativeEnvInfo,
NativeEnvManagerInfo,
NativePythonEnvironmentKind,
NativePythonFinder,
} from '../common/nativePythonFinder';
import { getShellActivationCommands, shortVersion } from '../common/utils';
export const PIPENV_PATH_KEY = `${ENVS_EXTENSION_ID}:pipenv:PIPENV_PATH`;
export const PIPENV_WORKSPACE_KEY = `${ENVS_EXTENSION_ID}:pipenv:WORKSPACE_SELECTED`;
export const PIPENV_GLOBAL_KEY = `${ENVS_EXTENSION_ID}:pipenv:GLOBAL_SELECTED`;
let pipenvPath: string | undefined;
async function findPipenv(): Promise<string | undefined> {
try {
return await which('pipenv');
} catch {
return undefined;
}
}
async function setPipenv(pipenv: string): Promise<void> {
pipenvPath = pipenv;
const state = await getWorkspacePersistentState();
await state.set(PIPENV_PATH_KEY, pipenv);
}
export async function clearPipenvCache(): Promise<void> {
pipenvPath = undefined;
}
function getPipenvPathFromSettings(): Uri[] {
const pipenvPath = getSettingWorkspaceScope<string>('python', 'pipenvPath');
return pipenvPath ? [Uri.file(pipenvPath)] : [];
}
export async function getPipenv(native?: NativePythonFinder): Promise<string | undefined> {
if (pipenvPath) {
return pipenvPath;
}
const state = await getWorkspacePersistentState();
pipenvPath = await state.get<string>(PIPENV_PATH_KEY);
if (pipenvPath) {
traceInfo(`Using pipenv from persistent state: ${pipenvPath}`);
return pipenvPath;
}
// Try to find pipenv in PATH
const foundPipenv = await findPipenv();
if (foundPipenv) {
pipenvPath = foundPipenv;
traceInfo(`Found pipenv in PATH: ${foundPipenv}`);
return foundPipenv;
}
// Use native finder as fallback
if (native) {
const data = await native.refresh(false);
const managers = data
.filter((e) => !isNativeEnvInfo(e))
.map((e) => e as NativeEnvManagerInfo)
.filter((e) => e.tool.toLowerCase() === 'pipenv');
if (managers.length > 0) {
pipenvPath = managers[0].executable;
traceInfo(`Using pipenv from native finder: ${pipenvPath}`);
await state.set(PIPENV_PATH_KEY, pipenvPath);
return pipenvPath;
}
}
traceInfo('Pipenv not found');
return undefined;
}
async function nativeToPythonEnv(
info: NativeEnvInfo,
api: PythonEnvironmentApi,
manager: EnvironmentManager,
): Promise<PythonEnvironment | undefined> {
if (!(info.prefix && info.executable && info.version)) {
traceError(`Incomplete pipenv environment info: ${JSON.stringify(info)}`);
return undefined;
}
const sv = shortVersion(info.version);
const folderName = path.basename(info.prefix);
const name = info.name || info.displayName || folderName;
const displayName = info.displayName || `${folderName} (${sv})`;
// Derive the environment's bin/scripts directory from the python executable
const binDir = path.dirname(info.executable);
let shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
let shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
try {
const maps = await getShellActivationCommands(binDir);
shellActivation = maps.shellActivation;
shellDeactivation = maps.shellDeactivation;
} catch (ex) {
traceError(`Failed to compute shell activation commands for pipenv at ${binDir}: ${ex}`);
}
const environment: PythonEnvironmentInfo = {
name: name,
displayName: displayName,
shortDisplayName: displayName,
displayPath: info.prefix,
version: info.version,
environmentPath: Uri.file(info.prefix),
description: undefined,
tooltip: info.prefix,
execInfo: {
run: { executable: info.executable },
shellActivation,
shellDeactivation,
},
sysPrefix: info.prefix,
};
return api.createPythonEnvironmentItem(environment, manager);
}
export async function refreshPipenv(
hardRefresh: boolean,
nativeFinder: NativePythonFinder,
api: PythonEnvironmentApi,
manager: EnvironmentManager,
): Promise<PythonEnvironment[]> {
traceInfo('Refreshing pipenv environments');
const searchUris = getPipenvPathFromSettings();
const data = await nativeFinder.refresh(hardRefresh, searchUris.length > 0 ? searchUris : undefined);
let pipenv = await getPipenv();
if (pipenv === undefined) {
const managers = data
.filter((e) => !isNativeEnvInfo(e))
.map((e) => e as NativeEnvManagerInfo)
.filter((e) => e.tool.toLowerCase() === 'pipenv');
if (managers.length > 0) {
pipenv = managers[0].executable;
await setPipenv(pipenv);
}
}
const envs = data
.filter((e) => isNativeEnvInfo(e))
.map((e) => e as NativeEnvInfo)
.filter((e) => e.kind === NativePythonEnvironmentKind.pipenv);
const collection: PythonEnvironment[] = [];
for (const e of envs) {
if (pipenv) {
const environment = await nativeToPythonEnv(e, api, manager);
if (environment) {
collection.push(environment);
}
}
}
traceInfo(`Found ${collection.length} pipenv environments`);
return collection;
}
export async function resolvePipenvPath(
fsPath: string,
nativeFinder: NativePythonFinder,
api: PythonEnvironmentApi,
manager: EnvironmentManager,
): Promise<PythonEnvironment | undefined> {
const resolved = await nativeFinder.resolve(fsPath);
if (resolved.kind === NativePythonEnvironmentKind.pipenv) {
const pipenv = await getPipenv(nativeFinder);
if (pipenv) {
return await nativeToPythonEnv(resolved, api, manager);
}
}
return undefined;
}
// Persistence functions for workspace/global environment selection
export async function getPipenvForGlobal(): Promise<string | undefined> {
const state = await getWorkspacePersistentState();
return await state.get(PIPENV_GLOBAL_KEY);
}
export async function setPipenvForGlobal(pipenvPath: string | undefined): Promise<void> {
const state = await getWorkspacePersistentState();
await state.set(PIPENV_GLOBAL_KEY, pipenvPath);
}
export async function getPipenvForWorkspace(fsPath: string): Promise<string | undefined> {
const state = await getWorkspacePersistentState();
const data: { [key: string]: string } | undefined = await state.get(PIPENV_WORKSPACE_KEY);
if (data) {
try {
return data[fsPath];
} catch {
return undefined;
}
}
return undefined;
}
export async function setPipenvForWorkspace(fsPath: string, envPath: string | undefined): Promise<void> {
const state = await getWorkspacePersistentState();
const data: { [key: string]: string } = (await state.get(PIPENV_WORKSPACE_KEY)) ?? {};
if (envPath) {
data[fsPath] = envPath;
} else {
delete data[fsPath];
}
await state.set(PIPENV_WORKSPACE_KEY, data);
}
export async function setPipenvForWorkspaces(fsPath: string[], envPath: string | undefined): Promise<void> {
const state = await getWorkspacePersistentState();
const data: { [key: string]: string } = (await state.get(PIPENV_WORKSPACE_KEY)) ?? {};
fsPath.forEach((s) => {
if (envPath) {
data[s] = envPath;
} else {
delete data[s];
}
});
await state.set(PIPENV_WORKSPACE_KEY, data);
}