Skip to content

Commit

Permalink
Use no reporter by default in cache clean (#8868)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Nov 6, 2024
1 parent 738f424 commit 273f453
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
46 changes: 23 additions & 23 deletions crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use uv_pypi_types::ResolutionMetadata;
pub use crate::by_timestamp::CachedByTimestamp;
#[cfg(feature = "clap")]
pub use crate::cli::CacheArgs;
use crate::removal::Remover;
pub use crate::removal::{rm_rf, Removal};
pub use crate::wheel::WheelCache;
use crate::wheel::WheelCacheKind;
Expand Down Expand Up @@ -320,8 +321,8 @@ impl Cache {
}

/// Clear the cache, removing all entries.
pub fn clear(&self, reporter: Option<&dyn CleanReporter>) -> Result<Removal, io::Error> {
rm_rf(&self.root, reporter)
pub fn clear(&self, reporter: Box<dyn CleanReporter>) -> Result<Removal, io::Error> {
Remover::new(reporter).rm_rf(&self.root)
}

/// Remove a package from the cache.
Expand Down Expand Up @@ -379,7 +380,7 @@ impl Cache {
let path = fs_err::canonicalize(entry.path())?;
if !after.contains(&path) && before.contains(&path) {
debug!("Removing dangling cache entry: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}
}
Expand Down Expand Up @@ -409,13 +410,13 @@ impl Cache {
if CacheBucket::iter().all(|bucket| entry.file_name() != bucket.to_str()) {
let path = entry.path();
debug!("Removing dangling cache bucket: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
} else {
// If the file is not a marker file, remove it.
let path = entry.path();
debug!("Removing dangling cache bucket: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}

Expand All @@ -427,7 +428,7 @@ impl Cache {
let entry = entry?;
let path = fs_err::canonicalize(entry.path())?;
debug!("Removing dangling cache environment: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Expand All @@ -444,7 +445,7 @@ impl Cache {
let path = fs_err::canonicalize(entry.path())?;
if path.is_dir() {
debug!("Removing unzipped wheel entry: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}
}
Expand Down Expand Up @@ -472,10 +473,10 @@ impl Cache {

if path.is_dir() {
debug!("Removing unzipped built wheel entry: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
} else if path.is_symlink() {
debug!("Removing unzipped built wheel entry: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}
}
Expand Down Expand Up @@ -505,7 +506,7 @@ impl Cache {
let path = fs_err::canonicalize(entry.path())?;
if !references.contains(&path) {
debug!("Removing dangling cache archive: {}", path.display());
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}
}
Expand All @@ -520,8 +521,7 @@ impl Cache {
pub trait CleanReporter: Send + Sync {
/// Called after one file or directory is removed.
fn on_clean(&self);
/// Called after a package is cleaned.
fn on_clean_package(&self, _package: &str, _removal: &Removal) {}

/// Called after all files and directories are removed.
fn on_complete(&self);
}
Expand Down Expand Up @@ -809,32 +809,32 @@ impl CacheBucket {
Self::Wheels => {
// For `pypi` wheels, we expect a directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
summary += rm_rf(root.join(name.to_string()), None)?;
summary += rm_rf(root.join(name.to_string()))?;

// For alternate indices, we expect a directory for every index (under an `index`
// subdirectory), followed by a directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Index);
for directory in directories(root) {
summary += rm_rf(directory.join(name.to_string()), None)?;
summary += rm_rf(directory.join(name.to_string()))?;
}

// For direct URLs, we expect a directory for every URL, followed by a
// directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Url);
for directory in directories(root) {
summary += rm_rf(directory.join(name.to_string()), None)?;
summary += rm_rf(directory.join(name.to_string()))?;
}
}
Self::SourceDistributions => {
// For `pypi` wheels, we expect a directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
summary += rm_rf(root.join(name.to_string()), None)?;
summary += rm_rf(root.join(name.to_string()))?;

// For alternate indices, we expect a directory for every index (under an `index`
// subdirectory), followed by a directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Index);
for directory in directories(root) {
summary += rm_rf(directory.join(name.to_string()), None)?;
summary += rm_rf(directory.join(name.to_string()))?;
}

// For direct URLs, we expect a directory for every URL, followed by a
Expand All @@ -843,7 +843,7 @@ impl CacheBucket {
let root = cache.bucket(self).join(WheelCacheKind::Url);
for url in directories(root) {
if directories(&url).any(|version| is_match(&version, name)) {
summary += rm_rf(url, None)?;
summary += rm_rf(url)?;
}
}

Expand All @@ -853,7 +853,7 @@ impl CacheBucket {
let root = cache.bucket(self).join(WheelCacheKind::Path);
for path in directories(root) {
if directories(&path).any(|version| is_match(&version, name)) {
summary += rm_rf(path, None)?;
summary += rm_rf(path)?;
}
}

Expand All @@ -864,28 +864,28 @@ impl CacheBucket {
for repository in directories(root) {
for sha in directories(repository) {
if is_match(&sha, name) {
summary += rm_rf(sha, None)?;
summary += rm_rf(sha)?;
}
}
}
}
Self::Simple => {
// For `pypi` wheels, we expect a rkyv file per package, indexed by name.
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
summary += rm_rf(root.join(format!("{name}.rkyv")), None)?;
summary += rm_rf(root.join(format!("{name}.rkyv")))?;

// For alternate indices, we expect a directory for every index (under an `index`
// subdirectory), followed by a directory per package (indexed by name).
let root = cache.bucket(self).join(WheelCacheKind::Index);
for directory in directories(root) {
summary += rm_rf(directory.join(format!("{name}.rkyv")), None)?;
summary += rm_rf(directory.join(format!("{name}.rkyv")))?;
}
}
Self::FlatIndex => {
// We can't know if the flat index includes a package, so we just remove the entire
// cache entry.
let root = cache.bucket(self);
summary += rm_rf(root, None)?;
summary += rm_rf(root)?;
}
Self::Git => {
// Nothing to do.
Expand Down
30 changes: 26 additions & 4 deletions crates/uv-cache/src/removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ use crate::CleanReporter;

/// Remove a file or directory and all its contents, returning a [`Removal`] with
/// the number of files and directories removed, along with a total byte count.
pub fn rm_rf(path: impl AsRef<Path>, reporter: Option<&dyn CleanReporter>) -> io::Result<Removal> {
let mut removal = Removal::default();
removal.rm_rf(path.as_ref(), reporter)?;
Ok(removal)
pub fn rm_rf(path: impl AsRef<Path>) -> io::Result<Removal> {
Remover::default().rm_rf(path)
}

/// A builder for a [`Remover`] that can remove files and directories.
#[derive(Default)]
pub(crate) struct Remover {
reporter: Option<Box<dyn CleanReporter>>,
}

impl Remover {
/// Create a new [`Remover`] with the given reporter.
pub(crate) fn new(reporter: Box<dyn CleanReporter>) -> Self {
Self {
reporter: Some(reporter),
}
}

/// Remove a file or directory and all its contents, returning a [`Removal`] with
/// the number of files and directories removed, along with a total byte count.
pub(crate) fn rm_rf(&self, path: impl AsRef<Path>) -> io::Result<Removal> {
let mut removal = Removal::default();
removal.rm_rf(path.as_ref(), self.reporter.as_deref())?;
Ok(removal)
}
}

/// A removal operation with statistics on the number of files and directories removed.
#[derive(Debug, Default)]
pub struct Removal {
/// The number of files removed.
Expand Down
8 changes: 4 additions & 4 deletions crates/uv-distribution/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1995,8 +1995,8 @@ pub fn prune(cache: &Cache) -> Result<Removal, Error> {
"Removing dangling source revision: {}",
sibling.path().display()
);
removal += uv_cache::rm_rf(sibling.path(), None)
.map_err(Error::CacheWrite)?;
removal +=
uv_cache::rm_rf(sibling.path()).map_err(Error::CacheWrite)?;
}
}
}
Expand All @@ -2020,8 +2020,8 @@ pub fn prune(cache: &Cache) -> Result<Removal, Error> {
"Removing dangling source revision: {}",
sibling.path().display()
);
removal += uv_cache::rm_rf(sibling.path(), None)
.map_err(Error::CacheWrite)?;
removal +=
uv_cache::rm_rf(sibling.path()).map_err(Error::CacheWrite)?;
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/uv/src/commands/cache_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Write;
use anyhow::{Context, Result};
use owo_colors::OwoColorize;

use uv_cache::{Cache, CleanReporter, Removal};
use uv_cache::{Cache, Removal};
use uv_fs::Simplified;
use uv_normalize::PackageName;

Expand Down Expand Up @@ -37,7 +37,7 @@ pub(crate) fn cache_clean(
let reporter = CleaningDirectoryReporter::new(printer, num_paths);

cache
.clear(Some(&reporter))
.clear(Box::new(reporter))
.with_context(|| format!("Failed to clear cache at: {}", cache.root().user_display()))?
} else {
let reporter = CleaningPackageReporter::new(printer, packages.len());
Expand All @@ -46,8 +46,7 @@ pub(crate) fn cache_clean(
for package in packages {
let removed = cache.remove(package)?;
summary += removed;

reporter.on_clean_package(package.as_str(), &summary);
reporter.on_clean(package.as_str(), &summary);
}
reporter.on_complete();

Expand Down
9 changes: 3 additions & 6 deletions crates/uv/src/commands/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ impl uv_cache::CleanReporter for CleaningDirectoryReporter {
}
}

#[derive(Debug)]
pub(crate) struct CleaningPackageReporter {
bar: ProgressBar,
}
Expand All @@ -573,20 +574,16 @@ impl CleaningPackageReporter {
bar.set_prefix(format!("{}", "Cleaning".bold().cyan()));
Self { bar }
}
}

impl uv_cache::CleanReporter for CleaningPackageReporter {
fn on_clean(&self) {}

fn on_clean_package(&self, package: &str, removal: &Removal) {
pub(crate) fn on_clean(&self, package: &str, removal: &Removal) {
self.bar.inc(1);
self.bar.set_message(format!(
": {}, {} files {} folders removed",
package, removal.num_files, removal.num_dirs,
));
}

fn on_complete(&self) {
pub(crate) fn on_complete(&self) {
self.bar.finish_and_clear();
}
}
Expand Down

0 comments on commit 273f453

Please sign in to comment.