From ac1503b1d02001b91175a973533798fa1bbdbce2 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 28 Oct 2024 15:01:39 -0500 Subject: [PATCH] Add support for `uv python dir --bin` --- crates/uv-cli/src/lib.rs | 23 ++++++++++++++++++++++- crates/uv/src/commands/python/dir.rs | 24 +++++++++++++++--------- crates/uv/src/lib.rs | 8 ++++++-- crates/uv/src/settings.rs | 19 ++++++++++++++++++- docs/reference/cli.md | 18 ++++++++++++++++++ 5 files changed, 79 insertions(+), 13 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 0d31624f6353..b733e7ef751d 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -3836,7 +3836,9 @@ pub enum PythonCommand { /// `%APPDATA%\uv\data\python` on Windows. /// /// The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`. - Dir, + /// + /// To instead view the directory uv installs Python executables into, use the `--bin` flag. + Dir(PythonDirArgs), /// Uninstall Python versions. Uninstall(PythonUninstallArgs), @@ -3864,6 +3866,25 @@ pub struct PythonListArgs { pub only_installed: bool, } +#[derive(Args)] +#[allow(clippy::struct_excessive_bools)] +pub struct PythonDirArgs { + /// Show the directory into which `uv python` will install Python executables. + /// + /// By default, `uv python dir` shows the directory into which the Python distributions + /// themselves are installed, rather than the directory containing the linked executables. + /// + /// The Python executable directory is determined according to the XDG standard and is derived + /// from the following environment variables, in order of preference: + /// + /// - `$UV_PYTHON_BIN_DIR` + /// - `$XDG_BIN_HOME` + /// - `$XDG_DATA_HOME/../bin` + /// - `$HOME/.local/bin` + #[arg(long, verbatim_doc_comment)] + pub bin: bool, +} + #[derive(Args)] #[allow(clippy::struct_excessive_bools)] pub struct PythonInstallArgs { diff --git a/crates/uv/src/commands/python/dir.rs b/crates/uv/src/commands/python/dir.rs index d83668fe1a03..7c6a1b325726 100644 --- a/crates/uv/src/commands/python/dir.rs +++ b/crates/uv/src/commands/python/dir.rs @@ -3,15 +3,21 @@ use anyhow::Context; use owo_colors::OwoColorize; use uv_fs::Simplified; -use uv_python::managed::ManagedPythonInstallations; +use uv_python::managed::{python_executable_dir, ManagedPythonInstallations}; + +/// Show the Python installation directory. +pub(crate) fn dir(bin: bool) -> anyhow::Result<()> { + if bin { + let bin = python_executable_dir()?; + println!("{}", bin.simplified_display().cyan()); + } else { + let installed_toolchains = ManagedPythonInstallations::from_settings() + .context("Failed to initialize toolchain settings")?; + println!( + "{}", + installed_toolchains.root().simplified_display().cyan() + ); + } -/// Show the toolchain directory. -pub(crate) fn dir() -> anyhow::Result<()> { - let installed_toolchains = ManagedPythonInstallations::from_settings() - .context("Failed to initialize toolchain settings")?; - println!( - "{}", - installed_toolchains.root().simplified_display().cyan() - ); Ok(()) } diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index 61e753bbab44..a577d629f237 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -1118,9 +1118,13 @@ async fn run(mut cli: Cli) -> Result { .await } Commands::Python(PythonNamespace { - command: PythonCommand::Dir, + command: PythonCommand::Dir(args), }) => { - commands::python_dir()?; + // Resolve the settings from the command-line arguments and workspace configuration. + let args = settings::PythonDirSettings::resolve(args, filesystem); + show_settings!(args); + + commands::python_dir(args.bin)?; Ok(ExitStatus::Success) } Commands::Publish(args) => { diff --git a/crates/uv/src/settings.rs b/crates/uv/src/settings.rs index 9b287ce7e597..a96fc8787419 100644 --- a/crates/uv/src/settings.rs +++ b/crates/uv/src/settings.rs @@ -8,7 +8,7 @@ use url::Url; use uv_cache::{CacheArgs, Refresh}; use uv_cli::{ options::{flag, resolver_installer_options, resolver_options}, - AuthorFrom, BuildArgs, ExportArgs, PublishArgs, ToolUpgradeArgs, + AuthorFrom, BuildArgs, ExportArgs, PublishArgs, PythonDirArgs, ToolUpgradeArgs, }; use uv_cli::{ AddArgs, ColorChoice, ExternalCommand, GlobalArgs, InitArgs, ListFormat, LockArgs, Maybe, @@ -623,6 +623,23 @@ impl PythonListSettings { } } +/// The resolved settings to use for a `python dir` invocation. +#[allow(clippy::struct_excessive_bools)] +#[derive(Debug, Clone)] +pub(crate) struct PythonDirSettings { + pub(crate) bin: bool, +} + +impl PythonDirSettings { + /// Resolve the [`PythonDirSettings`] from the CLI and filesystem configuration. + #[allow(clippy::needless_pass_by_value)] + pub(crate) fn resolve(args: PythonDirArgs, _filesystem: Option) -> Self { + let PythonDirArgs { bin } = args; + + Self { bin } + } +} + /// The resolved settings to use for a `python install` invocation. #[allow(clippy::struct_excessive_bools)] #[derive(Debug, Clone)] diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 961104d90feb..f2e3038d3df9 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -4786,6 +4786,8 @@ By default, Python installations are stored in the uv data directory at `$XDG_DA The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`. +To instead view the directory uv installs Python executables into, use the `--bin` flag. +

Usage

``` @@ -4803,6 +4805,22 @@ uv python dir [OPTIONS]

WARNING: Hosts included in this list will not be verified against the system’s certificate store. Only use --allow-insecure-host in a secure network with verified sources, as it bypasses SSL verification and could expose you to MITM attacks.

May also be set with the UV_INSECURE_HOST environment variable.

+
--bin

Show the directory into which uv python will install Python executables.

+ +

By default, uv python dir shows the directory into which the Python distributions themselves are installed, rather than the directory containing the linked executables.

+ +

The Python executable directory is determined according to the XDG standard and is derived from the following environment variables, in order of preference:

+ +
    +
  • $UV_PYTHON_BIN_DIR
  • + +
  • $XDG_BIN_HOME
  • + +
  • $XDG_DATA_HOME/../bin
  • + +
  • $HOME/.local/bin
  • +
+
--cache-dir cache-dir

Path to the cache directory.

Defaults to $XDG_CACHE_HOME/uv or $HOME/.cache/uv on macOS and Linux, and %LOCALAPPDATA%\uv\cache on Windows.