mirror of
https://github.com/astral-sh/setup-uv.git
synced 2025-04-07 07:58:48 -04:00
Merge 5770883575
into 839076380b
This commit is contained in:
commit
96cdc5676d
2 changed files with 104 additions and 14 deletions
|
@ -28,7 +28,7 @@ import {
|
||||||
} from "./utils/inputs";
|
} from "./utils/inputs";
|
||||||
import * as exec from "@actions/exec";
|
import * as exec from "@actions/exec";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { getUvVersionFromConfigFile } from "./utils/pyproject";
|
import { getUvVersionFromConfigFile, getPythonVersionFromPyProject } from "./utils/pyproject";
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
detectEmptyWorkdir();
|
detectEmptyWorkdir();
|
||||||
|
@ -164,15 +164,60 @@ function setToolDir(): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setupPython(): Promise<void> {
|
async function setupPython(): Promise<void> {
|
||||||
if (pythonVersion !== "") {
|
|
||||||
core.exportVariable("UV_PYTHON", pythonVersion);
|
|
||||||
core.info(`Set UV_PYTHON to ${pythonVersion}`);
|
|
||||||
const options: exec.ExecOptions = {
|
const options: exec.ExecOptions = {
|
||||||
silent: !core.isDebug(),
|
silent: !core.isDebug(),
|
||||||
};
|
};
|
||||||
const execArgs = ["venv", "--python", pythonVersion];
|
|
||||||
|
|
||||||
core.info("Activating python venv...");
|
// Case (1): No Python version and no pyproject.toml file
|
||||||
|
if (pythonVersion === "" && pyProjectFile === "") {
|
||||||
|
core.info("No Python setup required.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
if (!extractedPythonVersion){
|
||||||
|
core.warning(
|
||||||
|
`Could not find python version in pyproject.toml. Won't setup python.`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
await exec.exec("uv", execArgs, options);
|
||||||
|
|
||||||
let venvBinPath = ".venv/bin";
|
let venvBinPath = ".venv/bin";
|
||||||
|
@ -182,6 +227,23 @@ async function setupPython(): Promise<void> {
|
||||||
core.addPath(path.resolve(venvBinPath));
|
core.addPath(path.resolve(venvBinPath));
|
||||||
core.exportVariable("VIRTUAL_ENV", path.resolve(".venv"));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCacheDir(cacheLocalPath: string): void {
|
function setCacheDir(cacheLocalPath: string): void {
|
||||||
|
|
|
@ -44,3 +44,31 @@ function getRequiredVersion(filePath: string): string | undefined {
|
||||||
};
|
};
|
||||||
return tomlContent["required-version"];
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue