Skip to content

Commit 36a03e7

Browse files
authored
Centralize Python version compatibility checks
Refactored Python version compatibility checks to use a centralized function for better maintainability and clarity.
1 parent 2da4d3a commit 36a03e7

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/installer/get-python.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ async function verifyFileIntegrity(filePath, expectedSHA) {
168168
}
169169
}
170170

171+
/**
172+
* Check if Python version is compatible
173+
* @param {string} pythonVersion - Python version string (e.g., "3.13.1")
174+
* @param {boolean} forInstallation - If true, only allows 3.13.x; if false, allows 3.10-3.13
175+
* @returns {boolean} True if version is compatible
176+
*/
177+
function isPythonVersionCompatible(pythonVersion, forInstallation = false) {
178+
const versionParts = pythonVersion.split('.');
179+
const major = parseInt(versionParts[0], 10);
180+
const minor = parseInt(versionParts[1], 10);
181+
182+
if (major !== 3) return false;
183+
184+
if (forInstallation) {
185+
return minor === 13; // Only 3.13.x for installation
186+
} else {
187+
return minor >= 10 && minor <= 13; // 3.10-3.13 for finding existing
188+
}
189+
}
190+
171191
/**
172192
* Search for existing Python executable in system PATH with version validation
173193
* Accepts Python versions 3.10 through 3.13. Stops at first valid installation found.
@@ -226,8 +246,7 @@ async function isValidPythonVersion(executable) {
226246
const versionMatch = output.match(/Python (\d+\.\d+\.\d+)/);
227247
if (!versionMatch) return false;
228248

229-
const version = versionMatch[1];
230-
return semver.gte(version, '3.10.0') && semver.lt(version, '3.14.0');
249+
return isPythonVersionCompatible(versionMatch[1], false); // Allow 3.10-3.13 for finding
231250
} catch {
232251
return false;
233252
}
@@ -405,20 +424,9 @@ async function tryGetRegistryFromRelease(releaseTag, systype) {
405424
* @returns {object|null} Best asset or null if none found
406425
*/
407426
function selectBestAsset(releaseData, systype) {
408-
// Filter compatible assets with multiple naming pattern support
427+
// Filter compatible assets - only Python 3.13.x for installation
409428
const compatibleAssets = releaseData.assets.filter(asset => {
410-
const isCompatible = isAssetCompatible(asset.name, systype) || isAssetCompatibleFallback(asset.name, systype);
411-
if (!isCompatible) return false;
412-
413-
const parsed = parseAssetName(asset.name) || parseAssetNameFallback(asset.name);
414-
if (!parsed) return false;
415-
416-
const versionParts = parsed.pythonVersion.split('.');
417-
const major = parseInt(versionParts[0], 10);
418-
const minor = parseInt(versionParts[1], 10);
419-
420-
// Only Python 3.13.x allowed for installation
421-
return major === 3 && minor === 13;
429+
return isAssetCompatible(asset.name, systype) || isAssetCompatibleFallback(asset.name, systype);
422430
});
423431

424432
if (compatibleAssets.length === 0) {
@@ -520,11 +528,8 @@ function isAssetCompatible(assetName, systype) {
520528
return false;
521529
}
522530

523-
// Quick Python version check for v3.13
524-
const versionParts = parsed.pythonVersion.split('.');
525-
const major = parseInt(versionParts[0], 10);
526-
const minor = parseInt(versionParts[1], 10);
527-
if (major !== 3 || minor !== 13) {
531+
// Only Python 3.13.x for installation (centralized check)
532+
if (!isPythonVersionCompatible(parsed.pythonVersion, true)) {
528533
return false;
529534
}
530535

@@ -561,10 +566,8 @@ function isAssetCompatibleFallback(assetName, systype) {
561566
return false;
562567
}
563568

564-
const versionParts = parsed.pythonVersion.split('.');
565-
const major = parseInt(versionParts[0], 10);
566-
const minor = parseInt(versionParts[1], 10);
567-
if (major !== 3 || minor !== 13) {
569+
// Only Python 3.13.x for installation (centralized check)
570+
if (!isPythonVersionCompatible(parsed.pythonVersion, true)) {
568571
return false;
569572
}
570573

0 commit comments

Comments
 (0)