add support for --list --json (#74)

This commit is contained in:
Thomas LÉVEIL 2018-07-28 21:21:38 +00:00 committed by Gitea
parent cf6ab60d2e
commit 789bcd402a
6 changed files with 148 additions and 57 deletions

View file

@ -7,7 +7,7 @@ from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def list_archives(verbosity, repository, storage_config, local_path='borg', remote_path=None): def list_archives(verbosity, repository, storage_config, local_path='borg', remote_path=None, json=False):
''' '''
Given a verbosity flag, a local or remote repository path, and a storage config dict, Given a verbosity flag, a local or remote repository path, and a storage config dict,
list Borg archives in the repository. list Borg archives in the repository.
@ -18,6 +18,7 @@ def list_archives(verbosity, repository, storage_config, local_path='borg', remo
(local_path, 'list', repository) (local_path, 'list', repository)
+ (('--remote-path', remote_path) if remote_path else ()) + (('--remote-path', remote_path) if remote_path else ())
+ (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--lock-wait', str(lock_wait)) if lock_wait else ())
+ (('--json',) if json else ())
+ { + {
VERBOSITY_SOME: ('--info',), VERBOSITY_SOME: ('--info',),
VERBOSITY_LOTS: ('--debug',), VERBOSITY_LOTS: ('--debug',),
@ -25,4 +26,6 @@ def list_archives(verbosity, repository, storage_config, local_path='borg', remo
) )
logger.debug(' '.join(full_command)) logger.debug(' '.join(full_command))
subprocess.check_call(full_command)
output = subprocess.check_output(full_command)
return output.decode() if output is not None else None

View file

@ -1,5 +1,5 @@
from argparse import ArgumentParser from argparse import ArgumentParser
import json
import logging import logging
import os import os
from subprocess import CalledProcessError from subprocess import CalledProcessError
@ -76,6 +76,13 @@ def parse_arguments(*arguments):
action='store_true', action='store_true',
help='Display summary information on archives', help='Display summary information on archives',
) )
parser.add_argument(
'--json',
dest='json',
default=False,
action='store_true',
help='Output results from the --list option as json',
)
parser.add_argument( parser.add_argument(
'-n', '--dry-run', '-n', '--dry-run',
dest='dry_run', dest='dry_run',
@ -90,6 +97,9 @@ def parse_arguments(*arguments):
args = parser.parse_args(arguments) args = parser.parse_args(arguments)
if args.json and not args.list:
raise ValueError("The --json option can only be used with the --list option")
# If any of the action flags are explicitly requested, leave them as-is. Otherwise, assume # If any of the action flags are explicitly requested, leave them as-is. Otherwise, assume
# defaults: Mutate the given arguments to enable the default actions. # defaults: Mutate the given arguments to enable the default actions.
if args.prune or args.create or args.check or args.list or args.info: if args.prune or args.create or args.check or args.list or args.info:
@ -121,59 +131,7 @@ def run_configuration(config_filename, args): # pragma: no cover
if args.create: if args.create:
hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup') hook.execute_hook(hooks.get('before_backup'), config_filename, 'pre-backup')
for unexpanded_repository in location['repositories']: _run_commands(args, consistency, local_path, location, remote_path, retention, storage)
repository = os.path.expanduser(unexpanded_repository)
dry_run_label = ' (dry run; not making any changes)' if args.dry_run else ''
if args.prune:
logger.info('{}: Pruning archives{}'.format(repository, dry_run_label))
borg_prune.prune_archives(
args.verbosity,
args.dry_run,
repository,
storage,
retention,
local_path=local_path,
remote_path=remote_path,
)
if args.create:
logger.info('{}: Creating archive{}'.format(repository, dry_run_label))
borg_create.create_archive(
args.verbosity,
args.dry_run,
repository,
location,
storage,
local_path=local_path,
remote_path=remote_path,
)
if args.check:
logger.info('{}: Running consistency checks'.format(repository))
borg_check.check_archives(
args.verbosity,
repository,
storage,
consistency,
local_path=local_path,
remote_path=remote_path,
)
if args.list:
logger.info('{}: Listing archives'.format(repository))
borg_list.list_archives(
args.verbosity,
repository,
storage,
local_path=local_path,
remote_path=remote_path,
)
if args.info:
logger.info('{}: Displaying summary info for archives'.format(repository))
borg_info.display_archives_info(
args.verbosity,
repository,
storage,
local_path=local_path,
remote_path=remote_path,
)
if args.create: if args.create:
hook.execute_hook(hooks.get('after_backup'), config_filename, 'post-backup') hook.execute_hook(hooks.get('after_backup'), config_filename, 'post-backup')
@ -182,6 +140,76 @@ def run_configuration(config_filename, args): # pragma: no cover
raise raise
def _run_commands(args, consistency, local_path, location, remote_path, retention, storage):
json_results = []
for unexpanded_repository in location['repositories']:
_run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention,
storage, unexpanded_repository)
if args.json:
sys.stdout.write(json.dumps(json_results))
def _run_commands_on_repository(args, consistency, json_results, local_path, location, remote_path, retention, storage,
unexpanded_repository): # pragma: no cover
repository = os.path.expanduser(unexpanded_repository)
dry_run_label = ' (dry run; not making any changes)' if args.dry_run else ''
if args.prune:
logger.info('{}: Pruning archives{}'.format(repository, dry_run_label))
borg_prune.prune_archives(
args.verbosity,
args.dry_run,
repository,
storage,
retention,
local_path=local_path,
remote_path=remote_path,
)
if args.create:
logger.info('{}: Creating archive{}'.format(repository, dry_run_label))
borg_create.create_archive(
args.verbosity,
args.dry_run,
repository,
location,
storage,
local_path=local_path,
remote_path=remote_path,
)
if args.check:
logger.info('{}: Running consistency checks'.format(repository))
borg_check.check_archives(
args.verbosity,
repository,
storage,
consistency,
local_path=local_path,
remote_path=remote_path,
)
if args.list:
logger.info('{}: Listing archives'.format(repository))
output = borg_list.list_archives(
args.verbosity,
repository,
storage,
local_path=local_path,
remote_path=remote_path,
json=args.json,
)
if args.json:
json_results.append(json.loads(output))
else:
sys.stdout.write(output)
if args.info:
logger.info('{}: Displaying summary info for archives'.format(repository))
borg_info.display_archives_info(
args.verbosity,
repository,
storage,
local_path=local_path,
remote_path=remote_path,
)
def main(): # pragma: no cover def main(): # pragma: no cover
try: try:
configure_signals() configure_signals()

View file

@ -15,6 +15,7 @@ def test_parse_arguments_with_no_arguments_uses_defaults():
assert parser.config_paths == config_paths assert parser.config_paths == config_paths
assert parser.excludes_filename == None assert parser.excludes_filename == None
assert parser.verbosity is None assert parser.verbosity is None
assert parser.json is False
def test_parse_arguments_with_path_arguments_overrides_defaults(): def test_parse_arguments_with_path_arguments_overrides_defaults():
@ -47,6 +48,11 @@ def test_parse_arguments_with_verbosity_flag_overrides_default():
assert parser.verbosity == 1 assert parser.verbosity == 1
def test_parse_arguments_with_json_flag_overrides_default():
parser = module.parse_arguments('--list', '--json')
assert parser.json is True
def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled(): def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default']) flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
@ -82,3 +88,8 @@ def test_parse_arguments_with_invalid_arguments_exits():
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
module.parse_arguments('--posix-me-harder') module.parse_arguments('--posix-me-harder')
def test_parse_arguments_with_json_flag_but_no_list_flag_raises_value_error():
with pytest.raises(ValueError):
module.parse_arguments('--json')

View file

@ -8,7 +8,7 @@ from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
def insert_subprocess_mock(check_call_command, **kwargs): def insert_subprocess_mock(check_call_command, **kwargs):
subprocess = flexmock(module.subprocess) subprocess = flexmock(module.subprocess)
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once() subprocess.should_receive('check_output').with_args(check_call_command, **kwargs).once()
LIST_COMMAND = ('borg', 'list', 'repo') LIST_COMMAND = ('borg', 'list', 'repo')
@ -44,6 +44,17 @@ def test_list_archives_with_verbosity_lots_calls_borg_with_debug_parameter():
) )
def test_list_archives_with_json_calls_borg_with_json_parameter():
insert_subprocess_mock(LIST_COMMAND + ('--json',))
module.list_archives(
verbosity=None,
repository='repo',
storage_config={},
json=True,
)
def test_list_archives_with_local_path_calls_borg_via_local_path(): def test_list_archives_with_local_path_calls_borg_via_local_path():
insert_subprocess_mock(('borg1',) + LIST_COMMAND[1:]) insert_subprocess_mock(('borg1',) + LIST_COMMAND[1:])

View file

@ -0,0 +1,38 @@
from borgmatic.commands import borgmatic
from flexmock import flexmock
import json
import pytest
import sys
def test__run_commands_handles_multiple_json_outputs_in_array():
# THEN
(flexmock(borgmatic)
.should_receive("_run_commands_on_repository")
.times(3)
.replace_with(lambda args, consistency, json_results, local_path, location, remote_path, retention, storage,
unexpanded_repository: json_results.append({"whatever": unexpanded_repository}))
)
(flexmock(sys.stdout)
.should_call("write")
.with_args(json.dumps(json.loads('''
[
{"whatever": "fake_repo1"},
{"whatever": "fake_repo2"},
{"whatever": "fake_repo3"}
]
''')))
)
borgmatic._run_commands(args=flexmock(json=True),
consistency=None,
local_path=None,
location={"repositories": [
"fake_repo1",
"fake_repo2",
"fake_repo3"
]},
remote_path=None,
retention=None,
storage=None)