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

move build_filter/build_matcher to archiver.common

Thomas Waldmann 2 жил өмнө
parent
commit
e05f7971da

+ 4 - 26
src/borg/archiver/__init__.py

@@ -25,6 +25,7 @@ try:
     logger = create_logger()
 
     from .common import with_repository, with_archive, Highlander
+    from .common import build_filter, build_matcher
     from .. import __version__
     from .. import helpers
     from ..archive import Archive, ArchiveRecreater, is_special
@@ -160,13 +161,6 @@ class Archiver(
             else:
                 logging.getLogger("borg.output.list").info("%1s %s", status, remove_surrogates(path))
 
-    @staticmethod
-    def build_matcher(inclexcl_patterns, include_paths):
-        matcher = PatternMatcher()
-        matcher.add_inclexcl(inclexcl_patterns)
-        matcher.add_includepaths(include_paths)
-        return matcher
-
     @with_repository(fake="dry_run", exclusive=True, compatibility=(Manifest.Operation.WRITE,))
     def do_create(self, args, repository, manifest=None, key=None):
         """Create new archive"""
@@ -601,22 +595,6 @@ class Archiver(
         if not recurse_excluded_dir:
             self.print_file_status(status, path)
 
-    @staticmethod
-    def build_filter(matcher, strip_components):
-        if strip_components:
-
-            def item_filter(item):
-                matched = matcher.match(item.path) and os.sep.join(item.path.split(os.sep)[strip_components:])
-                return matched
-
-        else:
-
-            def item_filter(item):
-                matched = matcher.match(item.path)
-                return matched
-
-        return item_filter
-
     @with_repository(compatibility=(Manifest.Operation.READ,))
     @with_archive
     def do_extract(self, args, repository, manifest, key, archive):
@@ -631,7 +609,7 @@ class Archiver(
                     "Hint: You likely need to fix your locale setup. E.g. install locales and use: LANG=en_US.UTF-8"
                 )
 
-        matcher = self.build_matcher(args.patterns, args.paths)
+        matcher = build_matcher(args.patterns, args.paths)
 
         progress = args.progress
         output_list = args.output_list
@@ -642,7 +620,7 @@ class Archiver(
         dirs = []
         hlm = HardLinkManager(id_type=bytes, info_type=str)  # hlid -> path
 
-        filter = self.build_filter(matcher, strip_components)
+        filter = build_filter(matcher, strip_components)
         if progress:
             pi = ProgressIndicatorPercent(msg="%5.1f%% Extracting: %s", step=0.1, msgid="extract")
             pi.output("Calculating total archive size for the progress indicator (might take long for large archives)")
@@ -708,7 +686,7 @@ class Archiver(
     @with_repository(cache=True, exclusive=True, compatibility=(Manifest.Operation.CHECK,))
     def do_recreate(self, args, repository, manifest, key, cache):
         """Re-create archives"""
-        matcher = self.build_matcher(args.patterns, args.paths)
+        matcher = build_matcher(args.patterns, args.paths)
         self.output_list = args.output_list
         self.output_filter = args.output_filter
         recompress = args.recompress != "never"

+ 25 - 0
src/borg/archiver/common.py

@@ -1,5 +1,6 @@
 import argparse
 import functools
+import os
 import textwrap
 
 import borg
@@ -9,6 +10,7 @@ from ..cache import Cache, assert_secure
 from ..helpers import Error
 from ..helpers import Manifest, AI_HUMAN_SORT_KEYS
 from ..helpers import GlobSpec, SortBySpec, positive_int_validator
+from ..patterns import PatternMatcher
 from ..remote import RemoteRepository
 from ..repository import Repository
 from ..nanorst import rst_to_terminal
@@ -420,3 +422,26 @@ def define_archive_filters_group(subparser, *, sort_by=True, first_last=True):
         )
 
     return filters_group
+
+
+def build_matcher(inclexcl_patterns, include_paths):
+    matcher = PatternMatcher()
+    matcher.add_inclexcl(inclexcl_patterns)
+    matcher.add_includepaths(include_paths)
+    return matcher
+
+
+def build_filter(matcher, strip_components):
+    if strip_components:
+
+        def item_filter(item):
+            matched = matcher.match(item.path) and os.sep.join(item.path.split(os.sep)[strip_components:])
+            return matched
+
+    else:
+
+        def item_filter(item):
+            matched = matcher.match(item.path)
+            return matched
+
+    return item_filter

+ 2 - 2
src/borg/archiver/diff.py

@@ -1,7 +1,7 @@
 import argparse
 import json
 
-from .common import with_repository, with_archive
+from .common import with_repository, with_archive, build_matcher
 from ..archive import Archive
 from ..constants import *  # NOQA
 from ..helpers import archivename_validator
@@ -40,7 +40,7 @@ class DiffMixIn:
                 "to override this check."
             )
 
-        matcher = self.build_matcher(args.patterns, args.paths)
+        matcher = build_matcher(args.patterns, args.paths)
 
         diffs = Archive.compare_archives_iter(archive1, archive2, matcher, can_compare_chunk_ids=can_compare_chunk_ids)
         # Conversion to string and filtering for diff.equal to save memory if sorting

+ 2 - 2
src/borg/archiver/list_cmd.py

@@ -2,7 +2,7 @@ import argparse
 import textwrap
 import sys
 
-from .common import with_repository
+from .common import with_repository, build_matcher
 from ..archive import Archive
 from ..cache import Cache
 from ..constants import *  # NOQA
@@ -18,7 +18,7 @@ class ListMixIn:
     @with_repository(compatibility=(Manifest.Operation.READ,))
     def do_list(self, args, repository, manifest, key):
         """List archive contents"""
-        matcher = self.build_matcher(args.patterns, args.paths)
+        matcher = build_matcher(args.patterns, args.paths)
         if args.format is not None:
             format = args.format
         elif args.short:

+ 3 - 2
src/borg/archiver/tar.py

@@ -26,6 +26,7 @@ from ..helpers import basic_json_data, json_print
 from ..helpers import log_multi
 
 from .common import with_repository, with_archive, Highlander, define_exclusion_group
+from .common import build_matcher, build_filter
 
 from ..logger import create_logger
 
@@ -68,14 +69,14 @@ class TarMixIn:
         return self.exit_code
 
     def _export_tar(self, args, archive, tarstream):
-        matcher = self.build_matcher(args.patterns, args.paths)
+        matcher = build_matcher(args.patterns, args.paths)
 
         progress = args.progress
         output_list = args.output_list
         strip_components = args.strip_components
         hlm = HardLinkManager(id_type=bytes, info_type=str)  # hlid -> path
 
-        filter = self.build_filter(matcher, strip_components)
+        filter = build_filter(matcher, strip_components)
 
         # The | (pipe) symbol instructs tarfile to use a streaming mode of operation
         # where it never seeks on the passed fileobj.

+ 3 - 3
src/borg/fuse.py

@@ -35,7 +35,7 @@ from .logger import create_logger
 logger = create_logger()
 
 from .crypto.low_level import blake2b_128
-from .archiver import Archiver
+from .archiver.common import build_matcher, build_filter
 from .archive import Archive, get_item_uid_gid
 from .hashindex import FuseVersionsIndex
 from .helpers import daemonize, daemonizing, signal_handler, format_file_size
@@ -338,10 +338,10 @@ class FuseBackend:
             consider_part_files=self._args.consider_part_files,
         )
         strip_components = self._args.strip_components
-        matcher = Archiver.build_matcher(self._args.patterns, self._args.paths)
+        matcher = build_matcher(self._args.patterns, self._args.paths)
         hlm = HardLinkManager(id_type=bytes, info_type=str)  # hlid -> path
 
-        filter = Archiver.build_filter(matcher, strip_components)
+        filter = build_filter(matcher, strip_components)
         for item_inode, item in self.cache.iter_archive_items(
             archive.metadata.items, filter=filter, consider_part_files=self._args.consider_part_files
         ):

+ 4 - 3
src/borg/testsuite/archiver.py

@@ -32,6 +32,7 @@ import borg.helpers.errors
 from .. import xattr, helpers, platform
 from ..archive import Archive, ChunkBuffer
 from ..archiver import Archiver, PURE_PYTHON_MSGPACK_WARNING
+from ..archiver.common import build_filter, build_matcher
 from ..cache import Cache, LocalCache
 from ..chunker import has_seek_hole
 from ..constants import *  # NOQA
@@ -4573,19 +4574,19 @@ class TestBuildFilter:
     def test_basic(self):
         matcher = PatternMatcher()
         matcher.add([parse_pattern("included")], IECommand.Include)
-        filter = Archiver.build_filter(matcher, 0)
+        filter = build_filter(matcher, 0)
         assert filter(Item(path="included"))
         assert filter(Item(path="included/file"))
         assert not filter(Item(path="something else"))
 
     def test_empty(self):
         matcher = PatternMatcher(fallback=True)
-        filter = Archiver.build_filter(matcher, 0)
+        filter = build_filter(matcher, 0)
         assert filter(Item(path="anything"))
 
     def test_strip_components(self):
         matcher = PatternMatcher(fallback=True)
-        filter = Archiver.build_filter(matcher, strip_components=1)
+        filter = build_filter(matcher, strip_components=1)
         assert not filter(Item(path="shallow"))
         assert not filter(Item(path="shallow/"))  # can this even happen? paths are normalized...
         assert filter(Item(path="deep enough/file"))