From 07c2e15e5e7ef3450013c8b75e682c1d55293f81 Mon Sep 17 00:00:00 2001 From: LukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:03:51 +0200 Subject: [PATCH] Force the use of uv-managed Python --- verifier/src/main.rs | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/verifier/src/main.rs b/verifier/src/main.rs index 1c0b5a2..228dec0 100644 --- a/verifier/src/main.rs +++ b/verifier/src/main.rs @@ -1,28 +1,49 @@ +use std::{path::Path, process::Output}; + use duct::cmd; fn main() -> Result<(), Box> { - let venv = std::env::current_dir()?.join(".venv"); + let venv = std::env::current_dir()? + .canonicalize() + .expect("Failed to determine the canonical form of the current directory path") + .join(".venv"); if venv.try_exists().ok() != Some(true) { // Create the project-specific Python virtual environment. - println!("Creating the Python virtual environment..."); - cmd!("uv", "sync", "--no-install-workspace").run()?; + println!("\n=== Creating the Python virtual environment ===\n"); + cmd!("uv", "sync", "--no-install-workspace", "--managed-python") + .stderr_to_stdout() + .run()?; } - cmd!("uv", "sync", "--all-packages") - // Tell `pyo3` where to find the project-specific - // virtual environment. - .env("VIRTUAL_ENV", &venv) - .run()?; + println!("\n=== Syncing Python packages ===\n"); + run_in_venv( + cmd!("uv", "sync", "--all-packages", "--managed-python"), + &venv, + )?; - println!("Testing the Rust crate..."); - cmd!("cargo", "test") - // Tell `pyo3` where to find the project-specific - // virtual environment. - .env("VIRTUAL_ENV", venv) - .run()?; - println!("Testing the Python package..."); - cmd!("uv", "run", "pytest").run()?; + println!("\n=== Testing the Rust crate ===\n"); + run_in_venv(cmd!("cargo", "test"), &venv)?; + + println!("\n=== Testing the Python package ===\n"); + run_in_venv(cmd!("uv", "run", "--managed-python", "pytest"), &venv)?; Ok(()) } + +/// Execute a command in an environment that's equivalent to what you'd get +/// after activating a Python virtual environment in a shell session via +/// `source .venv/bin/activate`. +fn run_in_venv(command: duct::Expression, venv: &Path) -> std::io::Result { + let path = { + let old = std::env::var("PATH").unwrap_or_default(); + format!("{}:{old}", venv.join("bin").display()) + }; + + command + .stderr_to_stdout() + .env("VIRTUAL_ENV", &venv) + .env("PATH", &path) + .env_remove("PYTHON_HOME") + .run() +}