def call_output(cmd, stdin=None, encoding_errors="replace", **kwargs): """Run command and read output.""" p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) stdout, stderr, retcode = [], [], None while retcode is None: if stdin is not None: logger.log_prefix("<0 ", stdin.rstrip()) raw_out, raw_err = p.communicate(stdin) stdin = None out = raw_out.decode(get_encoding(sys.stdout), encoding_errors) if raw_out else "" if out: logger.log_prefix("1> ", out.rstrip()) stdout.append(out) err = raw_err.decode(get_encoding(sys.stderr), encoding_errors) if raw_err else "" if err: logger.log_prefix("2> ", err.rstrip()) stderr.append(err) retcode = p.poll() return stdout, stderr, retcode