Remove prefix

This commit is contained in:
Charlie Marsh 2025-11-22 09:04:39 -06:00
parent 936b1cff03
commit d111e5ccf3
4 changed files with 26 additions and 34 deletions

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use uv_pypi_types::{HashAlgorithm, HashDigest}; use uv_pypi_types::{HashAlgorithm, HashDigest};
@ -35,20 +35,9 @@ impl FromStr for ArchiveVersion {
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ArchiveId(SmallString); pub struct ArchiveId(SmallString);
impl ArchiveId { impl AsRef<Path> for ArchiveId {
/// Return the content-addressed path for the [`ArchiveId`]. fn as_ref(&self) -> &Path {
pub fn to_path_buf(&self) -> PathBuf { self.0.as_ref().as_ref()
if self.0.len() == 21 {
// A 21-digit NanoID.
PathBuf::from(self.0.as_ref())
} else {
// A SHA256 hex digest, split into three segments.
let mut path = PathBuf::new();
path.push(&self.0[0..2]);
path.push(&self.0[2..4]);
path.push(&self.0[4..]);
path
}
} }
} }

View File

@ -263,7 +263,7 @@ impl Cache {
/// Return the path to an archive in the cache. /// Return the path to an archive in the cache.
pub fn archive(&self, id: &ArchiveId) -> PathBuf { pub fn archive(&self, id: &ArchiveId) -> PathBuf {
self.bucket(CacheBucket::Archive).join(id.to_path_buf()) self.bucket(CacheBucket::Archive).join(id)
} }
/// Create a temporary directory to be used as a Python virtual environment. /// Create a temporary directory to be used as a Python virtual environment.
@ -352,7 +352,7 @@ impl Cache {
) -> io::Result<ArchiveId> { ) -> io::Result<ArchiveId> {
// Move the temporary directory into the directory store. // Move the temporary directory into the directory store.
let id = ArchiveId::from(hash); let id = ArchiveId::from(hash);
let archive_entry = self.bucket(CacheBucket::Archive).join(id.to_path_buf()); let archive_entry = self.bucket(CacheBucket::Archive).join(id.as_ref());
if let Some(parent) = archive_entry.parent() { if let Some(parent) = archive_entry.parent() {
fs_err::create_dir_all(parent)?; fs_err::create_dir_all(parent)?;
} }
@ -621,34 +621,34 @@ impl Cache {
match fs_err::read_dir(self.bucket(CacheBucket::Archive)) { match fs_err::read_dir(self.bucket(CacheBucket::Archive)) {
Ok(entries) => { Ok(entries) => {
for entry in entries { for entry in entries {
let entry = entry?; let entry = entry?;
// If two hex characters, it's a prefix; recurse. // If two hex characters, it's a prefix; recurse.
if entry if entry.file_name().to_str().is_some_and(|name| {
.file_name() name.len() == 2 && name.chars().all(|c| c.is_ascii_hexdigit())
.to_str() }) {
.is_some_and(|name| name.len() == 2 && name.chars().all(|c| c.is_ascii_hexdigit()))
{
match fs_err::read_dir(entry.path()) { match fs_err::read_dir(entry.path()) {
Ok(subentries) => { Ok(subentries) => {
for subentry in subentries { for subentry in subentries {
let subentry = subentry?; let subentry = subentry?;
// If two hex characters, it's a prefix; recurse. // If two hex characters, it's a prefix; recurse.
if subentry if subentry.file_name().to_str().is_some_and(|name| {
.file_name() name.len() == 2
.to_str() && name.chars().all(|c| c.is_ascii_hexdigit())
.is_some_and(|name| name.len() == 2 && name.chars().all(|c| c.is_ascii_hexdigit())) }) {
{
match fs_err::read_dir(subentry.path()) { match fs_err::read_dir(subentry.path()) {
Ok(subsubentries) => { Ok(subsubentries) => {
for subsubentry in subsubentries { for subsubentry in subsubentries {
let subsubentry = subsubentry?; let subsubentry = subsubentry?;
let path = fs_err::canonicalize(subsubentry.path())?; let path =
fs_err::canonicalize(subsubentry.path())?;
if !references.contains_key(&path) { if !references.contains_key(&path) {
debug!("Removing dangling cache archive: {}", path.display()); debug!(
"Removing dangling cache archive: {}",
path.display()
);
summary += rm_rf(path)?; summary += rm_rf(path)?;
} }
} }
@ -659,7 +659,10 @@ impl Cache {
} else { } else {
let path = fs_err::canonicalize(subentry.path())?; let path = fs_err::canonicalize(subentry.path())?;
if !references.contains_key(&path) { if !references.contains_key(&path) {
debug!("Removing dangling cache archive: {}", path.display()); debug!(
"Removing dangling cache archive: {}",
path.display()
);
summary += rm_rf(path)?; summary += rm_rf(path)?;
} }
} }

View File

@ -845,7 +845,7 @@ impl TestContext {
)); ));
// Filter archive hashes // Filter archive hashes
filters.push(( filters.push((
r"archive-v(\d+)[\\/][0-9a-f]{2}[\\/][0-9a-f]{2}[\\/][0-9a-f]{60}".to_string(), r"archive-v(\d+)[\\/][0-9a-f]{64}".to_string(),
"archive-v$1/[HASH]".to_string(), "archive-v$1/[HASH]".to_string(),
)); ));

View File

@ -53,10 +53,10 @@ Collecting numpy==1.19.5
ERROR: Exception: ERROR: Exception:
Traceback (most recent call last): Traceback (most recent call last):
... ...
File "/Users/example/.cache/uv/archive-v1/3783IbOdglemN3ieOULx2/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_impl.py", line 321, in _call_hook File "/Users/example/.cache/uv/archive-v0/97de8790030bbd5c2d96b7ec782fc2f7820ef8dba6db909ccf95449f2d062d4b/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_impl.py", line 321, in _call_hook
raise BackendUnavailable(data.get('traceback', '')) raise BackendUnavailable(data.get('traceback', ''))
pip._vendor.pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last): pip._vendor.pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last):
File "/Users/example/.cache/uv/archive-v1/3783IbOdglemN3ieOULx2/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend File "/Users/example/.cache/uv/archive-v0/97de8790030bbd5c2d96b7ec782fc2f7820ef8dba6db909ccf95449f2d062d4b/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
obj = import_module(mod_path) obj = import_module(mod_path)
File "/Users/example/.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/importlib/__init__.py", line 88, in import_module File "/Users/example/.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/importlib/__init__.py", line 88, in import_module
return _bootstrap._gcd_import(name[level:], package, level) return _bootstrap._gcd_import(name[level:], package, level)