Skip to content

Commit

Permalink
Add support for uv python dir --bin
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 25, 2024
1 parent 2021ad8 commit f24f880
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
23 changes: 22 additions & 1 deletion crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3852,7 +3852,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),
Expand Down Expand Up @@ -3880,6 +3882,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 {
Expand Down
24 changes: 15 additions & 9 deletions crates/uv/src/commands/python/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
8 changes: 6 additions & 2 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,9 +1112,13 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
.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) => {
Expand Down
19 changes: 18 additions & 1 deletion crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -606,6 +606,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<FilesystemOptions>) -> 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)]
Expand Down
20 changes: 19 additions & 1 deletion docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -4646,6 +4646,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.

<h3 class="cli-reference">Usage</h3>

```
Expand All @@ -4654,7 +4656,23 @@ uv python dir [OPTIONS]

<h3 class="cli-reference">Options</h3>

<dl class="cli-reference"><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>
<dl class="cli-reference"><dt><code>--bin</code></dt><dd><p>Show the directory into which <code>uv python</code> will install Python executables.</p>

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

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

<ul>
<li><code>$UV_PYTHON_BIN_DIR</code></li>

<li><code>$XDG_BIN_HOME</code></li>

<li><code>$XDG_DATA_HOME/../bin</code></li>

<li><code>$HOME/.local/bin</code></li>
</ul>

</dd><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>

<p>Defaults to <code>$HOME/Library/Caches/uv</code> on macOS, <code>$XDG_CACHE_HOME/uv</code> or <code>$HOME/.cache/uv</code> on Linux, and <code>%LOCALAPPDATA%\uv\cache</code> on Windows.</p>

Expand Down

0 comments on commit f24f880

Please sign in to comment.