Reuse "borg info" function.

This commit is contained in:
Dan Helfman 2022-05-27 13:51:11 -07:00
parent cb5fe02ebd
commit 8ddb7268eb
2 changed files with 13 additions and 14 deletions

View file

@ -1,6 +1,8 @@
import argparse
import logging import logging
import subprocess import subprocess
from borgmatic.borg import info
from borgmatic.execute import DO_NOT_CAPTURE, execute_command from borgmatic.execute import DO_NOT_CAPTURE, execute_command
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -23,17 +25,14 @@ def initialize_repository(
whether the repository should be append-only, and the storage quota to use, initialize the whether the repository should be append-only, and the storage quota to use, initialize the
repository. If the repository already exists, then log and skip initialization. repository. If the repository already exists, then log and skip initialization.
''' '''
info_command = (
(local_path, 'info')
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
+ (('--debug',) if logger.isEnabledFor(logging.DEBUG) else ())
+ (('--remote-path', remote_path) if remote_path else ())
+ (repository,)
)
logger.debug(' '.join(info_command))
try: try:
execute_command(info_command, output_log_level=None) info.display_archives_info(
repository,
storage_config,
argparse.Namespace(json=True, archive=None),
local_path,
remote_path,
)
logger.info('Repository already exists. Skipping initialization.') logger.info('Repository already exists. Skipping initialization.')
return return
except subprocess.CalledProcessError as error: except subprocess.CalledProcessError as error:

View file

@ -13,11 +13,11 @@ INIT_COMMAND = ('borg', 'init', '--encryption', 'repokey')
def insert_info_command_found_mock(): def insert_info_command_found_mock():
flexmock(module).should_receive('execute_command') flexmock(module.info).should_receive('display_archives_info')
def insert_info_command_not_found_mock(): def insert_info_command_not_found_mock():
flexmock(module).should_receive('execute_command').and_raise( flexmock(module.info).should_receive('display_archives_info').and_raise(
subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, []) subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, [])
) )
@ -48,13 +48,13 @@ def test_initialize_repository_raises_for_borg_init_error():
def test_initialize_repository_skips_initialization_when_repository_already_exists(): def test_initialize_repository_skips_initialization_when_repository_already_exists():
flexmock(module).should_receive('execute_command').once() insert_info_command_found_mock()
module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey') module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
def test_initialize_repository_raises_for_unknown_info_command_error(): def test_initialize_repository_raises_for_unknown_info_command_error():
flexmock(module).should_receive('execute_command').and_raise( flexmock(module.info).should_receive('display_archives_info').and_raise(
subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, []) subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
) )