utils: decode command output in runcmd() as UTF-8

Sometimes we get back UTF-8 characters (particularly when picking up
names from git commits), and the ascii codec will error out if that
happens, so switch over to utf-8.

We can as a result remove the decode() calls from the bulk change view.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2018-03-01 08:26:57 +13:00
parent edb1261d2b
commit aad000734c
2 changed files with 3 additions and 4 deletions

View File

@ -264,7 +264,7 @@ def runcmd(cmd, destdir=None, printerr=True, logger=None):
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
out.seek(0) out.seek(0)
output = out.read() output = out.read()
output = output.decode('ascii').strip() output = output.decode('utf-8', errors='replace').strip()
if printerr: if printerr:
if logger: if logger:
logger.error("%s" % output) logger.error("%s" % output)
@ -275,7 +275,7 @@ def runcmd(cmd, destdir=None, printerr=True, logger=None):
out.seek(0) out.seek(0)
output = out.read() output = out.read()
output = output.decode('ascii').strip() output = output.decode('utf-8', errors='replace').strip()
if logger: if logger:
logger.debug("output: %s" % output.rstrip() ) logger.debug("output: %s" % output.rstrip() )
return output return output

View File

@ -224,7 +224,7 @@ def bulk_change_patch_view(request, pk):
try: try:
ret = utils.runcmd('%s bulkchange.py %d %s' % (sys.executable, int(pk), settings.TEMP_BASE_DIR), os.path.dirname(__file__)) ret = utils.runcmd('%s bulkchange.py %d %s' % (sys.executable, int(pk), settings.TEMP_BASE_DIR), os.path.dirname(__file__))
if ret: if ret:
fn = ret.splitlines()[-1].decode('utf-8') fn = ret.splitlines()[-1]
if os.path.exists(fn): if os.path.exists(fn):
if fn.endswith('.tar.gz'): if fn.endswith('.tar.gz'):
mimetype = 'application/x-gzip' mimetype = 'application/x-gzip'
@ -241,7 +241,6 @@ def bulk_change_patch_view(request, pk):
except Exception as e: except Exception as e:
output = getattr(e, 'output', None) output = getattr(e, 'output', None)
if output: if output:
output = output.decode('utf-8', errors="ignore")
if 'timeout' in output: if 'timeout' in output:
return HttpResponse('Failed to generate patches: timed out waiting for lock. Please try again shortly.', content_type='text/plain') return HttpResponse('Failed to generate patches: timed out waiting for lock. Please try again shortly.', content_type='text/plain')
else: else: