Skip to content

Commit

Permalink
Display the target virtual environment path if non-default (#7850)
Browse files Browse the repository at this point in the history
Supersedes #4835

Closes #2155

e.g.

```
❯ cargo run -q -- pip install --python ../../example httpx
Using Python 3.12.1 environment at /Users/zb/example
Resolved 7 packages in 561ms
Prepared 4 packages in 62ms
Installed 4 packages in 13ms
 + certifi==2024.8.30
 + h11==0.14.0
 + httpcore==1.0.5
 + httpx==0.27.2

❯ cargo run -q -- pip install httpx
Resolved 7 packages in 17ms
Installed 4 packages in 10ms
 + certifi==2024.8.30
 + h11==0.14.0
 + httpcore==1.0.5
 + httpx==0.27.2
```
  • Loading branch information
zanieb authored Oct 2, 2024
1 parent 2552dc2 commit b001483
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 51 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 uv_cache::Cache;
use uv_distribution_types::{Diagnostic, InstalledDist};
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 uv_cache::Cache;
use uv_distribution_types::{Diagnostic, InstalledDist, Name};
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 @@ -33,6 +33,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::{diagnostics, ExitStatus, SharedState};
Expand Down Expand Up @@ -147,11 +148,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 uv_cache::Cache;
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 uv_cache::Cache;
use uv_client::{BaseClientBuilder, RegistryClient};
Expand Down Expand Up @@ -538,6 +539,48 @@ pub(crate) async fn install(
Ok(changelog)
}

/// Display a message about the target environment for the operation.
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) = std::path::absolute(env.root()) 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) = std::path::absolute(PathBuf::from(".venv")) {
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 uv_cache::Cache;
use uv_distribution_types::{Diagnostic, Name};
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 @@ -29,6 +29,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::{diagnostics, ExitStatus, SharedState};
Expand Down Expand Up @@ -131,11 +132,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
13 changes: 3 additions & 10 deletions crates/uv/src/commands/pip/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ use anyhow::Result;
use indexmap::IndexMap;
use owo_colors::OwoColorize;
use rustc_hash::{FxHashMap, FxHashSet};
use tracing::debug;

use uv_cache::Cache;
use uv_distribution::Metadata;
use uv_distribution_types::{Diagnostic, Name};
use uv_fs::Simplified;
use uv_installer::SitePackages;
use uv_normalize::PackageName;
use uv_pypi_types::{RequirementSource, ResolverMarkerEnvironment};
use uv_python::EnvironmentPreference;
use uv_python::PythonEnvironment;
use uv_python::PythonRequest;
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};

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

Expand All @@ -42,11 +39,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 b001483

Please sign in to comment.