forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipUtils.ts
More file actions
195 lines (178 loc) · 6.81 KB
/
pipUtils.ts
File metadata and controls
195 lines (178 loc) · 6.81 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
import * as fse from 'fs-extra';
import * as path from 'path';
import * as tomljs from '@iarna/toml';
import { LogOutputChannel, ProgressLocation, QuickInputButtons, Uri } from 'vscode';
import { showQuickPickWithButtons, withProgress } from '../../common/window.apis';
import { PackageManagement, Pickers, VenvManagerStrings } from '../../common/localize';
import { PackageInstallOptions, PythonEnvironmentApi, PythonProject } from '../../api';
import { findFiles } from '../../common/workspace.apis';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { Installable, selectFromCommonPackagesToInstall, selectFromInstallableToInstall } from '../common/pickers';
import { traceInfo } from '../../common/logging';
async function tomlParse(fsPath: string, log?: LogOutputChannel): Promise<tomljs.JsonMap> {
try {
const content = await fse.readFile(fsPath, 'utf-8');
return tomljs.parse(content);
} catch (err) {
log?.error('Failed to parse `pyproject.toml`:', err);
}
return {};
}
function isPipInstallableToml(toml: tomljs.JsonMap): boolean {
return toml['build-system'] !== undefined && toml.project !== undefined;
}
function getTomlInstallable(toml: tomljs.JsonMap, tomlPath: Uri): Installable[] {
const extras: Installable[] = [];
if (isPipInstallableToml(toml)) {
const name = path.basename(tomlPath.fsPath);
extras.push({
name,
displayName: name,
description: VenvManagerStrings.installEditable,
group: 'TOML',
args: ['-e', path.dirname(tomlPath.fsPath)],
uri: tomlPath,
});
}
if (toml.project && (toml.project as tomljs.JsonMap)['optional-dependencies']) {
const deps = (toml.project as tomljs.JsonMap)['optional-dependencies'];
for (const key of Object.keys(deps)) {
extras.push({
name: key,
displayName: key,
group: 'TOML',
args: ['-e', `.[${key}]`],
uri: tomlPath,
});
}
}
return extras;
}
async function getCommonPackages(): Promise<Installable[]> {
try {
const pipData = path.join(EXTENSION_ROOT_DIR, 'files', 'common_pip_packages.json');
const data = await fse.readFile(pipData, { encoding: 'utf-8' });
const packages = JSON.parse(data) as { name: string; uri: string }[];
return packages.map((p) => {
return {
name: p.name,
displayName: p.name,
uri: Uri.parse(p.uri),
};
});
} catch {
return [];
}
}
async function selectWorkspaceOrCommon(
installable: Installable[],
common: Installable[],
showSkipOption: boolean,
): Promise<string[] | undefined> {
if (installable.length === 0 && common.length === 0) {
return undefined;
}
const items = [];
if (installable.length > 0) {
items.push({
label: PackageManagement.workspaceDependencies,
description: PackageManagement.workspaceDependenciesDescription,
});
}
if (common.length > 0) {
items.push({
label: PackageManagement.commonPackages,
description: PackageManagement.commonPackagesDescription,
});
}
if (showSkipOption && items.length > 0) {
items.push({ label: PackageManagement.skipPackageInstallation });
}
const selected =
items.length === 1
? items[0]
: await showQuickPickWithButtons(items, {
placeHolder: Pickers.Packages.selectOption,
ignoreFocusOut: true,
showBackButton: true,
matchOnDescription: false,
matchOnDetail: false,
});
if (selected && !Array.isArray(selected)) {
try {
if (selected.label === PackageManagement.workspaceDependencies) {
return await selectFromInstallableToInstall(installable);
} else if (selected.label === PackageManagement.commonPackages) {
return await selectFromCommonPackagesToInstall(common);
} else {
traceInfo('Package Installer: user selected skip package installation');
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (ex: any) {
if (ex === QuickInputButtons.Back) {
return selectWorkspaceOrCommon(installable, common, showSkipOption);
}
}
}
return undefined;
}
export async function getWorkspacePackagesToInstall(
api: PythonEnvironmentApi,
options?: PackageInstallOptions,
project?: PythonProject[],
): Promise<string[] | undefined> {
const installable = (await getProjectInstallable(api, project)) ?? [];
const common = await getCommonPackages();
return selectWorkspaceOrCommon(installable, common, !!options?.showSkipOption);
}
export async function getProjectInstallable(
api: PythonEnvironmentApi,
projects?: PythonProject[],
): Promise<Installable[]> {
if (!projects) {
return [];
}
const exclude = '**/{.venv*,.git,.nox,.tox,.conda,site-packages,__pypackages__}/**';
const installable: Installable[] = [];
await withProgress(
{
location: ProgressLocation.Window,
title: VenvManagerStrings.searchingDependencies,
},
async (_progress, token) => {
const results: Uri[] = (
await Promise.all([
findFiles('**/*requirements*.txt', exclude, undefined, token),
findFiles('**/requirements/*.txt', exclude, undefined, token),
findFiles('**/pyproject.toml', exclude, undefined, token),
])
).flat();
const fsPaths = projects.map((p) => p.uri.fsPath);
const filtered = results
.filter((uri) => {
const p = api.getPythonProject(uri)?.uri.fsPath;
return p && fsPaths.includes(p);
})
.sort();
await Promise.all(
filtered.map(async (uri) => {
if (uri.fsPath.endsWith('.toml')) {
const toml = await tomlParse(uri.fsPath);
installable.push(...getTomlInstallable(toml, uri));
} else {
const name = path.basename(uri.fsPath);
installable.push({
name,
uri,
displayName: name,
group: 'Requirements',
args: ['-r', uri.fsPath],
});
}
}),
);
},
);
return installable;
}