Merge "Fix encoding issue during processing output"

This commit is contained in:
Zuul 2018-06-06 07:58:14 +00:00 committed by Gerrit Code Review
commit fc3748f49b

View File

@ -18,6 +18,7 @@ from __future__ import print_function
import argparse import argparse
import json import json
import locale
import os import os
import subprocess import subprocess
import sys import sys
@ -29,19 +30,20 @@ from collections import defaultdict
# if follow is set, output will be echoed to stdout # if follow is set, output will be echoed to stdout
def process_output(cmdline, follow=False): def process_output(cmdline, follow=False):
proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT)
universal_newlines=True)
if follow: if follow:
print("Running command: %s" % cmdline) print("Running command: %s" % cmdline)
out = "" out = ""
with proc.stdout: with proc.stdout:
for line in iter(proc.stdout.readline, ''): for line in iter(proc.stdout.readline, b''):
line = line.decode(encoding=locale.getpreferredencoding(False),
errors='backslashreplace')
out += line out += line
print("> %s" % line, end="") print("> %s" % line, end="")
proc.wait() proc.wait()
print("returncode: %d" % proc.returncode) print("returncode: %d" % proc.returncode)
else: else:
out = proc.communicate()[0] out = proc.communicate()[0].decode(errors='backslashreplace')
if proc.returncode: if proc.returncode:
e = subprocess.CalledProcessError(proc.returncode, cmdline) e = subprocess.CalledProcessError(proc.returncode, cmdline)