update python.ts
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
writeStringToFile,
|
||||
} from "https://global.hatter.ink/script/get/@16/deno-commons-mod.ts";
|
||||
import { parseArgs } from "jsr:@std/cli/parse-args";
|
||||
import { assertEquals } from "jsr:@std/assert";
|
||||
|
||||
const PYTHON_CONFIG_FILE = "~/.config/python-config.json";
|
||||
const PYTHON_VENV_DEFAULT_BASE_DIR = "~/.venv/";
|
||||
@@ -128,7 +129,7 @@ async function addVirtualEnv(
|
||||
pythonVersion: string | null,
|
||||
pythonVenv: string,
|
||||
comment?: string,
|
||||
) {
|
||||
): Promise<void> {
|
||||
const pythonConfig = await loadPythonConfig();
|
||||
if (!pythonVersion) {
|
||||
throw `No Python version assigned.`;
|
||||
@@ -190,6 +191,23 @@ async function addVirtualEnv(
|
||||
await savePythonConfig(pythonConfig);
|
||||
}
|
||||
|
||||
function versionSort(a: string, b: string): number {
|
||||
const versionAParts = a.split(".");
|
||||
const versionBParts = b.split(".");
|
||||
const minLen = Math.min(versionAParts.length, versionBParts.length);
|
||||
for (let i: number = 0; i < minLen; i++) {
|
||||
const ai = parseInt(versionAParts[i], 10);
|
||||
const bi = parseInt(versionBParts[i], 10);
|
||||
if (ai !== bi) {
|
||||
return (ai < bi) ? -1 : 1;
|
||||
}
|
||||
}
|
||||
if (versionAParts.length === versionBParts.length) {
|
||||
return 0;
|
||||
}
|
||||
return (versionAParts.length < versionBParts.length) ? -1 : 1;
|
||||
}
|
||||
|
||||
async function handlePython(args: string[]) {
|
||||
const flags = parseArgs(Deno.args, {
|
||||
boolean: ["help"],
|
||||
@@ -206,31 +224,16 @@ async function handlePython(args: string[]) {
|
||||
|
||||
const versions: string[] = [];
|
||||
let maxVersionLength = 0;
|
||||
for (let version in pythonConfig.versions) {
|
||||
for (const version in pythonConfig.versions) {
|
||||
versions.push(version);
|
||||
if (version.length > maxVersionLength) {
|
||||
maxVersionLength = version.length;
|
||||
}
|
||||
}
|
||||
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], 10);
|
||||
const bi = parseInt(versionBParts[i], 10);
|
||||
if (ai !== bi) {
|
||||
return (ai < bi) ? -1 : 1;
|
||||
}
|
||||
}
|
||||
if (versionAParts.length === versionBParts.length) {
|
||||
return 0;
|
||||
}
|
||||
return (versionAParts.length < versionBParts.length) ? -1 : 1;
|
||||
});
|
||||
versions.sort(versionSort);
|
||||
|
||||
console.log(`Found ${versions.length} Python version(s)`);
|
||||
for (let version in pythonConfig.versions) {
|
||||
for (const version of versions) {
|
||||
const pythonVersion = pythonConfig.versions[version];
|
||||
const versionPadding = " ".repeat(
|
||||
maxVersionLength - version.length,
|
||||
@@ -519,5 +522,14 @@ async function main() {
|
||||
|
||||
main().catch((e) => log.error(e));
|
||||
|
||||
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260126T224501+08:00.MEYCIQCcH+T4kogAD0bc8MS7
|
||||
// 1HmKATErZhVwcyiJRc0NVYl8cQIhAMObac/Id8AZcSdrvZ/dow5a+RXdRjREPSkMqJe7w4TW
|
||||
Deno.test("versionSort", () => {
|
||||
const versions1 = ["3.10", "3.7"];
|
||||
versions1.sort(versionSort);
|
||||
assertEquals(["3.7", "3.10"], versions1);
|
||||
const versions2 = ["3.10", "3.10.1", "3.11", "3.7"];
|
||||
versions2.sort(versionSort);
|
||||
assertEquals(["3.7", "3.10", "3.10.1", "3.11"], versions2);
|
||||
});
|
||||
|
||||
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260127T221900+08:00.MEYCIQC/ePZbN/H3JAOhZ43u
|
||||
// 4ek90MK1IFKLKTynP9RHYRgxTAIhALWtaXAarv25CKcd1ok4AYfuw5ooR68smBzOokmBsEfI
|
||||
|
||||
@@ -168,11 +168,11 @@
|
||||
},
|
||||
"python.ts": {
|
||||
"script_name": "python.ts",
|
||||
"script_length": 15933,
|
||||
"script_sha256": "e35408f8167b06a924e667ae617a761fe0d8a9d991da19f04e9e34a9bd75acf8",
|
||||
"script_length": 16299,
|
||||
"script_sha256": "774cde4fc706489444e4ba0a88bb5db16cb8fe3f16ea37d1468d9ff4e8b78975",
|
||||
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/python-ts/main.ts",
|
||||
"publish_time": 1768236234264,
|
||||
"update_time": 1769438707872
|
||||
"update_time": 1769523568468
|
||||
},
|
||||
"random.ts": {
|
||||
"script_name": "random.ts",
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
},
|
||||
"python-ts": {
|
||||
"script_name": "python-ts",
|
||||
"script_length": 15933,
|
||||
"script_sha256": "e35408f8167b06a924e667ae617a761fe0d8a9d991da19f04e9e34a9bd75acf8"
|
||||
"script_length": 16299,
|
||||
"script_sha256": "774cde4fc706489444e4ba0a88bb5db16cb8fe3f16ea37d1468d9ff4e8b78975"
|
||||
},
|
||||
"sigstore-verify-ts": {
|
||||
"script_name": "sigstore-verify-ts",
|
||||
|
||||
Reference in New Issue
Block a user