from subprocess import Popen, PIPE
-def call_and_capture(command, stdin_data=None, *args, **kwargs):
+def call_and_capture(command, stdin_data=None, split_lines=True,
+ *args, **kwargs):
""" call command, captures stdout, stderr and return code, return when done
Use only for commands with little output since all output has to be
:param command: forwarded as first arg to Popen constructor
:param str stdin_data: forwarded to stdin of process through communicate
+ :param bool split_lines: True (default) to split output line-wise and
+ return list of strings; False --> return single
+ string for out and one for err
:param args: forwarded to Popen constructor
:param kwargs: forwarded to Popen constructor
:returns: (return_code, stdout, stderr); stdout and stderr are lists of
text lines (without terminating newlines); if universal_newlines is
True (default), the lines are of type str, otherwise they are non-
- unicode text (type str in py2, bytes in py3)
+ unicode text (type str in py2, bytes in py3). If split_lines is False
+ (not default), then stdout and stderr are single multi-line strings
:raise: OSError (e.g., if command does not exist), ValueError if args are
invalid; no :py:class:`subprocess.CalledProcessError` nor
stdout_data, stderr_data = proc.communicate(stdin_data)
# return
- return proc.returncode, stdout_data.splitlines(), stderr_data.splitlines()
+ if split_lines:
+ return proc.returncode, stdout_data.splitlines(), \
+ stderr_data.splitlines()
+ else:
+ return proc.returncode, stdout_data, stderr_data