mirror of https://github.com/astral-sh/uv
Use no reporter by default in `cache clean` (#8868)
This commit is contained in:
parent
738f4245a5
commit
273f453e75
|
|
@ -19,6 +19,7 @@ use uv_pypi_types::ResolutionMetadata;
|
||||||
pub use crate::by_timestamp::CachedByTimestamp;
|
pub use crate::by_timestamp::CachedByTimestamp;
|
||||||
#[cfg(feature = "clap")]
|
#[cfg(feature = "clap")]
|
||||||
pub use crate::cli::CacheArgs;
|
pub use crate::cli::CacheArgs;
|
||||||
|
use crate::removal::Remover;
|
||||||
pub use crate::removal::{rm_rf, Removal};
|
pub use crate::removal::{rm_rf, Removal};
|
||||||
pub use crate::wheel::WheelCache;
|
pub use crate::wheel::WheelCache;
|
||||||
use crate::wheel::WheelCacheKind;
|
use crate::wheel::WheelCacheKind;
|
||||||
|
|
@ -320,8 +321,8 @@ impl Cache {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear the cache, removing all entries.
|
/// Clear the cache, removing all entries.
|
||||||
pub fn clear(&self, reporter: Option<&dyn CleanReporter>) -> Result<Removal, io::Error> {
|
pub fn clear(&self, reporter: Box<dyn CleanReporter>) -> Result<Removal, io::Error> {
|
||||||
rm_rf(&self.root, reporter)
|
Remover::new(reporter).rm_rf(&self.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a package from the cache.
|
/// Remove a package from the cache.
|
||||||
|
|
@ -379,7 +380,7 @@ impl Cache {
|
||||||
let path = fs_err::canonicalize(entry.path())?;
|
let path = fs_err::canonicalize(entry.path())?;
|
||||||
if !after.contains(&path) && before.contains(&path) {
|
if !after.contains(&path) && before.contains(&path) {
|
||||||
debug!("Removing dangling cache entry: {}", path.display());
|
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()) {
|
if CacheBucket::iter().all(|bucket| entry.file_name() != bucket.to_str()) {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
debug!("Removing dangling cache bucket: {}", path.display());
|
debug!("Removing dangling cache bucket: {}", path.display());
|
||||||
summary += rm_rf(path, None)?;
|
summary += rm_rf(path)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If the file is not a marker file, remove it.
|
// If the file is not a marker file, remove it.
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
debug!("Removing dangling cache bucket: {}", path.display());
|
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 entry = entry?;
|
||||||
let path = fs_err::canonicalize(entry.path())?;
|
let path = fs_err::canonicalize(entry.path())?;
|
||||||
debug!("Removing dangling cache environment: {}", path.display());
|
debug!("Removing dangling cache environment: {}", path.display());
|
||||||
summary += rm_rf(path, None)?;
|
summary += rm_rf(path)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
|
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
|
||||||
|
|
@ -444,7 +445,7 @@ impl Cache {
|
||||||
let path = fs_err::canonicalize(entry.path())?;
|
let path = fs_err::canonicalize(entry.path())?;
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
debug!("Removing unzipped wheel entry: {}", path.display());
|
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() {
|
if path.is_dir() {
|
||||||
debug!("Removing unzipped built wheel entry: {}", path.display());
|
debug!("Removing unzipped built wheel entry: {}", path.display());
|
||||||
summary += rm_rf(path, None)?;
|
summary += rm_rf(path)?;
|
||||||
} else if path.is_symlink() {
|
} else if path.is_symlink() {
|
||||||
debug!("Removing unzipped built wheel entry: {}", path.display());
|
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())?;
|
let path = fs_err::canonicalize(entry.path())?;
|
||||||
if !references.contains(&path) {
|
if !references.contains(&path) {
|
||||||
debug!("Removing dangling cache archive: {}", path.display());
|
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 {
|
pub trait CleanReporter: Send + Sync {
|
||||||
/// Called after one file or directory is removed.
|
/// Called after one file or directory is removed.
|
||||||
fn on_clean(&self);
|
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.
|
/// Called after all files and directories are removed.
|
||||||
fn on_complete(&self);
|
fn on_complete(&self);
|
||||||
}
|
}
|
||||||
|
|
@ -809,32 +809,32 @@ impl CacheBucket {
|
||||||
Self::Wheels => {
|
Self::Wheels => {
|
||||||
// For `pypi` wheels, we expect a directory per package (indexed by name).
|
// For `pypi` wheels, we expect a directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
|
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`
|
// For alternate indices, we expect a directory for every index (under an `index`
|
||||||
// subdirectory), followed by a directory per package (indexed by name).
|
// subdirectory), followed by a directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
||||||
for directory in directories(root) {
|
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
|
// For direct URLs, we expect a directory for every URL, followed by a
|
||||||
// directory per package (indexed by name).
|
// directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Url);
|
let root = cache.bucket(self).join(WheelCacheKind::Url);
|
||||||
for directory in directories(root) {
|
for directory in directories(root) {
|
||||||
summary += rm_rf(directory.join(name.to_string()), None)?;
|
summary += rm_rf(directory.join(name.to_string()))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self::SourceDistributions => {
|
Self::SourceDistributions => {
|
||||||
// For `pypi` wheels, we expect a directory per package (indexed by name).
|
// For `pypi` wheels, we expect a directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
|
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`
|
// For alternate indices, we expect a directory for every index (under an `index`
|
||||||
// subdirectory), followed by a directory per package (indexed by name).
|
// subdirectory), followed by a directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
||||||
for directory in directories(root) {
|
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
|
// 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);
|
let root = cache.bucket(self).join(WheelCacheKind::Url);
|
||||||
for url in directories(root) {
|
for url in directories(root) {
|
||||||
if directories(&url).any(|version| is_match(&version, name)) {
|
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);
|
let root = cache.bucket(self).join(WheelCacheKind::Path);
|
||||||
for path in directories(root) {
|
for path in directories(root) {
|
||||||
if directories(&path).any(|version| is_match(&version, name)) {
|
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 repository in directories(root) {
|
||||||
for sha in directories(repository) {
|
for sha in directories(repository) {
|
||||||
if is_match(&sha, name) {
|
if is_match(&sha, name) {
|
||||||
summary += rm_rf(sha, None)?;
|
summary += rm_rf(sha)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -872,20 +872,20 @@ impl CacheBucket {
|
||||||
Self::Simple => {
|
Self::Simple => {
|
||||||
// For `pypi` wheels, we expect a rkyv file per package, indexed by name.
|
// For `pypi` wheels, we expect a rkyv file per package, indexed by name.
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Pypi);
|
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`
|
// For alternate indices, we expect a directory for every index (under an `index`
|
||||||
// subdirectory), followed by a directory per package (indexed by name).
|
// subdirectory), followed by a directory per package (indexed by name).
|
||||||
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
let root = cache.bucket(self).join(WheelCacheKind::Index);
|
||||||
for directory in directories(root) {
|
for directory in directories(root) {
|
||||||
summary += rm_rf(directory.join(format!("{name}.rkyv")), None)?;
|
summary += rm_rf(directory.join(format!("{name}.rkyv")))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self::FlatIndex => {
|
Self::FlatIndex => {
|
||||||
// We can't know if the flat index includes a package, so we just remove the entire
|
// We can't know if the flat index includes a package, so we just remove the entire
|
||||||
// cache entry.
|
// cache entry.
|
||||||
let root = cache.bucket(self);
|
let root = cache.bucket(self);
|
||||||
summary += rm_rf(root, None)?;
|
summary += rm_rf(root)?;
|
||||||
}
|
}
|
||||||
Self::Git => {
|
Self::Git => {
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,34 @@ use crate::CleanReporter;
|
||||||
|
|
||||||
/// Remove a file or directory and all its contents, returning a [`Removal`] with
|
/// 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.
|
/// 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> {
|
pub fn rm_rf(path: impl AsRef<Path>) -> io::Result<Removal> {
|
||||||
let mut removal = Removal::default();
|
Remover::default().rm_rf(path)
|
||||||
removal.rm_rf(path.as_ref(), reporter)?;
|
|
||||||
Ok(removal)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Removal {
|
pub struct Removal {
|
||||||
/// The number of files removed.
|
/// The number of files removed.
|
||||||
|
|
|
||||||
|
|
@ -1995,8 +1995,8 @@ pub fn prune(cache: &Cache) -> Result<Removal, Error> {
|
||||||
"Removing dangling source revision: {}",
|
"Removing dangling source revision: {}",
|
||||||
sibling.path().display()
|
sibling.path().display()
|
||||||
);
|
);
|
||||||
removal += uv_cache::rm_rf(sibling.path(), None)
|
removal +=
|
||||||
.map_err(Error::CacheWrite)?;
|
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: {}",
|
"Removing dangling source revision: {}",
|
||||||
sibling.path().display()
|
sibling.path().display()
|
||||||
);
|
);
|
||||||
removal += uv_cache::rm_rf(sibling.path(), None)
|
removal +=
|
||||||
.map_err(Error::CacheWrite)?;
|
uv_cache::rm_rf(sibling.path()).map_err(Error::CacheWrite)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::fmt::Write;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
|
||||||
use uv_cache::{Cache, CleanReporter, Removal};
|
use uv_cache::{Cache, Removal};
|
||||||
use uv_fs::Simplified;
|
use uv_fs::Simplified;
|
||||||
use uv_normalize::PackageName;
|
use uv_normalize::PackageName;
|
||||||
|
|
||||||
|
|
@ -37,7 +37,7 @@ pub(crate) fn cache_clean(
|
||||||
let reporter = CleaningDirectoryReporter::new(printer, num_paths);
|
let reporter = CleaningDirectoryReporter::new(printer, num_paths);
|
||||||
|
|
||||||
cache
|
cache
|
||||||
.clear(Some(&reporter))
|
.clear(Box::new(reporter))
|
||||||
.with_context(|| format!("Failed to clear cache at: {}", cache.root().user_display()))?
|
.with_context(|| format!("Failed to clear cache at: {}", cache.root().user_display()))?
|
||||||
} else {
|
} else {
|
||||||
let reporter = CleaningPackageReporter::new(printer, packages.len());
|
let reporter = CleaningPackageReporter::new(printer, packages.len());
|
||||||
|
|
@ -46,8 +46,7 @@ pub(crate) fn cache_clean(
|
||||||
for package in packages {
|
for package in packages {
|
||||||
let removed = cache.remove(package)?;
|
let removed = cache.remove(package)?;
|
||||||
summary += removed;
|
summary += removed;
|
||||||
|
reporter.on_clean(package.as_str(), &summary);
|
||||||
reporter.on_clean_package(package.as_str(), &summary);
|
|
||||||
}
|
}
|
||||||
reporter.on_complete();
|
reporter.on_complete();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -557,6 +557,7 @@ impl uv_cache::CleanReporter for CleaningDirectoryReporter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub(crate) struct CleaningPackageReporter {
|
pub(crate) struct CleaningPackageReporter {
|
||||||
bar: ProgressBar,
|
bar: ProgressBar,
|
||||||
}
|
}
|
||||||
|
|
@ -573,12 +574,8 @@ impl CleaningPackageReporter {
|
||||||
bar.set_prefix(format!("{}", "Cleaning".bold().cyan()));
|
bar.set_prefix(format!("{}", "Cleaning".bold().cyan()));
|
||||||
Self { bar }
|
Self { bar }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl uv_cache::CleanReporter for CleaningPackageReporter {
|
pub(crate) fn on_clean(&self, package: &str, removal: &Removal) {
|
||||||
fn on_clean(&self) {}
|
|
||||||
|
|
||||||
fn on_clean_package(&self, package: &str, removal: &Removal) {
|
|
||||||
self.bar.inc(1);
|
self.bar.inc(1);
|
||||||
self.bar.set_message(format!(
|
self.bar.set_message(format!(
|
||||||
": {}, {} files {} folders removed",
|
": {}, {} 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();
|
self.bar.finish_and_clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue