"""
stop_on_error = kw.pop('stop_on_error', True)
command_msg = "Running command: %s" % ' '.join(command)
+ stdin = kw.pop('stdin', None)
logger.info(command_msg)
terminal.write(command_msg)
- terminal_logging = kw.get('terminal_logging', True)
+ terminal_logging = kw.pop('terminal_logging', True)
process = subprocess.Popen(
command,
+ stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
**kw
)
+ if stdin:
+ process.communicate(stdin)
while True:
reads, _, _ = select(
[process.stdout.fileno(), process.stderr.fileno()],
Useful when system calls are needed to act on output, and that same output
shouldn't get displayed on the terminal.
- :param terminal_logging: Log command to terminal, defaults to False
+ :param terminal_verbose: Log command output to terminal, defaults to False, and
+ it is forcefully set to True if a return code is non-zero
"""
- terminal_logging = kw.pop('terminal_logging', False)
+ terminal_verbose = kw.pop('terminal_verbose', False)
command_msg = "Running command: %s" % ' '.join(command)
+ stdin = kw.pop('stdin', None)
logger.info(command_msg)
- if terminal_logging:
- terminal.write(command_msg)
+ terminal.write(command_msg)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
+ stdin=subprocess.PIPE,
close_fds=True,
**kw
)
-
+ if stdin:
+ stdout_stream, stderr_stream = process.communicate(stdin)
+ else:
+ stdout_stream = process.stdout.read()
+ stderr_stream = process.stderr.read()
returncode = process.wait()
- stdout_stream = process.stdout.read()
- stderr_stream = process.stderr.read()
if not isinstance(stdout_stream, str):
stdout_stream = stdout_stream.decode('utf-8')
if not isinstance(stderr_stream, str):
stdout = stdout_stream.splitlines()
stderr = stderr_stream.splitlines()
+ if returncode != 0:
+ # set to true so that we can log the stderr/stdout that callers would
+ # do anyway
+ terminal_verbose = True
+
# the following can get a messed up order in the log if the system call
# returns output with both stderr and stdout intermingled. This separates
# that.
for line in stdout:
- log_output('stdout', line, False)
+ log_output('stdout', line, terminal_verbose)
for line in stderr:
- log_output('stderr', line, False)
+ log_output('stderr', line, terminal_verbose)
return stdout, stderr, returncode