From aa9072ae311933757b0555800fd23386638901ca Mon Sep 17 00:00:00 2001 From: Julfried Date: Sun, 16 Feb 2025 18:35:08 +0100 Subject: [PATCH] Add function to extract Python version constraint from pyproject.toml --- src/utils/pyproject.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/utils/pyproject.ts b/src/utils/pyproject.ts index 9b00302..f7f816d 100644 --- a/src/utils/pyproject.ts +++ b/src/utils/pyproject.ts @@ -43,3 +43,31 @@ function getRequiredVersion(filePath: string): string | undefined { }; return tomlContent["required-version"]; } + +export function getPythonVersionFromPyProject( + filePath: string, +): string | undefined { + if (!fs.existsSync(filePath)) { + core.warning(`Could not find pyproject.toml file: ${filePath}`); + return undefined; + } + + let pythonVersionConstraint: string | undefined; + try { + const fileContent = fs.readFileSync(filePath, "utf-8"); + const tomlContent = toml.parse(fileContent) as { + tool?: { uv?: { "requires-python"?: string } }; + }; + + pythonVersionConstraint = tomlContent?.tool?.uv?.["requires-python"]; + + if (pythonVersionConstraint) { + core.info(`Extracted Python version constraint from ${filePath}: ${pythonVersionConstraint}`); + } + } catch (err) { + const message = (err as Error).message; + core.warning(`Error while parsing ${filePath} for Python version: ${message}`); + } + + return pythonVersionConstraint; +}