forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathinstallCheckUtils.ts
80 lines (74 loc) · 2.93 KB
/
installCheckUtils.ts
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
import { Diagnostic, DiagnosticSeverity, l10n, Range, TextDocument, Uri } from 'vscode';
import { installedCheckScript } from '../../../common/process/internal/scripts';
import { plainExec } from '../../../common/process/rawProcessApis';
import { IInterpreterPathService } from '../../../common/types';
import { traceInfo, traceVerbose, traceError } from '../../../logging';
import { getConfiguration } from '../../../common/vscodeApis/workspaceApis';
interface PackageDiagnostic {
package: string;
line: number;
character: number;
endLine: number;
endCharacter: number;
code: string;
severity: DiagnosticSeverity;
}
export const INSTALL_CHECKER_SOURCE = 'Python-InstalledPackagesChecker';
function parseDiagnostics(data: string): Diagnostic[] {
let diagnostics: Diagnostic[] = [];
try {
const raw = JSON.parse(data) as PackageDiagnostic[];
diagnostics = raw.map((item) => {
const d = new Diagnostic(
new Range(item.line, item.character, item.endLine, item.endCharacter),
l10n.t(`Package \`${item.package}\` is not installed in the selected environment.`),
item.severity,
);
d.code = { value: item.code, target: Uri.parse(`https://pypi.org/p/${item.package}`) };
d.source = INSTALL_CHECKER_SOURCE;
return d;
});
} catch {
diagnostics = [];
}
return diagnostics;
}
function getMissingPackageSeverity(doc: TextDocument): number {
const config = getConfiguration('python', doc.uri);
const severity: string = config.get<string>('missingPackage.severity', 'Hint');
if (severity === 'Error') {
return DiagnosticSeverity.Error;
}
if (severity === 'Warning') {
return DiagnosticSeverity.Warning;
}
if (severity === 'Information') {
return DiagnosticSeverity.Information;
}
return DiagnosticSeverity.Hint;
}
export async function getInstalledPackagesDiagnostics(
interpreterPathService: IInterpreterPathService,
doc: TextDocument,
): Promise<Diagnostic[]> {
const interpreter = interpreterPathService.get(doc.uri);
const scriptPath = installedCheckScript();
try {
traceInfo('Running installed packages checker: ', interpreter, scriptPath, doc.uri.fsPath);
const result = await plainExec(interpreter, [scriptPath, doc.uri.fsPath], {
env: {
VSCODE_MISSING_PGK_SEVERITY: `${getMissingPackageSeverity(doc)}`,
},
});
traceVerbose('Installed packages check result:\n', result.stdout);
if (result.stderr) {
traceError('Installed packages check error:\n', result.stderr);
}
return parseDiagnostics(result.stdout);
} catch (ex) {
traceError('Error while getting installed packages check result:\n', ex);
}
return [];
}