hook.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. from borgmatic import execute
  3. from borgmatic.logger import get_logger
  4. logger = get_logger(__name__)
  5. def execute_hook(commands, config_filename, description, dry_run):
  6. '''
  7. Given a list of hook commands to execute, a config filename, a hook description, and whether
  8. this is a dry run, run the given commands. Or, don't run them if this is a dry run.
  9. '''
  10. if not commands:
  11. logger.debug('{}: No commands to run for {} hook'.format(config_filename, description))
  12. return
  13. dry_run_label = ' (dry run; not actually running hooks)' if dry_run else ''
  14. if len(commands) == 1:
  15. logger.info(
  16. '{}: Running command for {} hook{}'.format(config_filename, description, dry_run_label)
  17. )
  18. else:
  19. logger.info(
  20. '{}: Running {} commands for {} hook{}'.format(
  21. config_filename, len(commands), description, dry_run_label
  22. )
  23. )
  24. for command in commands:
  25. if not dry_run:
  26. execute.execute_command(
  27. [command],
  28. output_log_level=logging.ERROR if description == 'on-error' else logging.WARNING,
  29. shell=True,
  30. )