From aea3d6aefe4c2549c01e2ca7fc938222f1091bfe Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 10 Jun 2025 21:16:23 +0200 Subject: [PATCH] pathlib refactor compact_cmd --- src/borg/archiver/compact_cmd.py | 10 +++++----- src/borg/cache.py | 2 +- src/borg/testsuite/archiver/compact_cmd_test.py | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/borg/archiver/compact_cmd.py b/src/borg/archiver/compact_cmd.py index 2d0290259..ec7d8061b 100644 --- a/src/borg/archiver/compact_cmd.py +++ b/src/borg/archiver/compact_cmd.py @@ -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.") diff --git a/src/borg/cache.py b/src/borg/cache.py index 055259ecf..904a7e881 100644 --- a/src/borg/cache.py +++ b/src/borg/cache.py @@ -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 diff --git a/src/borg/testsuite/archiver/compact_cmd_test.py b/src/borg/testsuite/archiver/compact_cmd_test.py index 802645b4c..4982dedfe 100644 --- a/src/borg/testsuite/archiver/compact_cmd_test.py +++ b/src/borg/testsuite/archiver/compact_cmd_test.py @@ -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