Browse Source

Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files, editor swap files, etc. (#77)

Dan Helfman 7 years ago
parent
commit
64364b20ff
4 changed files with 21 additions and 2 deletions
  1. 2 0
      NEWS
  2. 3 2
      borgmatic/config/collect.py
  3. 1 0
      borgmatic/config/validate.py
  4. 15 0
      borgmatic/tests/unit/config/test_collect.py

+ 2 - 0
NEWS

@@ -1,6 +1,8 @@
 1.2.1.dev0
 1.2.1.dev0
  * Skip before/after backup hooks when only doing --prune, --check, --list, and/or --info.
  * Skip before/after backup hooks when only doing --prune, --check, --list, and/or --info.
  * #38, #76: Upgrade ruamel.yaml compatibility version range and fix support for Python 3.7.
  * #38, #76: Upgrade ruamel.yaml compatibility version range and fix support for Python 3.7.
+ * #77: Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files,
+   editor swap files, etc.
 
 
 1.2.0
 1.2.0
  * #61: Support for Borg --list option via borgmatic command-line to list all archives.
  * #61: Support for Borg --list option via borgmatic command-line to list all archives.

+ 3 - 2
borgmatic/config/collect.py

@@ -12,7 +12,8 @@ def collect_config_filenames(config_paths):
     '''
     '''
     Given a sequence of config paths, both filenames and directories, resolve that to just an
     Given a sequence of config paths, both filenames and directories, resolve that to just an
     iterable of files. Accomplish this by listing any given directories looking for contained config
     iterable of files. Accomplish this by listing any given directories looking for contained config
-    files. This is non-recursive, so any directories within the given directories are ignored.
+    files (ending with the ".yaml" extension). This is non-recursive, so any directories within the
+    given directories are ignored.
 
 
     Return paths even if they don't exist on disk, so the user can find out about missing
     Return paths even if they don't exist on disk, so the user can find out about missing
     configuration paths. However, skip a default config path if it's missing, so the user doesn't
     configuration paths. However, skip a default config path if it's missing, so the user doesn't
@@ -32,5 +33,5 @@ def collect_config_filenames(config_paths):
 
 
         for filename in os.listdir(path):
         for filename in os.listdir(path):
             full_filename = os.path.join(path, filename)
             full_filename = os.path.join(path, filename)
-            if not os.path.isdir(full_filename):
+            if full_filename.endswith('.yaml') and not os.path.isdir(full_filename):
                 yield full_filename
                 yield full_filename

+ 1 - 0
borgmatic/config/validate.py

@@ -10,6 +10,7 @@ from ruamel import yaml
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
+
 def schema_filename():
 def schema_filename():
     '''
     '''
     Path to the installed YAML configuration schema file, used to validate and parse the
     Path to the installed YAML configuration schema file, used to validate and parse the

+ 15 - 0
borgmatic/tests/unit/config/test_collect.py

@@ -32,6 +32,21 @@ def test_collect_config_filenames_collects_files_from_given_directories_and_igno
     )
     )
 
 
 
 
+def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
+    config_paths = ('/etc/borgmatic.d',)
+    mock_path = flexmock(module.os.path)
+    mock_path.should_receive('exists').and_return(True)
+    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
+    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
+    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
+    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
+    flexmock(module.os).should_receive('listdir').and_return(['foo.yaml', 'bar.yaml~', 'baz.txt'])
+
+    config_filenames = tuple(module.collect_config_filenames(config_paths))
+
+    assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
+
+
 def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
 def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
     config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
     config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
     mock_path = flexmock(module.os.path)
     mock_path = flexmock(module.os.path)