diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 50540369c..2604818d8 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -30,7 +30,7 @@ from .constants import * # NOQA from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR from .helpers import Error, NoManifestError from .helpers import location_validator, archivename_validator, ChunkerParams, CompressionSpec -from .helpers import PrefixSpec, sort_by_spec, HUMAN_SORT_KEYS +from .helpers import prefix_spec, sort_by_spec, HUMAN_SORT_KEYS from .helpers import BaseFormatter, ItemFormatter, ArchiveFormatter, format_time, format_file_size, format_archive from .helpers import safe_encode, remove_surrogates, bin_to_hex from .helpers import prune_within, prune_split @@ -1605,7 +1605,7 @@ class Archiver: subparser.add_argument('--last', dest='last', type=int, default=None, metavar='N', help='only check last N archives (Default: all)') - subparser.add_argument('-P', '--prefix', dest='prefix', type=PrefixSpec, + subparser.add_argument('-P', '--prefix', dest='prefix', type=prefix_spec, help='only consider archive names starting with this prefix') subparser.add_argument('-p', '--progress', dest='progress', action='store_true', default=False, @@ -1995,7 +1995,7 @@ class Archiver: subparser.add_argument('--format', '--list-format', dest='format', type=str, help="""specify format for file listing (default: "{mode} {user:6} {group:6} {size:8d} {isomtime} {path}{extra}{NL}")""") - subparser.add_argument('-P', '--prefix', dest='prefix', type=PrefixSpec, + subparser.add_argument('-P', '--prefix', dest='prefix', type=prefix_spec, help='only consider archive names starting with this prefix') subparser.add_argument('-e', '--exclude', dest='excludes', type=parse_pattern, action='append', @@ -2161,7 +2161,7 @@ class Archiver: help='number of monthly archives to keep') subparser.add_argument('-y', '--keep-yearly', dest='yearly', type=int, default=0, help='number of yearly archives to keep') - subparser.add_argument('-P', '--prefix', dest='prefix', type=PrefixSpec, + subparser.add_argument('-P', '--prefix', dest='prefix', type=prefix_spec, help='only consider archive names starting with this prefix') subparser.add_argument('--save-space', dest='save_space', action='store_true', default=False, @@ -2634,7 +2634,7 @@ class Archiver: if args.location.archive: raise Error('The options --first, --last and --prefix can only be used on repository targets.') - archives = manifest.archives.list() + archives = manifest.archives.list(prefix=args.prefix) if not archives: logger.critical('There are no archives.') self.exit_code = self.exit_code or EXIT_WARNING @@ -2645,7 +2645,7 @@ class Archiver: if args.last: archives.reverse() - n = args.first or args.last + n = args.first or args.last or len(archives) return archives[:n] diff --git a/src/borg/helpers.py b/src/borg/helpers.py index 85959e864..27ce121b9 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -142,11 +142,11 @@ class Archives(abc.MutableMapping): name = safe_encode(name) del self._archives[name] - def list(self, sort_by=None, reverse=False): + def list(self, sort_by=None, reverse=False, prefix=''): """ Inexpensive Archive.list_archives replacement if we just need .name, .id, .ts Returns list of borg.helpers.ArchiveInfo instances """ - archives = list(self.values()) # [self[name] for name in self] + archives = [x for x in self.values() if x.name.startswith(prefix)] if sort_by is not None: archives = sorted(archives, key=attrgetter(sort_by)) if reverse: @@ -572,10 +572,6 @@ def CompressionSpec(s): raise ValueError -def PrefixSpec(s): - return replace_placeholders(s) - - def dir_is_cachedir(path): """Determines whether the specified path is a cache directory (and therefore should potentially be excluded from the backup) according to @@ -658,10 +654,13 @@ def replace_placeholders(text): } return format_line(text, data) +prefix_spec = replace_placeholders + HUMAN_SORT_KEYS = ['timestamp'] + list(ArchiveInfo._fields) HUMAN_SORT_KEYS.remove('ts') + def sort_by_spec(text): for token in text.split(','): if token not in HUMAN_SORT_KEYS: