mirror of
https://github.com/astral-sh/setup-uv.git
synced 2026-09-20 12:18:48 +00:00
Expose a Python "identity" output (#1036)
https://github.com/pyca/cryptography/pull/15572#discussion_r3913508686 has the context for this: TL;DR our current `python-version` output mirrors the "request" version exactly, which means that it's insufficient for any downstream that needs to manage its own cache keys (since caches shouldn't be shared across release candidates, but the `uv python` request version doesn't include RC numbers). The first commit here was my attempt to fix this by exposing the runtime Python version, but this too is imprecise: the runtime version doesn't indicate the interpreter variant (e.g. freethreading), which is also important to capture in the cache identity. My solution here is to expose `python-runtime-id`, which is just the `key` of the active Python version from `uv python list --output-format=json`. --------- Signed-off-by: William Woodruff <william@yossarian.net>
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
getPlatform,
|
||||
type Platform,
|
||||
} from "./utils/platforms";
|
||||
import { getPythonRuntimeId } from "./utils/python-runtime";
|
||||
import { resolveUvVersion } from "./version/resolve";
|
||||
|
||||
const sourceDir = __dirname;
|
||||
@@ -101,6 +102,7 @@ async function run(): Promise<void> {
|
||||
|
||||
const detectedPythonVersion = await getPythonVersion(inputs);
|
||||
core.setOutput("python-version", detectedPythonVersion);
|
||||
core.setOutput("python-runtime-id", await getPythonRuntimeId(inputs));
|
||||
|
||||
if (inputs.enableCache) {
|
||||
await restoreCache(inputs, detectedPythonVersion);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import type { SetupInputs } from "./inputs";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export async function getPythonRuntimeId(inputs: SetupInputs): Promise<string> {
|
||||
if (!inputs.activateEnvironment) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
// The venv path restricts results to this invocation's runtime, even if
|
||||
// earlier setup-uv calls installed other Python versions in the same job.
|
||||
const { stdout } = await execFileAsync(
|
||||
"uv",
|
||||
[
|
||||
"python",
|
||||
"list",
|
||||
inputs.venvPath,
|
||||
"--only-installed",
|
||||
"--output-format",
|
||||
"json",
|
||||
],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
const pythons = JSON.parse(stdout);
|
||||
if (
|
||||
!Array.isArray(pythons) ||
|
||||
pythons.length !== 1 ||
|
||||
typeof pythons[0]?.key !== "string" ||
|
||||
pythons[0].key === ""
|
||||
) {
|
||||
throw new Error("Expected one installed Python with a runtime key");
|
||||
}
|
||||
return pythons[0].key;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to identify the activated environment's Python runtime: ${error instanceof Error ? error.message : String(error)}`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user