pathlib refactor compact_cmd

This commit is contained in:
Thomas Waldmann 2025-06-10 21:16:23 +02:00
parent 4866d49e77
commit aea3d6aefe
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
3 changed files with 10 additions and 9 deletions

View file

@ -1,5 +1,5 @@
import argparse
import os
from pathlib import Path
from ._common import with_repository
from ..archive import Archive
@ -83,8 +83,8 @@ class ArchiveGarbageCollector:
"""
logger.info("Cleaning up files cache...")
cache_dir = os.path.join(get_cache_dir(), self.repository.id_str)
if not os.path.exists(cache_dir):
cache_dir = Path(get_cache_dir()) / self.repository.id_str
if not cache_dir.exists():
logger.debug("Cache directory does not exist, skipping files cache cleanup")
return
@ -104,9 +104,9 @@ class ArchiveGarbageCollector:
unused_files_cache_names = files_cache_names - used_files_cache_names
for cache_filename in unused_files_cache_names:
cache_path = os.path.join(cache_dir, cache_filename)
cache_path = cache_dir / cache_filename
try:
os.unlink(cache_path)
cache_path.unlink()
except (FileNotFoundError, PermissionError) as e:
logger.warning(f"Could not access cache file: {e}")
logger.info(f"Removed {len(unused_files_cache_names)} unused files cache files.")

View file

@ -64,7 +64,7 @@ def discover_files_cache_names(path, files_cache_name="files"):
:param files_cache_name: base name of the files cache files
:return: list of files cache file names
"""
return [p.name for p in Path(path).iterdir() if p.name.startswith(files_cache_name + ".")]
return [p.name for p in path.iterdir() if p.name.startswith(files_cache_name + ".")]
# chunks is a list of ChunkListEntry

View file

@ -1,4 +1,5 @@
import os
from pathlib import Path
import pytest
from ...constants import * # NOQA
@ -104,8 +105,8 @@ def test_compact_files_cache_cleanup(archivers, request):
pytest.fail("Could not find repository ID in info output")
# Check cache directory for files cache files
cache_dir = os.path.join(get_cache_dir(), repo_id)
if not os.path.exists(cache_dir):
cache_dir = Path(get_cache_dir()) / repo_id
if not cache_dir.exists():
pytest.skip("Cache directory does not exist, skipping test")
# Get initial files cache files