2019-06-12 21:11:36 +02:00
|
|
|
import logging
|
2019-10-23 01:28:42 +02:00
|
|
|
import os
|
2019-04-03 07:30:14 +02:00
|
|
|
import subprocess
|
|
|
|
|
2019-06-17 20:53:08 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-04-03 07:30:14 +02:00
|
|
|
|
|
|
|
|
2019-06-24 18:55:41 +02:00
|
|
|
ERROR_OUTPUT_MAX_LINE_COUNT = 25
|
2019-08-04 00:13:54 +02:00
|
|
|
BORG_ERROR_EXIT_CODE = 2
|
2019-06-24 18:55:41 +02:00
|
|
|
|
|
|
|
|
2019-10-31 20:57:36 +01:00
|
|
|
def exit_code_indicates_error(command, exit_code, error_on_warnings=False):
|
2019-10-23 01:28:42 +02:00
|
|
|
'''
|
2019-10-31 20:57:36 +01:00
|
|
|
Return True if the given exit code from running the command corresponds to an error.
|
2019-10-23 01:28:42 +02:00
|
|
|
'''
|
2019-10-31 20:57:36 +01:00
|
|
|
# If we're running something other than Borg, treat all non-zero exit codes as errors.
|
|
|
|
if 'borg' in command[0] and not error_on_warnings:
|
|
|
|
return bool(exit_code >= BORG_ERROR_EXIT_CODE)
|
|
|
|
|
|
|
|
return bool(exit_code != 0)
|
2019-10-23 01:28:42 +02:00
|
|
|
|
|
|
|
|
2019-11-08 20:17:52 +01:00
|
|
|
def log_output(command, process, output_buffer, output_log_level, error_on_warnings):
|
|
|
|
'''
|
|
|
|
Given a command already executed, its process opened by subprocess.Popen(), and the process'
|
|
|
|
relevant output buffer (stderr or stdout), log its output with the requested log level.
|
|
|
|
Additionally, raise a CalledProcessException if the process exits with an error (or a warning,
|
|
|
|
if error on warnings is True).
|
|
|
|
'''
|
2019-06-24 18:55:41 +02:00
|
|
|
last_lines = []
|
2019-06-12 01:42:04 +02:00
|
|
|
|
|
|
|
while process.poll() is None:
|
2019-11-08 20:17:52 +01:00
|
|
|
line = output_buffer.readline().rstrip().decode()
|
2019-06-13 05:56:20 +02:00
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
2019-06-24 18:55:41 +02:00
|
|
|
# Keep the last few lines of output in case the command errors, and we need the output for
|
|
|
|
# the exception below.
|
|
|
|
last_lines.append(line)
|
|
|
|
if len(last_lines) > ERROR_OUTPUT_MAX_LINE_COUNT:
|
|
|
|
last_lines.pop(0)
|
|
|
|
|
|
|
|
logger.log(output_log_level, line)
|
2019-06-12 01:42:04 +02:00
|
|
|
|
2019-11-08 20:17:52 +01:00
|
|
|
remaining_output = output_buffer.read().rstrip().decode()
|
2019-06-14 06:38:06 +02:00
|
|
|
if remaining_output: # pragma: no cover
|
2019-06-13 19:48:21 +02:00
|
|
|
logger.log(output_log_level, remaining_output)
|
2019-06-12 01:42:04 +02:00
|
|
|
|
2019-06-12 20:49:35 +02:00
|
|
|
exit_code = process.poll()
|
2019-09-13 01:37:43 +02:00
|
|
|
|
2019-11-08 20:17:52 +01:00
|
|
|
if exit_code_indicates_error(command, exit_code, error_on_warnings):
|
2019-06-24 18:55:41 +02:00
|
|
|
# If an error occurs, include its output in the raised exception so that we don't
|
|
|
|
# inadvertently hide error output.
|
|
|
|
if len(last_lines) == ERROR_OUTPUT_MAX_LINE_COUNT:
|
|
|
|
last_lines.insert(0, '...')
|
|
|
|
|
2019-11-08 20:17:52 +01:00
|
|
|
raise subprocess.CalledProcessError(exit_code, ' '.join(command), '\n'.join(last_lines))
|
2019-06-12 01:42:04 +02:00
|
|
|
|
|
|
|
|
2019-10-23 01:28:42 +02:00
|
|
|
def execute_command(
|
2019-10-31 20:57:36 +01:00
|
|
|
full_command,
|
|
|
|
output_log_level=logging.INFO,
|
2019-11-08 20:17:52 +01:00
|
|
|
output_file=None,
|
2019-11-12 06:59:30 +01:00
|
|
|
input_file=None,
|
2019-10-31 20:57:36 +01:00
|
|
|
shell=False,
|
|
|
|
extra_environment=None,
|
|
|
|
working_directory=None,
|
|
|
|
error_on_warnings=False,
|
2019-10-23 01:28:42 +02:00
|
|
|
):
|
2019-04-03 07:30:14 +02:00
|
|
|
'''
|
2019-06-12 21:11:36 +02:00
|
|
|
Execute the given command (a sequence of command/argument strings) and log its output at the
|
2019-11-08 20:17:52 +01:00
|
|
|
given log level. If output log level is None, instead capture and return the output. If an
|
|
|
|
open output file object is given, then write stdout to the file and only log stderr (but only
|
2019-11-12 06:59:30 +01:00
|
|
|
if an output log level is set). If an open input file object is given, then read stdin from the
|
|
|
|
file. If shell is True, execute the command within a shell. If an extra environment dict is
|
|
|
|
given, then use it to augment the current environment, and pass the result into the command. If
|
|
|
|
a working directory is given, use that as the present working directory when running the
|
|
|
|
command.
|
2019-09-29 01:18:10 +02:00
|
|
|
|
|
|
|
Raise subprocesses.CalledProcessError if an error occurs while running the command.
|
2019-04-03 07:30:14 +02:00
|
|
|
'''
|
2019-11-12 18:50:26 +01:00
|
|
|
logger.debug(
|
|
|
|
' '.join(full_command)
|
|
|
|
+ (' < {}'.format(input_file.name) if input_file else '')
|
|
|
|
+ (' > {}'.format(output_file.name) if output_file else '')
|
|
|
|
)
|
2019-10-23 01:28:42 +02:00
|
|
|
environment = {**os.environ, **extra_environment} if extra_environment else None
|
2019-04-03 07:30:14 +02:00
|
|
|
|
2019-06-12 21:11:36 +02:00
|
|
|
if output_log_level is None:
|
2019-10-31 20:57:36 +01:00
|
|
|
output = subprocess.check_output(
|
|
|
|
full_command, shell=shell, env=environment, cwd=working_directory
|
|
|
|
)
|
2019-04-03 07:30:14 +02:00
|
|
|
return output.decode() if output is not None else None
|
|
|
|
else:
|
2019-11-08 20:17:52 +01:00
|
|
|
process = subprocess.Popen(
|
2019-10-31 20:57:36 +01:00
|
|
|
full_command,
|
2019-11-12 06:59:30 +01:00
|
|
|
stdin=input_file,
|
2019-11-08 20:17:52 +01:00
|
|
|
stdout=output_file or subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE if output_file else subprocess.STDOUT,
|
2019-10-31 20:57:36 +01:00
|
|
|
shell=shell,
|
2019-11-08 20:17:52 +01:00
|
|
|
env=environment,
|
|
|
|
cwd=working_directory,
|
|
|
|
)
|
|
|
|
log_output(
|
|
|
|
full_command,
|
|
|
|
process,
|
|
|
|
process.stderr if output_file else process.stdout,
|
|
|
|
output_log_level,
|
|
|
|
error_on_warnings,
|
2019-10-31 20:57:36 +01:00
|
|
|
)
|
2019-09-25 21:03:10 +02:00
|
|
|
|
|
|
|
|
2019-10-31 20:57:36 +01:00
|
|
|
def execute_command_without_capture(full_command, working_directory=None, error_on_warnings=False):
|
2019-09-25 21:03:10 +02:00
|
|
|
'''
|
|
|
|
Execute the given command (a sequence of command/argument strings), but don't capture or log its
|
|
|
|
output in any way. This is necessary for commands that monkey with the terminal (e.g. progress
|
|
|
|
display) or provide interactive prompts.
|
2019-10-31 20:57:36 +01:00
|
|
|
|
|
|
|
If a working directory is given, use that as the present working directory when running the
|
|
|
|
command.
|
2019-09-25 21:03:10 +02:00
|
|
|
'''
|
|
|
|
logger.debug(' '.join(full_command))
|
|
|
|
|
|
|
|
try:
|
2019-10-31 20:57:36 +01:00
|
|
|
subprocess.check_call(full_command, cwd=working_directory)
|
2019-09-25 21:03:10 +02:00
|
|
|
except subprocess.CalledProcessError as error:
|
2019-10-31 20:57:36 +01:00
|
|
|
if exit_code_indicates_error(full_command, error.returncode, error_on_warnings):
|
2019-09-25 21:03:10 +02:00
|
|
|
raise
|