From 0e012522a4fece66a5c349dd092f02f960793e4a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 26 Jan 2026 22:39:58 +0800 Subject: [PATCH] python.rs opt version match and sort --- python-ts/main.ts | 55 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/python-ts/main.ts b/python-ts/main.ts index 05b9674..e1ddcd4 100755 --- a/python-ts/main.ts +++ b/python-ts/main.ts @@ -12,7 +12,7 @@ import { term, writeStringToFile, } from "https://global.hatter.ink/script/get/@16/deno-commons-mod.ts"; -import { parseArgs } from "jsr:@std/cli/parse-args"; +import {parseArgs} from "jsr:@std/cli/parse-args"; const PYTHON_CONFIG_FILE = "~/.config/python-config.json"; const PYTHON_VENV_DEFAULT_BASE_DIR = "~/.venv/"; @@ -35,6 +35,34 @@ interface PythonVenv { comment?: string; } +function matchVersion(version: string, versionFilter: string): boolean { + if (version === versionFilter) { + return true; + } + return version.startsWith( + versionFilter.endsWith(".") ? versionFilter : (versionFilter + "."), + ); +} + +function findPythonVersion( + pythonConfig: PythonConfig, + version: string, +): PythonVersion | null { + if (!pythonConfig.versions) { + return null; + } + let pythonVersion: PythonVersion | null = null; + for (let ver in pythonConfig.versions) { + if (matchVersion(ver, version)) { + if (pythonVersion !== null) { + throw `Too many versions matched, version filter: ${version}`; + } + pythonVersion = pythonConfig.versions[ver]; + } + } + return pythonVersion; +} + async function isFile(path: string): Promise { const fileInfo = await Deno.stat(path); return fileInfo?.isFile; @@ -108,7 +136,7 @@ async function addVirtualEnv( if (!pythonVenv) { throw `No Python virtual environment assigned.`; } - const pythonVersionProfile = pythonConfig.versions[pythonVersion]; + const pythonVersionProfile = findPythonVersion(pythonConfig, pythonVersion); if (!pythonVersionProfile) { throw `Python version: ${pythonVersion} not found`; } @@ -176,7 +204,7 @@ async function handlePython(args: string[]) { return; } - const versions = []; + const versions: string[] = []; let maxVersionLength = 0; for (let version in pythonConfig.versions) { versions.push(version); @@ -184,7 +212,22 @@ async function handlePython(args: string[]) { maxVersionLength = version.length; } } - versions.sort(); + versions.sort((a, b) => { + const versionAParts = a.split("."); + const versionBParts = b.split("."); + const minLen = Math.min(versionAParts.length, versionBParts.length); + for (let i = 0; i < minLen; i++) { + const ai = parseInt(versionAParts[i]); + const bi = parseInt(versionBParts[i]); + if (ai !== bi) { + return (ai < bi) ? -1 : 1; + } + } + if (versionAParts.length === versionBParts.length) { + return 0; + } + return (versionAParts.length < versionBParts.length) ? -1 : 1; + }); console.log(`Found ${versions.length} Python version(s)`); for (let version in pythonConfig.versions) { @@ -475,5 +518,5 @@ async function main() { main().catch((e) => log.error(e)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260126T003238+08:00.MEYCIQDwBR9b75cKiOgaUUP3 -// Jt4TPZBF2CDBh1cKKC4MgNSWNwIhAINFBhTxBWfomgo9uYcAYpYBM4ngc/F/jrAV+hnnO0/H +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260126T223933+08:00.MEQCIBEiaSlHYdEfYuxM0YQY +// TMERvlNGjaCn30i4zn1h4neWAiBj21PQhqP/z+55V/x9aZ6/uaLO17dg70bRzNn8BKN9+Q==