Skip to content

Commit

Permalink
Display the target virtual environment path if non-default
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 1, 2024
1 parent abf4823 commit c7f81f9
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 48 deletions.
10 changes: 3 additions & 7 deletions crates/uv/src/commands/pip/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use std::time::Instant;

use anyhow::Result;
use owo_colors::OwoColorize;
use tracing::debug;

use distribution_types::{Diagnostic, InstalledDist};
use uv_cache::Cache;
use uv_fs::Simplified;
use uv_installer::{SitePackages, SitePackagesDiagnostic};
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};

use crate::commands::{elapsed, ExitStatus};
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Check for incompatibilities in installed packages.
pub(crate) fn pip_check(
python: Option<&str>,
Expand All @@ -30,11 +30,7 @@ pub(crate) fn pip_check(
cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, cache, printer)?;

// Build the installed index.
let site_packages = SitePackages::from_environment(&environment)?;
Expand Down
10 changes: 3 additions & 7 deletions crates/uv/src/commands/pip/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use std::fmt::Write;
use anyhow::Result;
use itertools::Itertools;
use owo_colors::OwoColorize;
use tracing::debug;

use distribution_types::{Diagnostic, InstalledDist, Name};
use uv_cache::Cache;
use uv_fs::Simplified;
use uv_installer::SitePackages;
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};

