forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect installed packages in the selected environment
- Loading branch information
1 parent
b0da28c
commit 4e6db89
Showing
10 changed files
with
243 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import argparse | ||
import json | ||
import os | ||
import pathlib | ||
import sys | ||
from typing import Optional, Sequence | ||
|
||
LIB_ROOT = pathlib.Path(__file__).parent / "lib" / "python" | ||
sys.path.insert(0, os.fspath(LIB_ROOT)) | ||
|
||
from importlib_metadata import metadata | ||
from packaging.requirements import Requirement | ||
|
||
|
||
def parse_args(argv: Optional[Sequence[str]] = None): | ||
if argv is None: | ||
argv = sys.argv[1:] | ||
parser = argparse.ArgumentParser( | ||
description="Check for installed packages against requirements" | ||
) | ||
parser.add_argument("REQUIREMENTS", type=str, help="Path to requirements.[txt, in]", nargs="+") | ||
|
||
return parser.parse_args(argv) | ||
|
||
|
||
def parse_requirements(line: str) -> Optional[Requirement]: | ||
try: | ||
req = Requirement(line.strip("\\")) | ||
if req.marker is None: | ||
return req | ||
elif req.marker.evaluate(): | ||
return req | ||
except: | ||
return None | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
diagnostics = [] | ||
for req_file in args.REQUIREMENTS: | ||
req_file = pathlib.Path(req_file) | ||
if req_file.exists(): | ||
lines = req_file.read_text(encoding="utf-8").splitlines() | ||
for n, line in enumerate(lines): | ||
if line.startswith(("#", "-", " ")) or line == "": | ||
continue | ||
|
||
req = parse_requirements(line) | ||
if req: | ||
try: | ||
# Check if package is installed | ||
metadata(req.name) | ||
except: | ||
diagnostics.append( | ||
{ | ||
"line": n, | ||
"package": req.name, | ||
"code": "not-installed", | ||
"severity": 3, | ||
} | ||
) | ||
print(json.dumps(diagnostics, ensure_ascii=False)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License | ||
|
||
import { DiagnosticChangeEvent, DiagnosticCollection, Disposable, languages } from 'vscode'; | ||
|
||
export function createDiagnosticCollection(name: string): DiagnosticCollection { | ||
return languages.createDiagnosticCollection(name); | ||
} | ||
|
||
export function onDidChangeDiagnostics(handler: (e: DiagnosticChangeEvent) => void): Disposable { | ||
return languages.onDidChangeDiagnostics(handler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/client/pythonEnvironments/creation/installedPackagesDiagnostic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License | ||
|
||
import { Diagnostic, DiagnosticCollection, DiagnosticSeverity, l10n, Range, TextDocument, Uri } from 'vscode'; | ||
import { installedCheckScript } from '../../common/process/internal/scripts'; | ||
import { plainExec } from '../../common/process/rawProcessApis'; | ||
import { IDisposableRegistry, IInterpreterPathService } from '../../common/types'; | ||
import { executeCommand } from '../../common/vscodeApis/commandApis'; | ||
import { createDiagnosticCollection, onDidChangeDiagnostics } from '../../common/vscodeApis/languageApis'; | ||
import { getActiveTextEditor, onDidChangeActiveTextEditor } from '../../common/vscodeApis/windowApis'; | ||
import { | ||
getOpenTextDocuments, | ||
onDidCloseTextDocument, | ||
onDidOpenTextDocument, | ||
onDidSaveTextDocument, | ||
} from '../../common/vscodeApis/workspaceApis'; | ||
import { traceVerbose } from '../../logging'; | ||
|
||
interface PackageDiagnostic { | ||
package: string; | ||
line: number; | ||
code: string; | ||
severity: DiagnosticSeverity; | ||
} | ||
|
||
const SOURCE = 'Python-Ext'; | ||
const PIP_DEPS_NOT_INSTALLED_KEY = 'pipDepsNotInstalled'; | ||
|
||
async function getPipRequirementsDiagnostics( | ||
interpreterPathService: IInterpreterPathService, | ||
doc: TextDocument, | ||
): Promise<Diagnostic[]> { | ||
const interpreter = interpreterPathService.get(doc.uri); | ||
const result = await plainExec(interpreter, [installedCheckScript(), doc.uri.fsPath]); | ||
traceVerbose('Installed packages check result:\n', result.stdout); | ||
let diagnostics: Diagnostic[] = []; | ||
try { | ||
const raw = JSON.parse(result.stdout) as PackageDiagnostic[]; | ||
diagnostics = raw.map((item) => { | ||
const d = new Diagnostic( | ||
new Range(item.line, 0, item.line, item.package.length), | ||
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 = SOURCE; | ||
return d; | ||
}); | ||
} catch { | ||
diagnostics = []; | ||
} | ||
return diagnostics; | ||
} | ||
|
||
async function setContextForActiveEditor(diagnosticCollection: DiagnosticCollection): Promise<void> { | ||
const doc = getActiveTextEditor()?.document; | ||
if (doc && doc.languageId === 'pip-requirements') { | ||
const diagnostics = diagnosticCollection.get(doc.uri); | ||
if (diagnostics && diagnostics.length > 0) { | ||
traceVerbose(`Setting context for pip dependencies not installed: ${doc.uri.fsPath}`); | ||
await executeCommand('setContext', PIP_DEPS_NOT_INSTALLED_KEY, true); | ||
return; | ||
} | ||
} | ||
|
||
// undefined here in the logs means no file was selected | ||
traceVerbose(`Clearing context for pip dependencies not installed: ${doc?.uri.fsPath}`); | ||
await executeCommand('setContext', PIP_DEPS_NOT_INSTALLED_KEY, false); | ||
} | ||
|
||
export function registerInstalledPackagesChecking( | ||
interpreterPathService: IInterpreterPathService, | ||
disposables: IDisposableRegistry, | ||
): void { | ||
const diagnosticCollection = createDiagnosticCollection(SOURCE); | ||
|
||
disposables.push(diagnosticCollection); | ||
disposables.push( | ||
onDidOpenTextDocument(async (e: TextDocument) => { | ||
if (e.languageId === 'pip-requirements') { | ||
const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, e); | ||
if (diagnostics.length > 0) { | ||
diagnosticCollection.set(e.uri, diagnostics); | ||
} else if (diagnosticCollection.has(e.uri)) { | ||
diagnosticCollection.delete(e.uri); | ||
} | ||
} | ||
}), | ||
onDidSaveTextDocument(async (e: TextDocument) => { | ||
if (e.languageId === 'pip-requirements') { | ||
const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, e); | ||
if (diagnostics.length > 0) { | ||
diagnosticCollection.set(e.uri, diagnostics); | ||
} else if (diagnosticCollection.has(e.uri)) { | ||
diagnosticCollection.delete(e.uri); | ||
} | ||
} | ||
}), | ||
onDidCloseTextDocument((e: TextDocument) => { | ||
if (diagnosticCollection.has(e.uri)) { | ||
diagnosticCollection.delete(e.uri); | ||
} | ||
}), | ||
onDidChangeDiagnostics(async () => { | ||
await setContextForActiveEditor(diagnosticCollection); | ||
}), | ||
onDidChangeActiveTextEditor(async () => { | ||
await setContextForActiveEditor(diagnosticCollection); | ||
}), | ||
); | ||
|
||
getOpenTextDocuments().forEach(async (doc: TextDocument) => { | ||
if (doc.languageId === 'pip-requirements') { | ||
const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, doc); | ||
if (diagnostics.length > 0) { | ||
diagnosticCollection.set(doc.uri, diagnostics); | ||
} else if (diagnosticCollection.has(doc.uri)) { | ||
diagnosticCollection.delete(doc.uri); | ||
} | ||
} | ||
}); | ||
} |