Only include package versions in Manage Packages dialog if they're supported for the user's version of Python (#14584)

This commit is contained in:
Cory Rivera
2021-03-08 18:05:10 -08:00
committed by GitHub
parent bbdc324f17
commit e2a5859155
6 changed files with 245 additions and 16 deletions

View File

@@ -153,6 +153,11 @@ export function comparePackageVersions(first: string, second: string): number {
}
for (let i = 0; i < firstVersion.length; ++i) {
// Using asterisks means any version number is equivalent, so skip this value
if (firstVersion[i] === '*' || secondVersion[i] === '*') {
continue;
}
let firstVersionNum: string | number = Number(firstVersion[i]);
let secondVersionNum: string | number = Number(secondVersion[i]);
@@ -182,6 +187,86 @@ export function sortPackageVersions(versions: string[], ascending: boolean = tru
});
}
const specifierFirstCharMatch = /[><=!]/;
// Determines if a given package is supported for the provided version of Python
// using the version constraints from the pypi metadata.
export function isPackageSupported(pythonVersion: string, packageVersionConstraints: string[]): boolean {
if (pythonVersion === '') {
return true;
}
// Version constraint strings are formatted like '!=2.7, >=3.5, >=3.6',
// with each package release having its own set of version constraints.
let supportedVersionFound = true;
for (let packageVersionConstraint of packageVersionConstraints) {
if (!packageVersionConstraint) {
continue;
}
let constraintParts = packageVersionConstraint.split(',');
for (let constraint of constraintParts) {
constraint = constraint.trim();
if (constraint.length === 0) {
continue;
}
let splitIndex: number;
if (!constraint[0].match(specifierFirstCharMatch)) {
splitIndex = -1; // No version specifier is included with this version number
} else if ((constraint[0] === '>' || constraint[0] === '<') && constraint[1] !== '=') {
splitIndex = 1;
} else {
splitIndex = 2;
}
let versionSpecifier: string;
let version: string;
if (splitIndex === -1) {
versionSpecifier = '=='; // If there's no version specifier, then we need to match the version exactly
version = constraint;
} else {
versionSpecifier = constraint.slice(0, splitIndex);
version = constraint.slice(splitIndex).trim();
}
let versionComparison = comparePackageVersions(pythonVersion, version);
switch (versionSpecifier) {
case '>=':
supportedVersionFound = versionComparison !== -1;
break;
case '<=':
supportedVersionFound = versionComparison !== 1;
break;
case '>':
supportedVersionFound = versionComparison === 1;
break;
case '<':
supportedVersionFound = versionComparison === -1;
break;
case '==':
supportedVersionFound = versionComparison === 0;
break;
case '!=':
supportedVersionFound = versionComparison !== 0;
break;
default:
// We hit an unexpected version specifier. Rather than throw an error here, we should
// let the package be installable so that we're not too restrictive by mistake.
// Trying to install the package will still throw its own unsupported version error later.
supportedVersionFound = true; // The package is tentatively supported until we find a constraint that fails
break;
}
if (!supportedVersionFound) {
break; // Failed at least one version check, so skip checking the other constraints
}
}
if (supportedVersionFound) {
break; // All constraints passed for this package, so we don't need to check any of the others
}
}
return supportedVersionFound;
}
export function isEditorTitleFree(title: string): boolean {
let hasTextDoc = vscode.workspace.textDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title && !notebookLanguages.find(lang => lang === doc.languageId)) > -1;