2
0
Эх сурвалжийг харах

pathlib refactor compact_cmd

Thomas Waldmann 1 долоо хоног өмнө
parent
commit
aea3d6aefe

+ 5 - 5
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.")

+ 1 - 1
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

+ 4 - 3
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