From 39e5aac47940bb7a893491cc8a79546775593421 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sat, 27 Jul 2019 14:04:13 -0700 Subject: [PATCH] If a "prefix" option in borgmatic's configuration has an empty value (blank or ""), then disable default prefix. --- NEWS | 5 ++++- borgmatic/borg/check.py | 4 ++-- borgmatic/borg/prune.py | 11 +++++++---- borgmatic/config/schema.yaml | 5 +++-- setup.py | 4 ++-- test_requirements.txt | 2 +- tests/unit/borg/test_check.py | 32 ++++++++++++++++++++++---------- tests/unit/borg/test_prune.py | 20 ++++++++++++++++++++ 8 files changed, 61 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index 7319611..96f0c1e 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,9 @@ -1.3.13.dev0 +1.3.13 * #199: Add note to documentation about using spaces instead of tabs for indentation, as YAML does not allow tabs. + * #203: Fix compatibility with ruamel.yaml 0.16.x. + * If a "prefix" option in borgmatic's configuration has an empty value (blank or ""), then disable + default prefix. 1.3.12 * Only log to syslog when run from a non-interactive console (e.g. a cron job). diff --git a/borgmatic/borg/check.py b/borgmatic/borg/check.py index f343157..678dd46 100644 --- a/borgmatic/borg/check.py +++ b/borgmatic/borg/check.py @@ -55,7 +55,7 @@ def _make_check_flags(checks, check_last=None, prefix=None): ''' if 'archives' in checks: last_flags = ('--last', str(check_last)) if check_last else () - prefix_flags = ('--prefix', prefix) if prefix else ('--prefix', DEFAULT_PREFIX) + prefix_flags = ('--prefix', prefix) if prefix else () else: last_flags = () prefix_flags = () @@ -102,7 +102,7 @@ def check_archives( if logger.isEnabledFor(logging.DEBUG): verbosity_flags = ('--debug', '--show-rc') - prefix = consistency_config.get('prefix') + prefix = consistency_config.get('prefix', DEFAULT_PREFIX) full_command = ( (local_path, 'check', repository) diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index 8525f54..e6f4eb0 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -21,12 +21,15 @@ def _make_prune_flags(retention_config): ('--keep-monthly', '6'), ) ''' - if not retention_config.get('prefix'): - retention_config['prefix'] = '{hostname}-' + config = dict(retention_config) + + if 'prefix' not in config: + config['prefix'] = '{hostname}-' + elif not config['prefix']: + config.pop('prefix') return ( - ('--' + option_name.replace('_', '-'), str(retention_config[option_name])) - for option_name, value in retention_config.items() + ('--' + option_name.replace('_', '-'), str(value)) for option_name, value in config.items() ) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 3957ae5..4907a5b 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -269,7 +269,7 @@ map: desc: | When pruning, only consider archive names starting with this prefix. Borg placeholders can be used. See the output of "borg help placeholders" for - details. Defaults to "{hostname}-". + details. Defaults to "{hostname}-". Use an empty value to disable the default. example: sourcehostname consistency: desc: | @@ -311,7 +311,8 @@ map: desc: | When performing the "archives" check, only consider archive names starting with this prefix. Borg placeholders can be used. See the output of - "borg help placeholders" for details. Defaults to "{hostname}-". + "borg help placeholders" for details. Defaults to "{hostname}-". Use an empty + value to disable the default. example: sourcehostname output: desc: | diff --git a/setup.py b/setup.py index f467413..19cd184 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import find_packages, setup -VERSION = '1.3.13.dev0' +VERSION = '1.3.13' setup( @@ -31,7 +31,7 @@ setup( obsoletes=['atticmatic'], install_requires=( 'pykwalify>=1.6.0,<14.06', - 'ruamel.yaml>0.15.0,<0.16.0', + 'ruamel.yaml>0.15.0,<0.17.0', 'setuptools', 'colorama>=0.4.1,<0.5', ), diff --git a/test_requirements.txt b/test_requirements.txt index cc0df6b..4495898 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -20,5 +20,5 @@ pytest==4.6.3 pytest-cov==2.7.1 python-dateutil==2.8.0 PyYAML==5.1.1 -ruamel.yaml>0.15.0,<0.16.0 +ruamel.yaml>0.15.0,<0.17.0 toml==0.10.0 diff --git a/tests/unit/borg/test_check.py b/tests/unit/borg/test_check.py index 83365f1..bdd9899 100644 --- a/tests/unit/borg/test_check.py +++ b/tests/unit/borg/test_check.py @@ -52,14 +52,14 @@ def test_make_check_flags_with_extract_omits_extract_flag(): assert flags == () -def test_make_check_flags_with_default_checks_returns_default_flags(): - flags = module._make_check_flags(module.DEFAULT_CHECKS) +def test_make_check_flags_with_default_checks_and_default_prefix_returns_default_flags(): + flags = module._make_check_flags(module.DEFAULT_CHECKS, prefix=module.DEFAULT_PREFIX) assert flags == ('--prefix', module.DEFAULT_PREFIX) -def test_make_check_flags_with_all_checks_returns_default_flags(): - flags = module._make_check_flags(module.DEFAULT_CHECKS + ('extract',)) +def test_make_check_flags_with_all_checks_and_default_prefix_returns_default_flags(): + flags = module._make_check_flags(module.DEFAULT_CHECKS + ('extract',), prefix=module.DEFAULT_PREFIX) assert flags == ('--prefix', module.DEFAULT_PREFIX) @@ -67,7 +67,7 @@ def test_make_check_flags_with_all_checks_returns_default_flags(): def test_make_check_flags_with_archives_check_and_last_includes_last_flag(): flags = module._make_check_flags(('archives',), check_last=3) - assert flags == ('--archives-only', '--last', '3', '--prefix', module.DEFAULT_PREFIX) + assert flags == ('--archives-only', '--last', '3') def test_make_check_flags_with_repository_check_and_last_omits_last_flag(): @@ -79,7 +79,7 @@ def test_make_check_flags_with_repository_check_and_last_omits_last_flag(): def test_make_check_flags_with_default_checks_and_last_includes_last_flag(): flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3) - assert flags == ('--last', '3', '--prefix', module.DEFAULT_PREFIX) + assert flags == ('--last', '3') def test_make_check_flags_with_archives_check_and_prefix_includes_prefix_flag(): @@ -88,6 +88,18 @@ def test_make_check_flags_with_archives_check_and_prefix_includes_prefix_flag(): assert flags == ('--archives-only', '--prefix', 'foo-') +def test_make_check_flags_with_archives_check_and_empty_prefix_omits_prefix_flag(): + flags = module._make_check_flags(('archives',), prefix='') + + assert flags == ('--archives-only',) + + +def test_make_check_flags_with_archives_check_and_none_prefix_omits_prefix_flag(): + flags = module._make_check_flags(('archives',), prefix=None) + + assert flags == ('--archives-only',) + + def test_make_check_flags_with_repository_check_and_prefix_omits_prefix_flag(): flags = module._make_check_flags(('repository',), prefix='foo-') @@ -114,7 +126,7 @@ def test_check_archives_calls_borg_with_parameters(checks): consistency_config = {'check_last': check_last} flexmock(module).should_receive('_parse_checks').and_return(checks) flexmock(module).should_receive('_make_check_flags').with_args( - checks, check_last, None + checks, check_last, module.DEFAULT_PREFIX ).and_return(()) insert_execute_command_mock(('borg', 'check', 'repo')) @@ -179,7 +191,7 @@ def test_check_archives_with_local_path_calls_borg_via_local_path(): consistency_config = {'check_last': check_last} flexmock(module).should_receive('_parse_checks').and_return(checks) flexmock(module).should_receive('_make_check_flags').with_args( - checks, check_last, None + checks, check_last, module.DEFAULT_PREFIX ).and_return(()) insert_execute_command_mock(('borg1', 'check', 'repo')) @@ -197,7 +209,7 @@ def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters( consistency_config = {'check_last': check_last} flexmock(module).should_receive('_parse_checks').and_return(checks) flexmock(module).should_receive('_make_check_flags').with_args( - checks, check_last, None + checks, check_last, module.DEFAULT_PREFIX ).and_return(()) insert_execute_command_mock(('borg', 'check', 'repo', '--remote-path', 'borg1')) @@ -215,7 +227,7 @@ def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): consistency_config = {'check_last': check_last} flexmock(module).should_receive('_parse_checks').and_return(checks) flexmock(module).should_receive('_make_check_flags').with_args( - checks, check_last, None + checks, check_last, module.DEFAULT_PREFIX ).and_return(()) insert_execute_command_mock(('borg', 'check', 'repo', '--lock-wait', '5')) diff --git a/tests/unit/borg/test_prune.py b/tests/unit/borg/test_prune.py index fedfec9..74b0dca 100644 --- a/tests/unit/borg/test_prune.py +++ b/tests/unit/borg/test_prune.py @@ -33,6 +33,26 @@ def test_make_prune_flags_accepts_prefix_with_placeholders(): assert tuple(result) == expected +def test_make_prune_flags_treats_empty_prefix_as_no_prefix(): + retention_config = OrderedDict((('keep_daily', 1), ('prefix', ''))) + + result = module._make_prune_flags(retention_config) + + expected = (('--keep-daily', '1'),) + + assert tuple(result) == expected + + +def test_make_prune_flags_treats_none_prefix_as_no_prefix(): + retention_config = OrderedDict((('keep_daily', 1), ('prefix', None))) + + result = module._make_prune_flags(retention_config) + + expected = (('--keep-daily', '1'),) + + assert tuple(result) == expected + + PRUNE_COMMAND = ( 'borg', 'prune',