-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpipUtils.ts
More file actions
239 lines (219 loc) · 8.66 KB
/
pipUtils.ts
File metadata and controls
239 lines (219 loc) · 8.66 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
import * as tomljs from '@iarna/toml';
import * as fse from 'fs-extra';
import * as path from 'path';
import { LogOutputChannel, ProgressLocation, QuickInputButtons, QuickPickItem, Uri } from 'vscode';
import { PackageManagementOptions, PythonEnvironment, PythonEnvironmentApi, PythonProject } from '../../api';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { PackageManagement, Pickers, VenvManagerStrings } from '../../common/localize';
import { traceInfo } from '../../common/logging';
import { showQuickPickWithButtons, withProgress } from '../../common/window.apis';
import { findFiles } from '../../common/workspace.apis';
import { selectFromCommonPackagesToInstall, selectFromInstallableToInstall } from '../common/pickers';
import { Installable } from '../common/types';
import { mergePackages } from '../common/utils';
import { refreshPipPackages } from './utils';
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[] = [];
const projectDir = path.dirname(tomlPath.fsPath);
if (isPipInstallableToml(toml)) {
const name = path.basename(tomlPath.fsPath);
extras.push({
name,
displayName: name,
description: VenvManagerStrings.installEditable,
group: 'TOML',
args: ['-e', projectDir],
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',
// Use a single -e argument with the extras specified as part of the path
args: ['-e', `${projectDir}[${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,
installed: string[],
): Promise<PipPackages | undefined> {
if (installable.length === 0 && common.length === 0) {
return undefined;
}
const items: QuickPickItem[] = [];
if (installable.length > 0) {
items.push({
label: PackageManagement.workspaceDependencies,
description: PackageManagement.workspaceDependenciesDescription,
});
}
if (common.length > 0) {
items.push({
label: PackageManagement.searchCommonPackages,
description: PackageManagement.searchCommonPackagesDescription,
});
}
if (showSkipOption && items.length > 0) {
items.push({ label: PackageManagement.skipPackageInstallation });
}
let showBackButton = true;
let selected: QuickPickItem[] | QuickPickItem | undefined = undefined;
if (items.length === 1) {
selected = items[0];
showBackButton = false;
} else {
selected = 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, undefined, { showBackButton });
} else if (selected.label === PackageManagement.searchCommonPackages) {
return await selectFromCommonPackagesToInstall(common, installed, undefined, { showBackButton });
} else if (selected.label === PackageManagement.skipPackageInstallation) {
traceInfo('Package Installer: user selected skip package installation');
return undefined;
} else {
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (ex: any) {
if (ex === QuickInputButtons.Back) {
return selectWorkspaceOrCommon(installable, common, showSkipOption, installed);
}
}
}
return undefined;
}
export interface PipPackages {
install: string[];
uninstall: string[];
}
export async function getWorkspacePackagesToInstall(
api: PythonEnvironmentApi,
options: PackageManagementOptions,
project?: PythonProject[],
environment?: PythonEnvironment,
log?: LogOutputChannel,
): Promise<PipPackages | undefined> {
const installable = (await getProjectInstallable(api, project)) ?? [];
let common = await getCommonPackages();
let installed: string[] | undefined;
if (environment) {
installed = (await refreshPipPackages(environment, log, { showProgress: true }))?.map((pkg) => pkg.name);
common = mergePackages(common, installed ?? []);
}
return selectWorkspaceOrCommon(installable, common, !!options.showSkipOption, installed ?? []);
}
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.Notification,
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('**/requirements/*.txt', exclude, undefined, token),
findFiles('**/pyproject.toml', exclude, undefined, token),
])
).flat();
// Deduplicate by fsPath
const uniqueResults = Array.from(
new Map(results.map((uri) => [uri.fsPath, uri])).values(),
);
const fsPaths = projects.map((p) => p.uri.fsPath);
const filtered = uniqueResults
.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;
}
export function isPipInstallCommand(command: string): boolean {
// Regex to match pip install commands, capturing variations like:
// pip install package
// python -m pip install package
// pip3 install package
// py -m pip install package
// pip install -r requirements.txt
// uv pip install package
// poetry run pip install package
// pipx run pip install package
// Any other tool that might wrap pip install
return /(?:^|\s)(?:\S+\s+)*(?:pip\d*)\s+(install|uninstall)\b/.test(command);
}