1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-25 01:06:50 +00:00

move rlist command to archiver.rlist

This commit is contained in:
Thomas Waldmann 2022-07-09 01:35:13 +02:00
parent b959ff913a
commit add73745c6
3 changed files with 124 additions and 105 deletions

View file

@ -127,6 +127,7 @@ per_file_ignores =
src/borg/archiver/keys.py:F405
src/borg/archiver/prune.py:F405
src/borg/archiver/rdelete.py:F405
src/borg/archiver/rlist.py:E501
src/borg/archiver/tar.py:F405
src/borg/cache.py:E127,E128,E402,E501,E722,W504
src/borg/fuse.py:E402,E501,E722,W504

View file

@ -38,7 +38,6 @@
from ..helpers import Error, set_ec
from ..helpers import location_validator, archivename_validator, ChunkerParams, Location
from ..helpers import NameSpec, CommentSpec, FilesCacheMode
from ..helpers import BaseFormatter, ArchiveFormatter
from ..helpers import format_timedelta, format_file_size
from ..helpers import remove_surrogates, bin_to_hex, eval_escapes
from ..helpers import timestamp
@ -104,6 +103,7 @@ def get_func(args):
from .rename import RenameMixIn
from .rcreate import RCreateMixIn
from .rdelete import RDeleteMixIn
from .rlist import RListMixIn
from .serve import ServeMixIn
from .tar import TarMixIn
from .transfer import TransferMixIn
@ -127,6 +127,7 @@ class Archiver(
RenameMixIn,
RCreateMixIn,
RDeleteMixIn,
RListMixIn,
ServeMixIn,
TransferMixIn,
):
@ -701,30 +702,6 @@ def do_extract(self, args, repository, manifest, key, archive):
pi.finish()
return self.exit_code
@with_repository(compatibility=(Manifest.Operation.READ,))
def do_rlist(self, args, repository, manifest, key):
"""List the archives contained in a repository"""
if args.format is not None:
format = args.format
elif args.short:
format = "{archive}{NL}"
else:
format = "{archive:<36} {time} [{id}]{NL}"
formatter = ArchiveFormatter(format, repository, manifest, key, json=args.json, iec=args.iec)
output_data = []
for archive_info in manifest.archives.list_considering(args):
if args.json:
output_data.append(formatter.get_item_data(archive_info))
else:
sys.stdout.write(formatter.format_item(archive_info))
if args.json:
json_print(basic_json_data(manifest, extra={"archives": output_data}))
return self.exit_code
@with_repository(cache=True, compatibility=(Manifest.Operation.READ,))
def do_rinfo(self, args, repository, manifest, key, cache):
"""Show repository infos"""
@ -1574,6 +1551,7 @@ def define_common_options(add_common_option):
self.build_parser_delete(subparsers, common_parser, mid_common_parser)
self.build_parser_help(subparsers, common_parser, mid_common_parser, parser)
self.build_parser_rdelete(subparsers, common_parser, mid_common_parser, parser)
self.build_parser_rlist(subparsers, common_parser, mid_common_parser)
# borg extract
extract_epilog = process_epilog(
@ -1707,86 +1685,6 @@ def define_common_options(add_common_option):
self.build_parser_keys(subparsers, common_parser, mid_common_parser)
self.build_parser_rcreate(subparsers, common_parser, mid_common_parser)
# borg rlist
rlist_epilog = (
process_epilog(
"""
This command lists the archives contained in a repository.
.. man NOTES
The FORMAT specifier syntax
+++++++++++++++++++++++++++
The ``--format`` option uses python's `format string syntax
<https://docs.python.org/3.9/library/string.html#formatstrings>`_.
Examples:
::
$ borg rlist --format '{archive}{NL}'
ArchiveFoo
ArchiveBar
...
# {VAR:NUMBER} - pad to NUMBER columns.
# Strings are left-aligned, numbers are right-aligned.
# Note: time columns except ``isomtime``, ``isoctime`` and ``isoatime`` cannot be padded.
$ borg rlist --format '{archive:36} {time} [{id}]{NL}' /path/to/repo
ArchiveFoo Thu, 2021-12-09 10:22:28 [0b8e9a312bef3f2f6e2d0fc110c196827786c15eba0188738e81697a7fa3b274]
...
The following keys are always available:
"""
)
+ BaseFormatter.keys_help()
+ textwrap.dedent(
"""
Keys available only when listing archives in a repository:
"""
)
+ ArchiveFormatter.keys_help()
)
subparser = subparsers.add_parser(
"rlist",
parents=[common_parser],
add_help=False,
description=self.do_rlist.__doc__,
epilog=rlist_epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
help="list repository contents",
)
subparser.set_defaults(func=self.do_rlist)
subparser.add_argument(
"--consider-checkpoints",
action="store_true",
dest="consider_checkpoints",
help="Show checkpoint archives in the repository contents list (default: hidden).",
)
subparser.add_argument(
"--short", dest="short", action="store_true", help="only print the archive names, nothing else"
)
subparser.add_argument(
"--format",
metavar="FORMAT",
dest="format",
help="specify format for archive listing " '(default: "{archive:<36} {time} [{id}]{NL}")',
)
subparser.add_argument(
"--json",
action="store_true",
help="Format output as JSON. "
"The form of ``--format`` is ignored, "
"but keys used in it are added to the JSON output. "
"Some keys are always present. Note: JSON can only represent text. "
'A "barchive" key is therefore not available.',
)
define_archive_filters_group(subparser)
# borg recreate
recreate_epilog = process_epilog(
"""

120
src/borg/archiver/rlist.py Normal file
View file

@ -0,0 +1,120 @@
import argparse
import textwrap
import sys
from .common import with_repository
from ..constants import * # NOQA
from ..helpers import Manifest
from ..helpers import BaseFormatter, ArchiveFormatter, json_print, basic_json_data
from ..logger import create_logger
logger = create_logger()
class RListMixIn:
@with_repository(compatibility=(Manifest.Operation.READ,))
def do_rlist(self, args, repository, manifest, key):
"""List the archives contained in a repository"""
if args.format is not None:
format = args.format
elif args.short:
format = "{archive}{NL}"
else:
format = "{archive:<36} {time} [{id}]{NL}"
formatter = ArchiveFormatter(format, repository, manifest, key, json=args.json, iec=args.iec)
output_data = []
for archive_info in manifest.archives.list_considering(args):
if args.json:
output_data.append(formatter.get_item_data(archive_info))
else:
sys.stdout.write(formatter.format_item(archive_info))
if args.json:
json_print(basic_json_data(manifest, extra={"archives": output_data}))
return self.exit_code
def build_parser_rlist(self, subparsers, common_parser, mid_common_parser):
from .common import process_epilog, define_archive_filters_group
rlist_epilog = (
process_epilog(
"""
This command lists the archives contained in a repository.
.. man NOTES
The FORMAT specifier syntax
+++++++++++++++++++++++++++
The ``--format`` option uses python's `format string syntax
<https://docs.python.org/3.9/library/string.html#formatstrings>`_.
Examples:
::
$ borg rlist --format '{archive}{NL}'
ArchiveFoo
ArchiveBar
...
# {VAR:NUMBER} - pad to NUMBER columns.
# Strings are left-aligned, numbers are right-aligned.
# Note: time columns except ``isomtime``, ``isoctime`` and ``isoatime`` cannot be padded.
$ borg rlist --format '{archive:36} {time} [{id}]{NL}' /path/to/repo
ArchiveFoo Thu, 2021-12-09 10:22:28 [0b8e9a312bef3f2f6e2d0fc110c196827786c15eba0188738e81697a7fa3b274]
...
The following keys are always available:
"""
)
+ BaseFormatter.keys_help()
+ textwrap.dedent(
"""
Keys available only when listing archives in a repository:
"""
)
+ ArchiveFormatter.keys_help()
)
subparser = subparsers.add_parser(
"rlist",
parents=[common_parser],
add_help=False,
description=self.do_rlist.__doc__,
epilog=rlist_epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
help="list repository contents",
)
subparser.set_defaults(func=self.do_rlist)
subparser.add_argument(
"--consider-checkpoints",
action="store_true",
dest="consider_checkpoints",
help="Show checkpoint archives in the repository contents list (default: hidden).",
)
subparser.add_argument(
"--short", dest="short", action="store_true", help="only print the archive names, nothing else"
)
subparser.add_argument(
"--format",
metavar="FORMAT",
dest="format",
help="specify format for archive listing " '(default: "{archive:<36} {time} [{id}]{NL}")',
)
subparser.add_argument(
"--json",
action="store_true",
help="Format output as JSON. "
"The form of ``--format`` is ignored, "
"but keys used in it are added to the JSON output. "
"Some keys are always present. Note: JSON can only represent text. "
'A "barchive" key is therefore not available.',
)
define_archive_filters_group(subparser)