2019-04-03 05:30:14 +00:00
|
|
|
import subprocess
|
|
|
|
|
2019-05-12 09:30:22 +00:00
|
|
|
from borgmatic.logger import get_logger
|
2019-04-03 05:30:14 +00:00
|
|
|
|
2019-05-12 09:30:22 +00:00
|
|
|
logger = get_logger(__name__)
|
2019-04-03 05:30:14 +00:00
|
|
|
|
|
|
|
|
2019-06-11 23:42:04 +00:00
|
|
|
def execute_and_log_output(full_command):
|
|
|
|
process = subprocess.Popen(full_command, stdout=None, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
while process.poll() is None:
|
|
|
|
line = process.stderr.readline().rstrip().decode()
|
|
|
|
if line.startswith('borg: error:'):
|
|
|
|
logger.error(line)
|
|
|
|
else:
|
|
|
|
logger.info(line)
|
|
|
|
|
|
|
|
logger.info(process.stderr.read().rstrip().decode())
|
|
|
|
exit_code = process.poll()
|
|
|
|
|
|
|
|
if exit_code != 0:
|
|
|
|
raise subprocess.CalledProcessError(exit_code, full_command)
|
|
|
|
|
|
|
|
|
2019-04-03 05:30:14 +00:00
|
|
|
def execute_command(full_command, capture_output=False):
|
|
|
|
'''
|
|
|
|
Execute the given command (a sequence of command/argument strings). If capture output is True,
|
|
|
|
then return the command's output as a string.
|
|
|
|
'''
|
|
|
|
logger.debug(' '.join(full_command))
|
|
|
|
|
|
|
|
if capture_output:
|
|
|
|
output = subprocess.check_output(full_command)
|
|
|
|
return output.decode() if output is not None else None
|
|
|
|
else:
|
2019-06-11 23:42:04 +00:00
|
|
|
execute_and_log_output(full_command)
|