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 364cfc1
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 48 deletions.
9 changes: 2 additions & 7 deletions crates/uv/src/commands/pip/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ 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::pip::operations::report_target_environment;
use crate::commands::{elapsed, ExitStatus};
use crate::printer::Printer;

Expand All @@ -30,11 +29,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
9 changes: 2 additions & 7 deletions crates/uv/src/commands/pip/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ 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::pip::operations::report_target_environment;
use crate::commands::ExitStatus;
use crate::printer::Printer;

Expand All @@ -30,11 +29,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
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use uv_resolver::{
use uv_types::{BuildIsolation, HashStrategy};

use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger, InstallLogger};
use crate::commands::pip::operations::report_target_environment;
use crate::commands::pip::operations::Modifications;
use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{ExitStatus, SharedState};
Expand Down Expand Up @@ -148,11 +149,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
8 changes: 2 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 @@ -17,6 +16,7 @@ use uv_normalize::PackageName;
use uv_python::PythonRequest;
use uv_python::{EnvironmentPreference, PythonEnvironment};

use crate::commands::pip::operations::report_target_environment;
use crate::commands::ExitStatus;
use crate::printer::Printer;

Expand All @@ -39,11 +39,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 report a default environment 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
8 changes: 2 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 @@ -13,6 +12,7 @@ use uv_installer::SitePackages;
use uv_normalize::PackageName;
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};

use crate::commands::pip::operations::report_target_environment;
use crate::commands::ExitStatus;
use crate::printer::Printer;

Expand Down Expand Up @@ -45,11 +45,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
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use uv_resolver::{
use uv_types::{BuildIsolation, HashStrategy};

use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger};
use crate::commands::pip::operations::report_target_environment;
use crate::commands::pip::operations::Modifications;
use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{ExitStatus, SharedState};
Expand Down Expand Up @@ -132,11 +133,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
9 changes: 2 additions & 7 deletions crates/uv/src/commands/pip/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ 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;
use uv_python::PythonEnvironment;
use uv_python::PythonRequest;

use crate::commands::pip::operations::report_target_environment;
use crate::commands::ExitStatus;
use crate::printer::Printer;

Expand All @@ -42,11 +41,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
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use uv_python::PythonRequest;
use uv_python::{Prefix, PythonEnvironment, Target};
use uv_requirements::{RequirementsSource, RequirementsSpecification};

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

Expand Down Expand Up @@ -57,11 +58,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 364cfc1

Please sign in to comment.