Skip to content

Commit

Permalink
🔧 add --install-dir arg to uv python commands
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgafni committed Oct 4, 2024
1 parent 172bff4 commit c749923
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
10 changes: 10 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,11 @@ pub struct PythonListArgs {
#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub struct PythonInstallArgs {
/// The directory where Python will be installed.
///
#[arg(long, short, env = "UV_PYTHON_INSTALL_DIR")]
pub install_dir: Option<PathBuf>,

/// The Python version(s) to install.
///
/// If not provided, the requested Python version(s) will be read from the
Expand All @@ -3580,6 +3585,11 @@ pub struct PythonInstallArgs {
#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub struct PythonUninstallArgs {
/// The directory where Python is installed.
///
#[arg(long, short, env = "UV_PYTHON_INSTALL_DIR")]
pub install_dir: Option<PathBuf>,

/// The Python version(s) to uninstall.
///
/// See `uv help python` to view supported request formats.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct ManagedPythonInstallations {

impl ManagedPythonInstallations {
/// A directory for Python installations at `root`.
fn from_path(root: impl Into<PathBuf>) -> Self {
pub fn from_path(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() }
}

Expand Down
8 changes: 7 additions & 1 deletion crates/uv/src/commands/python/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::printer::Printer;
/// Download and install Python versions.
pub(crate) async fn install(
project_dir: &Path,
install_dir: Option<&Path>,
targets: Vec<String>,
reinstall: bool,
python_downloads: PythonDownloads,
Expand All @@ -31,7 +32,12 @@ pub(crate) async fn install(
) -> Result<ExitStatus> {
let start = std::time::Instant::now();

let installations = ManagedPythonInstallations::from_settings()?.init()?;
let installations = if let Some(install_dir) = install_dir {
ManagedPythonInstallations::from_path(install_dir)
} else {
ManagedPythonInstallations::from_settings()?
}
.init()?;
let installations_dir = installations.root();
let cache_dir = installations.cache();
let _lock = installations.lock().await?;
Expand Down
9 changes: 8 additions & 1 deletion crates/uv/src/commands/python/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ use uv_python::PythonRequest;
use crate::commands::python::{ChangeEvent, ChangeEventKind};
use crate::commands::{elapsed, ExitStatus};
use crate::printer::Printer;
use std::path::Path;

/// Uninstall managed Python versions.
pub(crate) async fn uninstall(
install_dir: Option<&Path>,
targets: Vec<String>,
all: bool,

printer: Printer,
) -> Result<ExitStatus> {
let installations = ManagedPythonInstallations::from_settings()?.init()?;
let installations = if let Some(install_dir) = install_dir {
ManagedPythonInstallations::from_path(install_dir)
} else {
ManagedPythonInstallations::from_settings()?.init()?
};

let _lock = installations.lock().await?;

// Perform the uninstallation.
Expand Down
4 changes: 3 additions & 1 deletion crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {

commands::python_install(
&project_dir,
args.install_dir.as_deref(),
args.targets,
args.reinstall,
globals.python_downloads,
Expand All @@ -1033,7 +1034,8 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
let args = settings::PythonUninstallSettings::resolve(args, filesystem);
show_settings!(args);

commands::python_uninstall(args.targets, args.all, printer).await
commands::python_uninstall(args.install_dir.as_deref(), args.targets, args.all, printer)
.await
}
Commands::Python(PythonNamespace {
command: PythonCommand::Find(args),
Expand Down
26 changes: 22 additions & 4 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ impl PythonListSettings {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct PythonInstallSettings {
pub(crate) install_dir: Option<PathBuf>,
pub(crate) targets: Vec<String>,
pub(crate) reinstall: bool,
}
Expand All @@ -593,16 +594,25 @@ impl PythonInstallSettings {
/// Resolve the [`PythonInstallSettings`] from the CLI and filesystem configuration.
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn resolve(args: PythonInstallArgs, _filesystem: Option<FilesystemOptions>) -> Self {
let PythonInstallArgs { targets, reinstall } = args;
let PythonInstallArgs {
install_dir,
targets,
reinstall,
} = args;

Self { targets, reinstall }
Self {
install_dir,
targets,
reinstall,
}
}
}

/// The resolved settings to use for a `python uninstall` invocation.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct PythonUninstallSettings {
pub(crate) install_dir: Option<PathBuf>,
pub(crate) targets: Vec<String>,
pub(crate) all: bool,
}
Expand All @@ -614,9 +624,17 @@ impl PythonUninstallSettings {
args: PythonUninstallArgs,
_filesystem: Option<FilesystemOptions>,
) -> Self {
let PythonUninstallArgs { targets, all } = args;
let PythonUninstallArgs {
install_dir,
targets,
all,
} = args;

Self { targets, all }
Self {
install_dir,
targets,
all,
}
}
}

Expand Down

0 comments on commit c749923

Please sign in to comment.