collect.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. DEFAULT_CONFIG_PATHS = [
  3. '/etc/borgmatic/config.yaml',
  4. '/etc/borgmatic.d',
  5. os.path.expanduser('~/.config/borgmatic/config.yaml'),
  6. ]
  7. def collect_config_filenames(config_paths):
  8. '''
  9. Given a sequence of config paths, both filenames and directories, resolve that to just an
  10. iterable of files. Accomplish this by listing any given directories looking for contained config
  11. files. This is non-recursive, so any directories within the given directories are ignored.
  12. Return paths even if they don't exist on disk, so the user can find out about missing
  13. configuration paths. However, skip a default config path if it's missing, so the user doesn't
  14. have to create a default config path unless they need it.
  15. '''
  16. real_default_config_paths = set(map(os.path.realpath, DEFAULT_CONFIG_PATHS))
  17. for path in config_paths:
  18. exists = os.path.exists(path)
  19. if os.path.realpath(path) in real_default_config_paths and not exists:
  20. continue
  21. if not os.path.isdir(path) or not exists:
  22. yield path
  23. continue
  24. for filename in os.listdir(path):
  25. full_filename = os.path.join(path, filename)
  26. if not os.path.isdir(full_filename):
  27. yield full_filename