Skip to content

Refactor setupPython to use uv python pin & venv setup #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 76 additions & 14 deletions src/setup-uv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "./utils/inputs";
import * as exec from "@actions/exec";
import fs from "node:fs";
import { getUvVersionFromConfigFile } from "./utils/pyproject";
import { getUvVersionFromConfigFile, getPythonVersionFromPyProject } from "./utils/pyproject";

async function run(): Promise<void> {
const platform = getPlatform();
Expand Down Expand Up @@ -146,23 +146,85 @@ function setToolDir(): void {
}

async function setupPython(): Promise<void> {
if (pythonVersion !== "") {
core.exportVariable("UV_PYTHON", pythonVersion);
core.info(`Set UV_PYTHON to ${pythonVersion}`);
const options: exec.ExecOptions = {
silent: !core.isDebug(),
};
const execArgs = ["venv", "--python", pythonVersion];
const options: exec.ExecOptions = {
silent: !core.isDebug(),
};

// Case (1): No Python version and no pyproject.toml file
if (pythonVersion === "" && pyProjectFile === "") {
core.info("No Python setup required.");
return;
}

core.info("Activating python venv...");
// Case (2): Python version is provided and no pyproject.toml file
else if (pythonVersion !== "" && pyProjectFile === "") {
const execArgs = ["pin", "python", pythonVersion];
core.info(`Pinning Python version to ${pythonVersion}...`);
await exec.exec("uv", execArgs, options);
}

// Case (3): No Python version and pyproject.toml file
else if (pythonVersion === "" && pyProjectFile !== "") {
const extractedPythonVersion = getPythonVersionFromPyProject(pyProjectFile);

let venvBinPath = ".venv/bin";
if (process.platform === "win32") {
venvBinPath = ".venv/Scripts";
if (!extractedPythonVersion){
core.warning(
`Could not find python version in pyproject.toml. Won't setup python.`,
);
return;
}
core.addPath(path.resolve(venvBinPath));
core.exportVariable("VIRTUAL_ENV", path.resolve(".venv"));

const execArgs = ["pin", "python", extractedPythonVersion, "--project", pyProjectFile];
core.info(`Pinning Python version to ${extractedPythonVersion}...`);
await exec.exec("uv", execArgs, options);
}

// Case (4): Pin python version using uv pin if python version is provided and pyproject.toml file is present
if (pythonVersion !== "" && pyProjectFile !== "") {
const execArgs = ["pin", "python", pythonVersion, "--project", pyProjectFile];
core.info(`Pinning Python version to ${pythonVersion}...`);
await exec.exec("uv", execArgs, options);
}

// Extract the pinned python version
let pinnedPythonVersion = getPinnedPythonVersion();
if (!pinnedPythonVersion) {
core.setFailed("Failed to determine pinned Python version after uv pin.");
return;
}

// Setup and activate venv
// Set UV_PYHTON to the pinned python version
core.exportVariable("UV_PYTHON", pinnedPythonVersion);
core.info(`Setting UV_PYTHON to ${pinnedPythonVersion}`);

core.info("Activating Python venv...");
const execArgs = ["venv"];
await exec.exec("uv", execArgs, options);

let venvBinPath = ".venv/bin";
if (process.platform === "win32") {
venvBinPath = ".venv/Scripts";
}
core.addPath(path.resolve(venvBinPath));
core.exportVariable("VIRTUAL_ENV", path.resolve(".venv"));
}

function getPinnedPythonVersion(): string | undefined {
const pythonVersionFile = ".python-version";

if (!fs.existsSync(pythonVersionFile)) {
core.warning(`No .python-version file found after uv pin.`);
return undefined;
}

try {
const version = fs.readFileSync(pythonVersionFile, "utf-8").trim();
core.info(`Detected pinned Python version from .python-version: ${version}`);
return version;
} catch (error) {
core.warning(`Failed to read .python-version: ${error}`);
return undefined;
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/utils/pyproject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}