|
| 1 | +use std::ffi::OsString; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use itertools::Itertools; |
| 6 | +use tempfile::tempdir_in; |
| 7 | +use tokio::process::Command; |
| 8 | +use tracing::debug; |
| 9 | + |
| 10 | +use uv_cache::Cache; |
| 11 | +use uv_configuration::PreviewMode; |
| 12 | +use uv_interpreter::PythonEnvironment; |
| 13 | +use uv_requirements::RequirementsSource; |
| 14 | +use uv_warnings::warn_user; |
| 15 | + |
| 16 | +use crate::commands::project::update_environment; |
| 17 | +use crate::commands::ExitStatus; |
| 18 | +use crate::printer::Printer; |
| 19 | + |
| 20 | +/// Run a command. |
| 21 | +#[allow(clippy::too_many_arguments)] |
| 22 | +pub(crate) async fn run( |
| 23 | + target: String, |
| 24 | + args: Vec<OsString>, |
| 25 | + python: Option<String>, |
| 26 | + _isolated: bool, |
| 27 | + preview: PreviewMode, |
| 28 | + cache: &Cache, |
| 29 | + printer: Printer, |
| 30 | +) -> Result<ExitStatus> { |
| 31 | + if preview.is_disabled() { |
| 32 | + warn_user!("`uv tool run` is experimental and may change without warning."); |
| 33 | + } |
| 34 | + |
| 35 | + // TODO(zanieb): Allow users to pass an explicit package name different than the target |
| 36 | + // as well as additional requirements |
| 37 | + let requirements = [RequirementsSource::from_package(target.clone())]; |
| 38 | + |
| 39 | + // TODO(zanieb): When implementing project-level tools, discover the project and check if it has the tool |
| 40 | + // TOOD(zanieb): Determine if we sould layer on top of the project environment if it is present |
| 41 | + |
| 42 | + // If necessary, create an environment for the ephemeral requirements. |
| 43 | + debug!("Syncing ephemeral environment."); |
| 44 | + |
| 45 | + // Discover an interpreter. |
| 46 | + let interpreter = if let Some(python) = python.as_ref() { |
| 47 | + PythonEnvironment::from_requested_python(python, cache)?.into_interpreter() |
| 48 | + } else { |
| 49 | + PythonEnvironment::from_default_python(cache)?.into_interpreter() |
| 50 | + }; |
| 51 | + |
| 52 | + // Create a virtual environment |
| 53 | + // TODO(zanieb): Move this path derivation elsewhere |
| 54 | + let uv_state_path = std::env::current_dir()?.join(".uv"); |
| 55 | + fs_err::create_dir_all(&uv_state_path)?; |
| 56 | + let tmpdir = tempdir_in(uv_state_path)?; |
| 57 | + let venv = uv_virtualenv::create_venv( |
| 58 | + tmpdir.path(), |
| 59 | + interpreter, |
| 60 | + uv_virtualenv::Prompt::None, |
| 61 | + false, |
| 62 | + false, |
| 63 | + )?; |
| 64 | + |
| 65 | + // Install the ephemeral requirements. |
| 66 | + let ephemeral_env = |
| 67 | + Some(update_environment(venv, &requirements, preview, cache, printer).await?); |
| 68 | + |
| 69 | + // TODO(zanieb): Determine the command via the package entry points |
| 70 | + let command = target; |
| 71 | + |
| 72 | + // Construct the command |
| 73 | + let mut process = Command::new(&command); |
| 74 | + process.args(&args); |
| 75 | + |
| 76 | + // Construct the `PATH` environment variable. |
| 77 | + let new_path = std::env::join_paths( |
| 78 | + ephemeral_env |
| 79 | + .as_ref() |
| 80 | + .map(PythonEnvironment::scripts) |
| 81 | + .into_iter() |
| 82 | + .map(PathBuf::from) |
| 83 | + .chain( |
| 84 | + std::env::var_os("PATH") |
| 85 | + .as_ref() |
| 86 | + .iter() |
| 87 | + .flat_map(std::env::split_paths), |
| 88 | + ), |
| 89 | + )?; |
| 90 | + process.env("PATH", new_path); |
| 91 | + |
| 92 | + // Construct the `PYTHONPATH` environment variable. |
| 93 | + let new_python_path = std::env::join_paths( |
| 94 | + ephemeral_env |
| 95 | + .as_ref() |
| 96 | + .map(PythonEnvironment::site_packages) |
| 97 | + .into_iter() |
| 98 | + .flatten() |
| 99 | + .map(PathBuf::from) |
| 100 | + .chain( |
| 101 | + std::env::var_os("PYTHONPATH") |
| 102 | + .as_ref() |
| 103 | + .iter() |
| 104 | + .flat_map(std::env::split_paths), |
| 105 | + ), |
| 106 | + )?; |
| 107 | + process.env("PYTHONPATH", new_python_path); |
| 108 | + |
| 109 | + // Spawn and wait for completion |
| 110 | + // Standard input, output, and error streams are all inherited |
| 111 | + // TODO(zanieb): Throw a nicer error message if the command is not found |
| 112 | + let space = if args.is_empty() { "" } else { " " }; |
| 113 | + debug!( |
| 114 | + "Running `{command}{space}{}`", |
| 115 | + args.iter().map(|arg| arg.to_string_lossy()).join(" ") |
| 116 | + ); |
| 117 | + let mut handle = process.spawn()?; |
| 118 | + let status = handle.wait().await?; |
| 119 | + |
| 120 | + // Exit based on the result of the command |
| 121 | + // TODO(zanieb): Do we want to exit with the code of the child process? Probably. |
| 122 | + if status.success() { |
| 123 | + Ok(ExitStatus::Success) |
| 124 | + } else { |
| 125 | + Ok(ExitStatus::Failure) |
| 126 | + } |
| 127 | +} |
0 commit comments