2020-05-07 20:44:04 +02:00
|
|
|
import collections
|
2019-06-12 21:11:36 +02:00
|
|
|
import logging
|
2019-10-23 01:28:42 +02:00
|
|
|
import os
|
2020-05-07 20:44:04 +02:00
|
|
|
import select
|
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
|
|
|
|
|
|
|
|
2020-05-15 07:38:38 +02:00
|
|
|
def exit_code_indicates_error(process, exit_code, borg_local_path=None):
|
2019-10-23 01:28:42 +02:00
|
|
|
'''
|
2020-05-15 07:38:38 +02:00
|
|
|
Return True if the given exit code from running a command corresponds to an error. If a Borg
|
|
|
|
local path is given and matches the process' command, then treat exit code 1 as a warning
|
|
|
|
instead of an error.
|
2019-10-23 01:28:42 +02:00
|
|
|
'''
|
2020-05-15 08:04:01 +02:00
|
|
|
if exit_code is None:
|
|
|
|
return False
|
|
|
|
|
2020-05-15 07:38:38 +02:00
|
|
|
command = process.args.split(' ') if isinstance(process.args, str) else process.args
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2020-05-15 07:38:38 +02:00
|
|
|
if borg_local_path and command[0] == borg_local_path:
|
|
|
|
return bool(exit_code >= BORG_ERROR_EXIT_CODE)
|
|
|
|
|
|
|
|
return bool(exit_code != 0)
|
2019-10-23 01:28:42 +02:00
|
|
|
|
|
|
|
|
2020-05-11 20:17:24 +02:00
|
|
|
def command_for_process(process):
|
2019-11-08 20:17:52 +01:00
|
|
|
'''
|
2020-05-07 20:44:04 +02:00
|
|
|
Given a process as an instance of subprocess.Popen, return the command string that was used to
|
|
|
|
invoke it.
|
|
|
|
'''
|
|
|
|
return process.args if isinstance(process.args, str) else ' '.join(process.args)
|
|
|
|
|
|
|
|
|
|
|
|
def output_buffer_for_process(process, exclude_stdouts):
|
|
|
|
'''
|
2020-05-11 20:17:24 +02:00
|
|
|
Given a process as an instance of subprocess.Popen and a sequence of stdouts to exclude, return
|
|
|
|
either the process's stdout or stderr. The idea is that if stdout is excluded for a process, we
|
|
|
|
still have stderr to log.
|
2020-05-07 20:44:04 +02:00
|
|
|
'''
|
|
|
|
return process.stderr if process.stdout in exclude_stdouts else process.stdout
|
|
|
|
|
|
|
|
|
2020-05-15 07:38:38 +02:00
|
|
|
def log_outputs(processes, exclude_stdouts, output_log_level, borg_local_path):
|
2020-05-07 20:44:04 +02:00
|
|
|
'''
|
|
|
|
Given a sequence of subprocess.Popen() instances for multiple processes, log the output for each
|
|
|
|
process with the requested log level. Additionally, raise a CalledProcessError if a process
|
2020-05-15 07:38:38 +02:00
|
|
|
exits with an error (or a warning for exit code 1, if that process matches the Borg local path).
|
2020-05-07 20:44:04 +02:00
|
|
|
|
|
|
|
For simplicity, it's assumed that the output buffer for each process is its stdout. But if any
|
|
|
|
stdouts are given to exclude, then for any matching processes, log from their stderr instead.
|
2020-05-13 00:49:49 +02:00
|
|
|
|
|
|
|
Note that stdout for a process can be None if output is intentionally not captured. In which
|
|
|
|
case it won't be logged.
|
2020-05-07 20:44:04 +02:00
|
|
|
'''
|
|
|
|
# Map from output buffer to sequence of last lines.
|
|
|
|
buffer_last_lines = collections.defaultdict(list)
|
2020-05-10 06:53:16 +02:00
|
|
|
output_buffers = [
|
|
|
|
output_buffer_for_process(process, exclude_stdouts)
|
|
|
|
for process in processes
|
|
|
|
if process.stdout or process.stderr
|
|
|
|
]
|
2020-05-07 20:44:04 +02:00
|
|
|
|
2020-05-13 00:49:49 +02:00
|
|
|
# Log output for each process until they all exit.
|
2020-05-07 20:44:04 +02:00
|
|
|
while True:
|
2020-05-15 08:04:01 +02:00
|
|
|
if output_buffers:
|
|
|
|
(ready_buffers, _, _) = select.select(output_buffers, [], [])
|
|
|
|
|
|
|
|
for ready_buffer in ready_buffers:
|
|
|
|
line = ready_buffer.readline().rstrip().decode()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Keep the last few lines of output in case the process errors, and we need the output for
|
|
|
|
# the exception below.
|
|
|
|
last_lines = buffer_last_lines[ready_buffer]
|
|
|
|
last_lines.append(line)
|
|
|
|
if len(last_lines) > ERROR_OUTPUT_MAX_LINE_COUNT:
|
|
|
|
last_lines.pop(0)
|
|
|
|
|
|
|
|
logger.log(output_log_level, line)
|
|
|
|
|
|
|
|
for process in processes:
|
|
|
|
exit_code = process.poll() if output_buffers else process.wait()
|
|
|
|
|
|
|
|
# If any process errors, then raise accordingly.
|
|
|
|
if exit_code_indicates_error(process, exit_code, borg_local_path):
|
|
|
|
# If an error occurs, include its output in the raised exception so that we don't
|
|
|
|
# inadvertently hide error output.
|
|
|
|
output_buffer = output_buffer_for_process(process, exclude_stdouts)
|
|
|
|
|
|
|
|
last_lines = buffer_last_lines[output_buffer] if output_buffer else []
|
|
|
|
if len(last_lines) == ERROR_OUTPUT_MAX_LINE_COUNT:
|
|
|
|
last_lines.insert(0, '...')
|
|
|
|
|
|
|
|
# Something has gone wrong. So vent each process' output buffer to prevent it from
|
|
|
|
# hanging. And then kill the process.
|
|
|
|
for other_process in processes:
|
|
|
|
if other_process.poll() is None:
|
|
|
|
other_process.stdout.read(0)
|
|
|
|
other_process.kill()
|
|
|
|
|
|
|
|
raise subprocess.CalledProcessError(
|
|
|
|
exit_code, command_for_process(process), '\n'.join(last_lines)
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
|
|
|
|
if all(process.poll() is not None for process in processes):
|
|
|
|
break
|
|
|
|
|
2020-05-13 00:49:49 +02:00
|
|
|
# Consume any remaining output that we missed (if any).
|
2020-05-07 20:44:04 +02:00
|
|
|
for process in processes:
|
2020-05-10 06:53:16 +02:00
|
|
|
output_buffer = output_buffer_for_process(process, exclude_stdouts)
|
|
|
|
|
|
|
|
if not output_buffer:
|
|
|
|
continue
|
|
|
|
|
|
|
|
remaining_output = output_buffer.read().rstrip().decode()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
if remaining_output: # pragma: no cover
|
|
|
|
logger.log(output_log_level, remaining_output)
|
|
|
|
|
|
|
|
|
|
|
|
def log_command(full_command, input_file, output_file):
|
|
|
|
'''
|
|
|
|
Log the given command (a sequence of command/argument strings), along with its input/output file
|
|
|
|
paths.
|
|
|
|
'''
|
|
|
|
logger.debug(
|
|
|
|
' '.join(full_command)
|
|
|
|
+ (' < {}'.format(getattr(input_file, 'name', '')) if input_file else '')
|
|
|
|
+ (' > {}'.format(getattr(output_file, 'name', '')) if output_file else '')
|
|
|
|
)
|
2019-06-12 01:42:04 +02:00
|
|
|
|
|
|
|
|
2020-05-13 00:49:49 +02:00
|
|
|
# An sentinel passed as an output file to execute_command() to indicate that the command's output
|
|
|
|
# should be allowed to flow through to stdout without being captured for logging. Useful for
|
|
|
|
# commands with interactive prompts or those that mess directly with the console.
|
2020-05-10 06:53:16 +02:00
|
|
|
DO_NOT_CAPTURE = object()
|
|
|
|
|
|
|
|
|
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,
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=True,
|
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
|
2020-05-07 20:44:04 +02:00
|
|
|
given log level. If output log level is None, instead capture and return the output. (Implies
|
|
|
|
run_to_completion.) If an open output file object is given, then write stdout to the file and
|
|
|
|
only log stderr (but only 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
|
2020-05-15 07:38:38 +02:00
|
|
|
when running the command. If a Borg local path is given, and the command matches it (regardless
|
|
|
|
of arguments), treat exit code 1 as a warning instead of an error. If run to completion is
|
|
|
|
False, then return the process for the command without executing it to completion.
|
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
|
|
|
'''
|
2020-05-07 20:44:04 +02:00
|
|
|
log_command(full_command, input_file, output_file)
|
2019-10-23 01:28:42 +02:00
|
|
|
environment = {**os.environ, **extra_environment} if extra_environment else None
|
2020-05-10 06:53:16 +02:00
|
|
|
do_not_capture = bool(output_file is DO_NOT_CAPTURE)
|
2020-05-13 00:49:49 +02:00
|
|
|
command = ' '.join(full_command) if shell else full_command
|
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(
|
2020-05-13 00:49:49 +02:00
|
|
|
command, shell=shell, env=environment, cwd=working_directory
|
2019-10-31 20:57:36 +01:00
|
|
|
)
|
2019-04-03 07:30:14 +02:00
|
|
|
return output.decode() if output is not None else None
|
2019-09-25 21:03:10 +02:00
|
|
|
|
2020-05-10 06:53:16 +02:00
|
|
|
process = subprocess.Popen(
|
2020-05-13 00:49:49 +02:00
|
|
|
command,
|
2020-05-10 06:53:16 +02:00
|
|
|
stdin=input_file,
|
|
|
|
stdout=None if do_not_capture else (output_file or subprocess.PIPE),
|
|
|
|
stderr=None if do_not_capture else (subprocess.PIPE if output_file else subprocess.STDOUT),
|
|
|
|
shell=shell,
|
|
|
|
env=environment,
|
|
|
|
cwd=working_directory,
|
|
|
|
)
|
|
|
|
if not run_to_completion:
|
|
|
|
return process
|
2019-09-25 21:03:10 +02:00
|
|
|
|
2020-05-15 07:38:38 +02:00
|
|
|
log_outputs(
|
|
|
|
(process,), (input_file, output_file), output_log_level, borg_local_path=borg_local_path
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def execute_command_with_processes(
|
|
|
|
full_command,
|
|
|
|
processes,
|
|
|
|
output_log_level=logging.INFO,
|
|
|
|
output_file=None,
|
|
|
|
input_file=None,
|
|
|
|
shell=False,
|
|
|
|
extra_environment=None,
|
|
|
|
working_directory=None,
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
):
|
|
|
|
'''
|
|
|
|
Execute the given command (a sequence of command/argument strings) and log its output at the
|
|
|
|
given log level. Simultaneously, continue to poll one or more active processes so that they
|
|
|
|
run as well. This is useful, for instance, for processes that are streaming output to a named
|
|
|
|
pipe that the given command is consuming from.
|
|
|
|
|
|
|
|
If an open output file object is given, then write stdout to the file and only log stderr (but
|
|
|
|
only 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
|
2020-05-15 07:38:38 +02:00
|
|
|
command. If a Borg local path is given, then for any matching command or process (regardless of
|
|
|
|
arguments), treat exit code 1 as a warning instead of an error.
|
2020-05-07 20:44:04 +02:00
|
|
|
|
|
|
|
Raise subprocesses.CalledProcessError if an error occurs while running the command or in the
|
|
|
|
upstream process.
|
|
|
|
'''
|
|
|
|
log_command(full_command, input_file, output_file)
|
|
|
|
environment = {**os.environ, **extra_environment} if extra_environment else None
|
2020-05-10 06:53:16 +02:00
|
|
|
do_not_capture = bool(output_file is DO_NOT_CAPTURE)
|
2020-05-13 00:49:49 +02:00
|
|
|
command = ' '.join(full_command) if shell else full_command
|
2020-05-07 20:44:04 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
command_process = subprocess.Popen(
|
2020-05-13 00:49:49 +02:00
|
|
|
command,
|
2020-05-07 20:44:04 +02:00
|
|
|
stdin=input_file,
|
2020-05-10 06:53:16 +02:00
|
|
|
stdout=None if do_not_capture else (output_file or subprocess.PIPE),
|
|
|
|
stderr=None
|
|
|
|
if do_not_capture
|
|
|
|
else (subprocess.PIPE if output_file else subprocess.STDOUT),
|
2020-05-07 20:44:04 +02:00
|
|
|
shell=shell,
|
|
|
|
env=environment,
|
|
|
|
cwd=working_directory,
|
|
|
|
)
|
|
|
|
except (subprocess.CalledProcessError, OSError):
|
|
|
|
# Something has gone wrong. So vent each process' output buffer to prevent it from hanging.
|
|
|
|
# And then kill the process.
|
|
|
|
for process in processes:
|
|
|
|
if process.poll() is None:
|
|
|
|
process.stdout.read(0)
|
|
|
|
process.kill()
|
|
|
|
raise
|
|
|
|
|
2020-05-11 19:55:50 +02:00
|
|
|
log_outputs(
|
2020-05-07 20:44:04 +02:00
|
|
|
tuple(processes) + (command_process,),
|
|
|
|
(input_file, output_file),
|
|
|
|
output_log_level,
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path=borg_local_path,
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|