Use no reporter by default in cache clean (#8868)
Some checks are pending
CI / integration test | uv publish (push) Blocked by required conditions
CI / check cache | ubuntu (push) Blocked by required conditions
CI / check cache | macos aarch64 (push) Blocked by required conditions
CI / check system | python on debian (push) Blocked by required conditions
CI / check system | python on fedora (push) Blocked by required conditions
CI / check system | python on ubuntu (push) Blocked by required conditions
CI / check system | python on opensuse (push) Blocked by required conditions
CI / check system | python on rocky linux 8 (push) Blocked by required conditions
CI / check system | python on rocky linux 9 (push) Blocked by required conditions
CI / check system | pypy on ubuntu (push) Blocked by required conditions
CI / check system | pyston (push) Blocked by required conditions
CI / check system | alpine (push) Blocked by required conditions
CI / check system | python on macos aarch64 (push) Blocked by required conditions
CI / check system | homebrew python on macos aarch64 (push) Blocked by required conditions
CI / check system | python on macos x86_64 (push) Blocked by required conditions
CI / check system | python3.10 on windows (push) Blocked by required conditions
CI / check system | python3.10 on windows x86 (push) Blocked by required conditions
CI / check system | python3.13 on windows (push) Blocked by required conditions
CI / check system | python3.12 via chocolatey (push) Blocked by required conditions
CI / check system | python3.9 via pyenv (push) Blocked by required conditions
CI / check system | python3.13 (push) Blocked by required conditions
CI / check system | conda3.11 on linux (push) Blocked by required conditions
CI / check system | conda3.8 on linux (push) Blocked by required conditions
CI / check system | conda3.11 on macos (push) Blocked by required conditions
CI / check system | conda3.8 on macos (push) Blocked by required conditions
CI / check system | conda3.11 on windows (push) Blocked by required conditions
CI / check system | conda3.8 on windows (push) Blocked by required conditions
CI / check system | amazonlinux (push) Blocked by required conditions
CI / check system | embedded python3.10 on windows (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / lint (push) Waiting to run
CI / cargo clippy | ubuntu (push) Blocked by required conditions
CI / cargo clippy | windows (push) Blocked by required conditions
CI / cargo dev generate-all (push) Blocked by required conditions
CI / cargo shear (push) Waiting to run
CI / cargo test | ubuntu (push) Blocked by required conditions
CI / cargo test | macos (push) Blocked by required conditions
CI / cargo test | windows (push) Blocked by required conditions
CI / check windows trampoline | aarch64 (push) Blocked by required conditions
CI / check windows trampoline | i686 (push) Blocked by required conditions
CI / check windows trampoline | x86_64 (push) Blocked by required conditions
CI / test windows trampoline | i686 (push) Blocked by required conditions
CI / test windows trampoline | x86_64 (push) Blocked by required conditions
CI / typos (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / build binary | linux (push) Blocked by required conditions
CI / build binary | macos aarch64 (push) Blocked by required conditions
CI / build binary | macos x86_64 (push) Blocked by required conditions
CI / build binary | windows (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / build binary | freebsd (push) Blocked by required conditions
CI / ecosystem test | prefecthq/prefect (push) Blocked by required conditions
CI / ecosystem test | pallets/flask (push) Blocked by required conditions
CI / integration test | conda on ubuntu (push) Blocked by required conditions
CI / integration test | free-threaded on linux (push) Blocked by required conditions
CI / integration test | free-threaded on windows (push) Blocked by required conditions
CI / integration test | pypy on ubuntu (push) Blocked by required conditions
CI / integration test | pypy on windows (push) Blocked by required conditions
CI / integration test | graalpy on ubuntu (push) Blocked by required conditions
CI / integration test | graalpy on windows (push) Blocked by required conditions
CI / integration test | github actions (push) Blocked by required conditions
CI / integration test | determine publish changes (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
Charlie Marsh 2024-11-06 12:07:10 -05:00 committed by GitHub
parent 738f4245a5
commit 273f453e75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 41 deletions

View file

@ -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;
@ -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.
@ -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)?;
}
}
}
@ -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)?;
}
}
@ -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 => (),
@ -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)?;
}
}
}
@ -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)?;
}
}
}
@ -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)?;
}
}
}
@ -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);
}
@ -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
@ -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)?;
}
}
@ -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)?;
}
}
@ -864,7 +864,7 @@ 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)?;
}
}
}
@ -872,20 +872,20 @@ impl CacheBucket {
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.

View file

@ -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.

View file

@ -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)?;
}
}
}
@ -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)?;
}
}
}

View file

@ -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;
@ -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());
@ -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();

View file

@ -557,6 +557,7 @@ impl uv_cache::CleanReporter for CleaningDirectoryReporter {
}
}
#[derive(Debug)]
pub(crate) struct CleaningPackageReporter {
bar: ProgressBar,
}
@ -573,12 +574,8 @@ 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",
@ -586,7 +583,7 @@ impl uv_cache::CleanReporter for CleaningPackageReporter {
));
}
fn on_complete(&self) {
pub(crate) fn on_complete(&self) {
self.bar.finish_and_clear();
}
}