use crate::commands::ExitStatus;
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Enumerate the installed packages in the current environment.
pub(crate) fn pip_freeze(
exclude_editable: bool,
Expand All @@ -30,11 +30,7 @@ pub(crate) fn pip_freeze(
cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, cache, printer)?;

// Build the installed index.
let site_packages = SitePackages::from_environment(&environment)?;
Expand Down
8 changes: 3 additions & 5 deletions crates/uv/src/commands/pip/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Install packages into the current environment.
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) async fn pip_install(
Expand Down Expand Up @@ -148,11 +150,7 @@ pub(crate) async fn pip_install(
&cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, &cache, &printer)?;

// Apply any `--target` or `--prefix` directories.
let environment = if let Some(target) = target {
Expand Down
9 changes: 3 additions & 6 deletions crates/uv/src/commands/pip/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anyhow::Result;
use itertools::Itertools;
use owo_colors::OwoColorize;
use serde::Serialize;
use tracing::debug;
use unicode_width::UnicodeWidthStr;

use distribution_types::{Diagnostic, InstalledDist, Name};
Expand All @@ -20,6 +19,8 @@ use uv_python::{EnvironmentPreference, PythonEnvironment};
use crate::commands::ExitStatus;
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Enumerate the installed packages in the current environment.
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) fn pip_list(
Expand All @@ -39,11 +40,7 @@ pub(crate) fn pip_list(
cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, cache, printer)?;

// Build the installed index.
let site_packages = SitePackages::from_environment(&environment)?;
Expand Down
43 changes: 43 additions & 0 deletions crates/uv/src/commands/pip/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::{BTreeSet, HashSet};
use std::fmt::Write;
use std::path::PathBuf;
use tracing::debug;
use uv_tool::InstalledTools;

use distribution_types::{
CachedDist, Diagnostic, InstalledDist, LocalDist, NameRequirementSpecification,
Expand Down Expand Up @@ -538,6 +539,48 @@ pub(crate) async fn install(
Ok(changelog)
}

/// Display a message
pub(crate) fn report_target_environment(
env: &PythonEnvironment,
cache: &Cache,
printer: Printer,
) -> Result<(), Error> {
let message = format!(
"Using Python {} environment at {}",
env.interpreter().python_version(),
env.root().user_display()
);

let Ok(target) = env.root().canonicalize() else {
debug!("{}", message);
return Ok(());
};

// Do not report environments in the cache
if target.starts_with(cache.root()) {
debug!("{}", message);
return Ok(());
}

// Do not report tool environments
if let Ok(tools) = InstalledTools::from_settings() {
if target.starts_with(tools.root()) {
debug!("{}", message);
return Ok(());
}
}

// Do not a default environments path
if let Ok(default) = PathBuf::from(".venv").canonicalize() {
if target == default {
debug!("{}", message);
return Ok(());
}
}

Ok(writeln!(printer.stderr(), "{}", message.dimmed())?)
}

/// Report on the results of a dry-run installation.
fn report_dry_run(
resolution: &Resolution,
Expand Down
9 changes: 3 additions & 6 deletions crates/uv/src/commands/pip/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use anyhow::Result;
use itertools::{Either, Itertools};
use owo_colors::OwoColorize;
use rustc_hash::FxHashMap;
use tracing::debug;

use distribution_types::{Diagnostic, Name};
use uv_cache::Cache;
Expand All @@ -16,6 +15,8 @@ use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};
use crate::commands::ExitStatus;
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Show information about one or more installed packages.
pub(crate) fn pip_show(
mut packages: Vec<PackageName>,
Expand Down Expand Up @@ -45,11 +46,7 @@ pub(crate) fn pip_show(
cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, cache, printer)?;

// Build the installed index.
let site_packages = SitePackages::from_environment(&environment)?;
Expand Down
8 changes: 3 additions & 5 deletions crates/uv/src/commands/pip/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Install a set of locked requirements into the current Python environment.
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) async fn pip_sync(
Expand Down Expand Up @@ -132,11 +134,7 @@ pub(crate) async fn pip_sync(
&cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, &cache, &printer)?;

// Apply any `--target` or `--prefix` directories.
let environment = if let Some(target) = target {
Expand Down
10 changes: 3 additions & 7 deletions crates/uv/src/commands/pip/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use anyhow::Result;
use indexmap::IndexMap;
use owo_colors::OwoColorize;
use rustc_hash::{FxHashMap, FxHashSet};
use tracing::debug;

use distribution_types::{Diagnostic, Name};
use pypi_types::{RequirementSource, ResolverMarkerEnvironment};
use uv_cache::Cache;
use uv_distribution::Metadata;
use uv_fs::Simplified;
use uv_installer::SitePackages;
use uv_normalize::PackageName;
use uv_python::EnvironmentPreference;
Expand All @@ -20,6 +18,8 @@ use uv_python::PythonRequest;
use crate::commands::ExitStatus;
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Display the installed packages in the current environment as a dependency tree.
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) fn pip_tree(
Expand All @@ -42,11 +42,7 @@ pub(crate) fn pip_tree(
cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan()
);
report_target_environment(&environment, cache, printer)?;

// Read packages from the virtual environment.
let site_packages = SitePackages::from_environment(&environment)?;
Expand Down
8 changes: 3 additions & 5 deletions crates/uv/src/commands/pip/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use uv_requirements::{RequirementsSource, RequirementsSpecification};
use crate::commands::{elapsed, ExitStatus};
use crate::printer::Printer;

use super::operations::report_target_environment;

/// Uninstall packages from the current environment.
pub(crate) async fn pip_uninstall(
sources: &[RequirementsSource],
Expand Down Expand Up @@ -57,11 +59,7 @@ pub(crate) async fn pip_uninstall(
&cache,
)?;

debug!(
"Using Python {} environment at {}",
environment.interpreter().python_version(),
environment.python_executable().user_display().cyan(),
);
report_target_environment(&environment, &cache, &printer)?;

// Apply any `--target` or `--prefix` directories.
let environment = if let Some(target) = target {
Expand Down
1 change: 1 addition & 0 deletions crates/uv/tests/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ fn relative_path() -> Result<()> {
----- stdout -----
----- stderr -----
Using Python 3.12.[X] environment at [VENV]/
Resolved 3 packages in [TIME]
Prepared 3 packages in [TIME]
Installed 3 packages in [TIME]
Expand Down
Loading

0 comments on commit c7f81f9

Please sign in to comment.