Skip to content

Commit

Permalink
Add .stdout() and .stderr() outputs to Printer (#2227)
Browse files Browse the repository at this point in the history
## Summary

This adds a `.stdout()` stream to `Printer`, so that it automatically
respects `--quiet`.

Motivated by
https://github.com/astral-sh/uv/pull/2115/files#r1513753101.
  • Loading branch information
charliermarsh authored Mar 6, 2024
1 parent 395be44 commit 511e32e
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 126 deletions.
42 changes: 25 additions & 17 deletions crates/uv/src/commands/cache_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use crate::printer::Printer;
pub(crate) fn cache_clean(
packages: &[PackageName],
cache: &Cache,
mut printer: Printer,
printer: Printer,
) -> Result<ExitStatus> {
if !cache.root().exists() {
writeln!(
printer,
printer.stderr(),
"No cache found at: {}",
cache.root().simplified_display().cyan()
)?;
Expand All @@ -27,7 +27,7 @@ pub(crate) fn cache_clean(

if packages.is_empty() {
writeln!(
printer,
printer.stderr(),
"Clearing cache at: {}",
cache.root().simplified_display().cyan()
)?;
Expand All @@ -42,19 +42,19 @@ pub(crate) fn cache_clean(
// Write a summary of the number of files and directories removed.
match (summary.num_files, summary.num_dirs) {
(0, 0) => {
write!(printer, "No cache entries found")?;
write!(printer.stderr(), "No cache entries found")?;
}
(0, 1) => {
write!(printer, "Removed 1 directory")?;
write!(printer.stderr(), "Removed 1 directory")?;
}
(0, num_dirs_removed) => {
write!(printer, "Removed {num_dirs_removed} directories")?;
write!(printer.stderr(), "Removed {num_dirs_removed} directories")?;
}
(1, _) => {
write!(printer, "Removed 1 file")?;
write!(printer.stderr(), "Removed 1 file")?;
}
(num_files_removed, _) => {
write!(printer, "Removed {num_files_removed} files")?;
write!(printer.stderr(), "Removed {num_files_removed} files")?;
}
}

Expand All @@ -66,35 +66,43 @@ pub(crate) fn cache_clean(
let (bytes, unit) = human_readable_bytes(summary.total_bytes);
format!("{bytes:.1}{unit}")
};
write!(printer, " ({})", bytes.green())?;
write!(printer.stderr(), " ({})", bytes.green())?;
}

writeln!(printer)?;
writeln!(printer.stderr())?;
} else {
for package in packages {
let summary = cache.remove(package)?;

// Write a summary of the number of files and directories removed.
match (summary.num_files, summary.num_dirs) {
(0, 0) => {
write!(printer, "No cache entries found for {}", package.cyan())?;
write!(
printer.stderr(),
"No cache entries found for {}",
package.cyan()
)?;
}
(0, 1) => {
write!(printer, "Removed 1 directory for {}", package.cyan())?;
write!(
printer.stderr(),
"Removed 1 directory for {}",
package.cyan()
)?;
}
(0, num_dirs_removed) => {
write!(
printer,
printer.stderr(),
"Removed {num_dirs_removed} directories for {}",
package.cyan()
)?;
}
(1, _) => {
write!(printer, "Removed 1 file for {}", package.cyan())?;
write!(printer.stderr(), "Removed 1 file for {}", package.cyan())?;
}
(num_files_removed, _) => {
write!(
printer,
printer.stderr(),
"Removed {num_files_removed} files for {}",
package.cyan()
)?;
Expand All @@ -109,10 +117,10 @@ pub(crate) fn cache_clean(
let (bytes, unit) = human_readable_bytes(summary.total_bytes);
format!("{bytes:.1}{unit}")
};
write!(printer, " ({})", bytes.green())?;
write!(printer.stderr(), " ({})", bytes.green())?;
}

writeln!(printer)?;
writeln!(printer.stderr())?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub(crate) enum ListFormat {
pub(super) async fn compile_bytecode(
venv: &PythonEnvironment,
cache: &Cache,
mut printer: Printer,
printer: Printer,
) -> anyhow::Result<()> {
let start = std::time::Instant::now();
let files = compile_tree(venv.site_packages(), venv.python_executable(), cache.root())
Expand All @@ -127,7 +127,7 @@ pub(super) async fn compile_bytecode(
})?;
let s = if files == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Bytecode compiled {} in {}",
Expand Down
8 changes: 4 additions & 4 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) async fn pip_compile(
annotation_style: AnnotationStyle,
quiet: bool,
cache: Cache,
mut printer: Printer,
printer: Printer,
) -> Result<ExitStatus> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -271,7 +271,7 @@ pub(crate) async fn pip_compile(

let s = if editables.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Built {} in {}",
Expand Down Expand Up @@ -326,7 +326,7 @@ pub(crate) async fn pip_compile(

let s = if resolution.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Resolved {} in {}",
Expand All @@ -339,7 +339,7 @@ pub(crate) async fn pip_compile(
// Notify the user of any diagnostics.
for diagnostic in resolution.diagnostics() {
writeln!(
printer,
printer.stderr(),
"{}{} {}",
"warning".yellow().bold(),
":".bold(),
Expand Down
11 changes: 5 additions & 6 deletions crates/uv/src/commands/pip_freeze.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::Write;

use anstream::println;
use anyhow::Result;
use itertools::Itertools;
use owo_colors::OwoColorize;
Expand All @@ -22,7 +21,7 @@ pub(crate) fn pip_freeze(
python: Option<&str>,
system: bool,
cache: &Cache,
mut printer: Printer,
printer: Printer,
) -> Result<ExitStatus> {
// Detect the current Python interpreter.
let platform = Platform::current()?;
Expand Down Expand Up @@ -54,13 +53,13 @@ pub(crate) fn pip_freeze(
{
match dist {
InstalledDist::Registry(dist) => {
println!("{}=={}", dist.name().bold(), dist.version);
writeln!(printer.stdout(), "{}=={}", dist.name().bold(), dist.version)?;
}
InstalledDist::Url(dist) => {
if dist.editable {
println!("-e {}", dist.url);
writeln!(printer.stdout(), "-e {}", dist.url)?;
} else {
println!("{} @ {}", dist.name().bold(), dist.url);
writeln!(printer.stdout(), "{} @ {}", dist.name().bold(), dist.url)?;
}
}
}
Expand All @@ -70,7 +69,7 @@ pub(crate) fn pip_freeze(
if strict {
for diagnostic in site_packages.diagnostics()? {
writeln!(
printer,
printer.stderr(),
"{}{} {}",
"warning".yellow().bold(),
":".bold(),
Expand Down
32 changes: 16 additions & 16 deletions crates/uv/src/commands/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) async fn pip_install(
python: Option<String>,
system: bool,
cache: Cache,
mut printer: Printer,
printer: Printer,
) -> Result<ExitStatus> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -152,7 +152,7 @@ pub(crate) async fn pip_install(
let num_requirements = requirements.len() + editables.len();
let s = if num_requirements == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Audited {} in {}",
Expand Down Expand Up @@ -366,7 +366,7 @@ async fn build_editables(
tags: &Tags,
client: &RegistryClient,
build_dispatch: &BuildDispatch<'_>,
mut printer: Printer,
printer: Printer,
) -> Result<Vec<BuiltEditable>, Error> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -409,7 +409,7 @@ async fn build_editables(

let s = if editables.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Built {} in {}",
Expand Down Expand Up @@ -441,7 +441,7 @@ async fn resolve(
index: &InMemoryIndex,
build_dispatch: &BuildDispatch<'_>,
options: Options,
mut printer: Printer,
printer: Printer,
) -> Result<ResolutionGraph, Error> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -504,7 +504,7 @@ async fn resolve(

let s = if resolution.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Resolved {} in {}",
Expand Down Expand Up @@ -534,7 +534,7 @@ async fn install(
build_dispatch: &BuildDispatch<'_>,
cache: &Cache,
venv: &PythonEnvironment,
mut printer: Printer,
printer: Printer,
) -> Result<(), Error> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -570,7 +570,7 @@ async fn install(
if remote.is_empty() && local.is_empty() && reinstalls.is_empty() {
let s = if resolution.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Audited {} in {}",
Expand Down Expand Up @@ -610,7 +610,7 @@ async fn install(

let s = if wheels.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Downloaded {} in {}",
Expand Down Expand Up @@ -649,7 +649,7 @@ async fn install(

let s = if wheels.len() == 1 { "" } else { "s" };
writeln!(
printer,
printer.stderr(),
"{}",
format!(
"Installed {} in {}",
Expand Down Expand Up @@ -685,7 +685,7 @@ async fn install(
match event.kind {
ChangeEventKind::Added => {
writeln!(
printer,
printer.stderr(),
" {} {}{}",
"+".green(),
event.dist.name().as_ref().bold(),
Expand All @@ -694,7 +694,7 @@ async fn install(
}
ChangeEventKind::Removed => {
writeln!(
printer,
printer.stderr(),
" {} {}{}",
"-".red(),
event.dist.name().as_ref().bold(),
Expand All @@ -714,15 +714,15 @@ async fn install(
None | Some(Yanked::Bool(false)) => {}
Some(Yanked::Bool(true)) => {
writeln!(
printer,
printer.stderr(),
"{}{} {dist} is yanked.",
"warning".yellow().bold(),
":".bold(),
)?;
}
Some(Yanked::Reason(reason)) => {
writeln!(
printer,
printer.stderr(),
"{}{} {dist} is yanked (reason: \"{reason}\").",
"warning".yellow().bold(),
":".bold(),
Expand All @@ -738,7 +738,7 @@ async fn install(
fn validate(
resolution: &Resolution,
venv: &PythonEnvironment,
mut printer: Printer,
printer: Printer,
) -> Result<(), Error> {
let site_packages = SitePackages::from_executable(venv)?;
let diagnostics = site_packages.diagnostics()?;
Expand All @@ -749,7 +749,7 @@ fn validate(
.any(|package| diagnostic.includes(package))
{
writeln!(
printer,
printer.stderr(),
"{}{} {}",
"warning".yellow().bold(),
":".bold(),
Expand Down
Loading

0 comments on commit 511e32e

Please sign in to comment.