From 3e3e411bf4582a93465f558f8d5f0f92ae48340a Mon Sep 17 00:00:00 2001 From: Florian Lindner Date: Sat, 10 Nov 2018 18:27:31 +0100 Subject: [PATCH] Encapsulate the subprocess call inside a run function. --- borgmatic/borg/check.py | 3 +-- borgmatic/borg/create.py | 5 ++--- borgmatic/borg/extract.py | 7 +++---- borgmatic/borg/info.py | 7 ++----- borgmatic/borg/list.py | 6 ++---- borgmatic/borg/prune.py | 5 ++--- borgmatic/commands/borgmatic.py | 8 +++----- borgmatic/run.py | 14 ++++++++++++++ 8 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 borgmatic/run.py diff --git a/borgmatic/borg/check.py b/borgmatic/borg/check.py index 96f0fe27..7844e9c9 100644 --- a/borgmatic/borg/check.py +++ b/borgmatic/borg/check.py @@ -117,8 +117,7 @@ def check_archives( # The check command spews to stdout/stderr even without the verbose flag. Suppress it. stdout = None if verbosity_flags else open(os.devnull, 'w') - logger.debug(' '.join(full_command)) - subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT) + run(full_command, stdout=stdout, stderr=subprocess.STDOUT) if 'extract' in checks: extract.extract_last_archive_dry_run(repository, lock_wait, local_path, remote_path) diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index cad0a294..34c0c34a 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -2,9 +2,9 @@ import glob import itertools import logging import os -import subprocess import tempfile +from borgmatic.run import run logger = logging.getLogger(__name__) @@ -154,5 +154,4 @@ def create_archive( + (('--json',) if json else ()) ) - logger.debug(' '.join(full_command)) - subprocess.check_call(full_command) + return run(full_command, capture_output = json) diff --git a/borgmatic/borg/extract.py b/borgmatic/borg/extract.py index 960bda57..4f68138e 100644 --- a/borgmatic/borg/extract.py +++ b/borgmatic/borg/extract.py @@ -1,7 +1,7 @@ import logging import sys -import subprocess +from borgmatic.run import run logger = logging.getLogger(__name__) @@ -26,7 +26,7 @@ def extract_last_archive_dry_run(repository, lock_wait=None, local_path='borg', + verbosity_flags ) - list_output = subprocess.check_output(full_list_command).decode(sys.stdout.encoding) + list_output = run(full_list_command, capture_output = True) last_archive_name = list_output.strip().split('\n')[-1] if not last_archive_name: @@ -48,5 +48,4 @@ def extract_last_archive_dry_run(repository, lock_wait=None, local_path='borg', + list_flag ) - logger.debug(' '.join(full_extract_command)) - subprocess.check_call(full_extract_command) + run(full_extract_command) diff --git a/borgmatic/borg/info.py b/borgmatic/borg/info.py index 2fddd90d..316dde8d 100644 --- a/borgmatic/borg/info.py +++ b/borgmatic/borg/info.py @@ -1,6 +1,6 @@ import logging -import subprocess +from borgmatic.run import run logger = logging.getLogger(__name__) @@ -23,7 +23,4 @@ def display_archives_info( + (('--json',) if json else ()) ) - logger.debug(' '.join(full_command)) - - output = subprocess.check_output(full_command) - return output.decode() if output is not None else None + return run(full_command, capture_output = json) diff --git a/borgmatic/borg/list.py b/borgmatic/borg/list.py index 8e532cba..9d2284e6 100644 --- a/borgmatic/borg/list.py +++ b/borgmatic/borg/list.py @@ -1,6 +1,6 @@ import logging -import subprocess +from borgmatic.run import run logger = logging.getLogger(__name__) @@ -20,7 +20,5 @@ def list_archives(repository, storage_config, local_path='borg', remote_path=Non + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--json',) if json else ()) ) - logger.debug(' '.join(full_command)) - output = subprocess.check_output(full_command) - return output.decode() if output is not None else None + run(full_command, capture_output = json) diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index fa1277d5..c8d62a42 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -1,6 +1,6 @@ import logging -import subprocess +from borgmatic.run import run logger = logging.getLogger(__name__) @@ -53,5 +53,4 @@ def prune_archives( + (('--dry-run',) if dry_run else ()) ) - logger.debug(' '.join(full_command)) - subprocess.check_call(full_command) + run(full_command) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 61629fba..90983db6 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -205,7 +205,7 @@ def _run_commands_on_repository( ) if args.create: logger.info('{}: Creating archive{}'.format(repository, dry_run_label)) - borg_create.create_archive( + output = borg_create.create_archive( args.dry_run, repository, location, @@ -213,6 +213,8 @@ def _run_commands_on_repository( local_path=local_path, remote_path=remote_path, ) + if args.json: + json_results.append(json.loads(output)) if args.check and checks.repository_enabled_for_checks(repository, consistency): logger.info('{}: Running consistency checks'.format(repository)) borg_check.check_archives( @@ -225,8 +227,6 @@ def _run_commands_on_repository( ) 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)) output = borg_info.display_archives_info( @@ -234,8 +234,6 @@ def _run_commands_on_repository( ) if args.json: json_results.append(json.loads(output)) - else: - sys.stdout.write(output) def main(): # pragma: no cover diff --git a/borgmatic/run.py b/borgmatic/run.py new file mode 100644 index 00000000..e12a5b1c --- /dev/null +++ b/borgmatic/run.py @@ -0,0 +1,14 @@ +import logging +import subprocess + + +logger = logging.getLogger(__name__) + + +def run(args, capture_output = False, **kwargs): + logger.debug('Executing: ' + ' '.join(args)) + + if capture_output: + return subprocess.check_output(args, **kwargs).decode() + + subprocess.check_call(args, **kwargs)