Browse Source

If a "prefix" option in borgmatic's configuration has an empty value (blank or ""), then disable default prefix.

Dan Helfman 5 years ago
parent
commit
39e5aac479

+ 4 - 1
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
  * #199: Add note to documentation about using spaces instead of tabs for indentation, as YAML does
    not allow tabs.
    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
 1.3.12
  * Only log to syslog when run from a non-interactive console (e.g. a cron job).
  * Only log to syslog when run from a non-interactive console (e.g. a cron job).

+ 2 - 2
borgmatic/borg/check.py

@@ -55,7 +55,7 @@ def _make_check_flags(checks, check_last=None, prefix=None):
     '''
     '''
     if 'archives' in checks:
     if 'archives' in checks:
         last_flags = ('--last', str(check_last)) if check_last else ()
         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:
     else:
         last_flags = ()
         last_flags = ()
         prefix_flags = ()
         prefix_flags = ()
@@ -102,7 +102,7 @@ def check_archives(
         if logger.isEnabledFor(logging.DEBUG):
         if logger.isEnabledFor(logging.DEBUG):
             verbosity_flags = ('--debug', '--show-rc')
             verbosity_flags = ('--debug', '--show-rc')
 
 
-        prefix = consistency_config.get('prefix')
+        prefix = consistency_config.get('prefix', DEFAULT_PREFIX)
 
 
         full_command = (
         full_command = (
             (local_path, 'check', repository)
             (local_path, 'check', repository)

+ 7 - 4
borgmatic/borg/prune.py

@@ -21,12 +21,15 @@ def _make_prune_flags(retention_config):
             ('--keep-monthly', '6'),
             ('--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 (
     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()
     )
     )
 
 
 
 

+ 3 - 2
borgmatic/config/schema.yaml

@@ -269,7 +269,7 @@ map:
                 desc: |
                 desc: |
                     When pruning, only consider archive names starting with this prefix.
                     When pruning, only consider archive names starting with this prefix.
                     Borg placeholders can be used. See the output of "borg help placeholders" for
                     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
                 example: sourcehostname
     consistency:
     consistency:
         desc: |
         desc: |
@@ -311,7 +311,8 @@ map:
                 desc: |
                 desc: |
                     When performing the "archives" check, only consider archive names starting with
                     When performing the "archives" check, only consider archive names starting with
                     this prefix. Borg placeholders can be used. See the output of
                     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
                 example: sourcehostname
     output:
     output:
         desc: |
         desc: |

+ 2 - 2
setup.py

@@ -1,6 +1,6 @@
 from setuptools import find_packages, setup
 from setuptools import find_packages, setup
 
 
-VERSION = '1.3.13.dev0'
+VERSION = '1.3.13'
 
 
 
 
 setup(
 setup(
@@ -31,7 +31,7 @@ setup(
     obsoletes=['atticmatic'],
     obsoletes=['atticmatic'],
     install_requires=(
     install_requires=(
         'pykwalify>=1.6.0,<14.06',
         'pykwalify>=1.6.0,<14.06',
-        'ruamel.yaml>0.15.0,<0.16.0',
+        'ruamel.yaml>0.15.0,<0.17.0',
         'setuptools',
         'setuptools',
         'colorama>=0.4.1,<0.5',
         'colorama>=0.4.1,<0.5',
     ),
     ),

+ 1 - 1
test_requirements.txt

@@ -20,5 +20,5 @@ pytest==4.6.3
 pytest-cov==2.7.1
 pytest-cov==2.7.1
 python-dateutil==2.8.0
 python-dateutil==2.8.0
 PyYAML==5.1.1
 PyYAML==5.1.1
-ruamel.yaml>0.15.0,<0.16.0
+ruamel.yaml>0.15.0,<0.17.0
 toml==0.10.0
 toml==0.10.0

+ 22 - 10
tests/unit/borg/test_check.py

@@ -52,14 +52,14 @@ def test_make_check_flags_with_extract_omits_extract_flag():
     assert flags == ()
     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)
     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)
     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():
 def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
     flags = module._make_check_flags(('archives',), check_last=3)
     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():
 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():
 def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
     flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
     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():
 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-')
     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():
 def test_make_check_flags_with_repository_check_and_prefix_omits_prefix_flag():
     flags = module._make_check_flags(('repository',), prefix='foo-')
     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}
     consistency_config = {'check_last': check_last}
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_make_check_flags').with_args(
     flexmock(module).should_receive('_make_check_flags').with_args(
-        checks, check_last, None
+        checks, check_last, module.DEFAULT_PREFIX
     ).and_return(())
     ).and_return(())
     insert_execute_command_mock(('borg', 'check', 'repo'))
     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}
     consistency_config = {'check_last': check_last}
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_make_check_flags').with_args(
     flexmock(module).should_receive('_make_check_flags').with_args(
-        checks, check_last, None
+        checks, check_last, module.DEFAULT_PREFIX
     ).and_return(())
     ).and_return(())
     insert_execute_command_mock(('borg1', 'check', 'repo'))
     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}
     consistency_config = {'check_last': check_last}
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_make_check_flags').with_args(
     flexmock(module).should_receive('_make_check_flags').with_args(
-        checks, check_last, None
+        checks, check_last, module.DEFAULT_PREFIX
     ).and_return(())
     ).and_return(())
     insert_execute_command_mock(('borg', 'check', 'repo', '--remote-path', 'borg1'))
     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}
     consistency_config = {'check_last': check_last}
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_parse_checks').and_return(checks)
     flexmock(module).should_receive('_make_check_flags').with_args(
     flexmock(module).should_receive('_make_check_flags').with_args(
-        checks, check_last, None
+        checks, check_last, module.DEFAULT_PREFIX
     ).and_return(())
     ).and_return(())
     insert_execute_command_mock(('borg', 'check', 'repo', '--lock-wait', '5'))
     insert_execute_command_mock(('borg', 'check', 'repo', '--lock-wait', '5'))
 
 

+ 20 - 0
tests/unit/borg/test_prune.py

@@ -33,6 +33,26 @@ def test_make_prune_flags_accepts_prefix_with_placeholders():
     assert tuple(result) == expected
     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 = (
 PRUNE_COMMAND = (
     'borg',
     'borg',
     'prune',
     'prune',