def call(command, **kw): """ Similar to ``subprocess.Popen`` with the following changes: * returns stdout, stderr, and exit code (vs. just the exit code) * logs the full contents of stderr and stdout (separately) to the file log By default, no terminal output is given, not even the command that is going to run. Useful when system calls are needed to act on output, and that same output shouldn't get displayed on the terminal. Optionally, the command can be displayed on the terminal and the log file, and log file output can be turned off. This is useful to prevent sensitive output going to stderr/stdout and being captured on a log file. :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 :param logfile_verbose: Log stderr/stdout output to log file. Defaults to True :param verbose_on_failure: On a non-zero exit status, it will forcefully set logging ON for the terminal. Defaults to True """ executable = which(command.pop(0)) command.insert(0, executable) terminal_verbose = kw.pop('terminal_verbose', False) logfile_verbose = kw.pop('logfile_verbose', True) verbose_on_failure = kw.pop('verbose_on_failure', True) show_command = kw.pop('show_command', False) command_msg = "Running command: %s" % ' '.join(command) stdin = kw.pop('stdin', None) logger.info(command_msg) if show_command: 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(as_bytes(stdin)) else: stdout_stream = process.stdout.read() stderr_stream = process.stderr.read() returncode = process.wait() if not isinstance(stdout_stream, str): stdout_stream = stdout_stream.decode('utf-8') if not isinstance(stderr_stream, str): stderr_stream = stderr_stream.decode('utf-8') 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 as long as verbose_on_failure is set (defaults to True) if verbose_on_failure: terminal_verbose = True # logfiles aren't disruptive visually, unlike the terminal, so this # should always be on when there is a failure logfile_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, terminal_verbose, logfile_verbose) for line in stderr: log_output('stderr', line, terminal_verbose, logfile_verbose) return stdout, stderr, returncode
def test_bytes_just_gets_returned(self): bytes_string = "contents".encode('utf-8') assert util.as_bytes(bytes_string) == bytes_string
def test_string_gets_converted_to_bytes(self): result = util.as_bytes('contents') assert isinstance(result, bytes)
def call(command, **kw): """ Similar to ``subprocess.Popen`` with the following changes: * returns stdout, stderr, and exit code (vs. just the exit code) * logs the full contents of stderr and stdout (separately) to the file log By default, no terminal output is given, not even the command that is going to run. Useful when system calls are needed to act on output, and that same output shouldn't get displayed on the terminal. Optionally, the command can be displayed on the terminal and the log file, and log file output can be turned off. This is useful to prevent sensitive output going to stderr/stdout and being captured on a log file. :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 :param logfile_verbose: Log stderr/stdout output to log file. Defaults to True :param verbose_on_failure: On a non-zero exit status, it will forcefully set logging ON for the terminal. Defaults to True """ executable = which(command.pop(0)) command.insert(0, executable) terminal_verbose = kw.pop('terminal_verbose', False) logfile_verbose = kw.pop('logfile_verbose', True) verbose_on_failure = kw.pop('verbose_on_failure', True) show_command = kw.pop('show_command', False) command_msg = "Running command: %s" % ' '.join(command) stdin = kw.pop('stdin', None) logger.info(command_msg) if show_command: 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(as_bytes(stdin)) else: stdout_stream = process.stdout.read() stderr_stream = process.stderr.read() returncode = process.wait() if not isinstance(stdout_stream, str): stdout_stream = stdout_stream.decode('utf-8') if not isinstance(stderr_stream, str): stderr_stream = stderr_stream.decode('utf-8') 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 as long as verbose_on_failure is set (defaults to True) if verbose_on_failure: terminal_verbose = True # logfiles aren't disruptive visually, unlike the terminal, so this # should always be on when there is a failure logfile_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, terminal_verbose, logfile_verbose) for line in stderr: log_output('stderr', line, terminal_verbose, logfile_verbose) return stdout, stderr, returncode