diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 9363f1b..924857b 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -285,6 +285,23 @@ export function resolveFilename(filename: string): string { return filename; } +export function joinPath(path1: string, ...paths: string[]): string { + let basePath = path1; + if (paths != null && paths.length > 0) { + for (let i = 0; i < paths.length; i++) { + const path2 = paths[i]; + if (basePath.endsWith("/") && path2.startsWith("/")) { + basePath += path2.substring(1); + } else if (basePath.endsWith("/") || path2.startsWith("/")) { + basePath += path2; + } else { + basePath += "/" + path2; + } + } + } + return basePath; +} + export async function existsPath(path: string): Promise { try { const stat = await Deno.stat(path); @@ -507,3 +524,12 @@ Deno.test("test-key-ring-rs", () => { getKeyRingPassword("test-service", "test-user"), ); }); + +Deno.test("join-path", () => { + assertEquals("a/b", joinPath("a/", "/b")); + assertEquals("a/b", joinPath("a/", "b")); + assertEquals("a/b", joinPath("a", "/b")); + assertEquals("a/b", joinPath("a", "b")); + assertEquals("a/b/c", joinPath("a", "b", "/c")); + assertEquals("a/b/c", joinPath("a", "b", "c")); +}); diff --git a/python-ts/main.ts b/python-ts/main.ts index e7ec5fc..7ee0d55 100644 --- a/python-ts/main.ts +++ b/python-ts/main.ts @@ -1,6 +1,15 @@ #!/usr/bin/env runts -- --allow-env --allow-run +import { + existsPath, + joinPath, + log, + readFileToString, + resolveFilename, +} from "https://global.hatter.ink/script/get/@12/deno-commons-mod.ts"; + const PYTHON_CONFIG_FILE = "~/.config/python-config.json"; +const PYTHON_VENV_DEFAULT_BASE_DIR = "~/.venv/"; interface PythonConfig { default_version?: string; @@ -22,6 +31,51 @@ interface PythonVenv { comment?: string; } +async function loadPythonConfig(): Promise { + const pythonConfigFile = resolveFilename(PYTHON_CONFIG_FILE); + const pythonConfigJson = await readFileToString(pythonConfigFile); + if (!pythonConfigJson === null) { + throw `Could not read python config file: ${pythonConfigFile}`; + } + return JSON.parse(pythonConfigJson) as PythonConfig; +} + +async function newVirtualEnv(pythonVersion: string | null, pythonVenv: string) { + const pythonConfig = await loadPythonConfig(); + const selectedPythonVersion = pythonVersion || + pythonConfig.default_version || null; + if (!selectedPythonVersion) { + throw `No Python version assigned.`; + } + if (!selectedPythonVersion) { + throw `No Python venv assigned.`; + } + const pythonVersionProfile = pythonConfig.versions[pythonVersion]; + if (!pythonVersionProfile) { + throw `Python version: ${pythonVersion} not found`; + } + log.success(`Found Python version: ${pythonVersion}`); + const pythonVenvProfile = pythonConfig.profiles[pythonVenv]; + if (pythonVenvProfile) { + throw `Python venv already exists: ${pythonVenv}`; + } + const pythonVenvBaseDir = resolveFilename( + pythonConfig.python_venv_base_path || PYTHON_VENV_DEFAULT_BASE_DIR, + ); + if (!existsPath(pythonVenvBaseDir)) { + log.info(`Make python venv base dir: ${pythonVenvBaseDir}`); + Deno.mkdirSync(pythonVenvBaseDir); + } + const pythonVenvDir = joinPath(pythonVenvBaseDir, pythonVenv); + if (existsPath(pythonVenvDir)) { + throw `Python venv: ${pythonVenvDir} already exists`; + } + + // python3 -m venv myenv + const python3Cmd = joinPath(pythonVersionProfile.path, "bin", "python3"); + const pythonVenvArgs = ["-m", "vent", pythonVenvDir]; +} + async function main() { // TODO ... }