2017-10-26 04:38:27 +00:00
|
|
|
import subprocess
|
|
|
|
|
2019-05-12 09:30:22 +00:00
|
|
|
from borgmatic.logger import get_logger
|
2017-10-26 04:38:27 +00:00
|
|
|
|
2019-05-12 09:30:22 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
2017-10-26 05:32:06 +00:00
|
|
|
|
|
|
|
|
2019-05-07 23:06:31 +00:00
|
|
|
def execute_hook(commands, config_filename, description, dry_run):
|
|
|
|
'''
|
|
|
|
|
|
|
|
Given a list of hook commands to execute, a config filename, a hook description, and whether
|
|
|
|
this is a dry run, run the given commands. Or, don't run them if this is a dry run.
|
|
|
|
'''
|
2017-10-26 05:32:06 +00:00
|
|
|
if not commands:
|
|
|
|
logger.debug('{}: No commands to run for {} hook'.format(config_filename, description))
|
|
|
|
return
|
|
|
|
|
2019-05-07 23:06:31 +00:00
|
|
|
dry_run_label = ' (dry run; not actually running hooks)' if dry_run else ''
|
|
|
|
|
2017-10-26 05:32:06 +00:00
|
|
|
if len(commands) == 1:
|
2019-05-07 23:06:31 +00:00
|
|
|
logger.info(
|
|
|
|
'{}: Running command for {} hook{}'.format(config_filename, description, dry_run_label)
|
|
|
|
)
|
2017-10-26 05:32:06 +00:00
|
|
|
else:
|
2018-09-30 05:45:00 +00:00
|
|
|
logger.info(
|
2019-05-07 23:06:31 +00:00
|
|
|
'{}: Running {} commands for {} hook{}'.format(
|
|
|
|
config_filename, len(commands), description, dry_run_label
|
2018-09-30 05:45:00 +00:00
|
|
|
)
|
|
|
|
)
|
2017-10-26 05:32:06 +00:00
|
|
|
|
|
|
|
for command in commands:
|
|
|
|
logger.debug('{}: Hook command: {}'.format(config_filename, command))
|
2019-05-07 23:06:31 +00:00
|
|
|
if not dry_run:
|
|
|
|
subprocess.check_call(command, shell=True)
|