From aad000734c8ae9bd1cca311469c62bae8a1aa1cd Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Thu, 1 Mar 2018 08:26:57 +1300 Subject: [PATCH] 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 --- layerindex/utils.py | 4 ++-- layerindex/views.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/layerindex/utils.py b/layerindex/utils.py index 093ba14..db5bfc6 100644 --- a/layerindex/utils.py +++ b/layerindex/utils.py @@ -264,7 +264,7 @@ def runcmd(cmd, destdir=None, printerr=True, logger=None): except subprocess.CalledProcessError as e: out.seek(0) output = out.read() - output = output.decode('ascii').strip() + output = output.decode('utf-8', errors='replace').strip() if printerr: if logger: logger.error("%s" % output) @@ -275,7 +275,7 @@ def runcmd(cmd, destdir=None, printerr=True, logger=None): out.seek(0) output = out.read() - output = output.decode('ascii').strip() + output = output.decode('utf-8', errors='replace').strip() if logger: logger.debug("output: %s" % output.rstrip() ) return output diff --git a/layerindex/views.py b/layerindex/views.py index bc3cddf..74cccd5 100644 --- a/layerindex/views.py +++ b/layerindex/views.py @@ -224,7 +224,7 @@ def bulk_change_patch_view(request, pk): try: ret = utils.runcmd('%s bulkchange.py %d %s' % (sys.executable, int(pk), settings.TEMP_BASE_DIR), os.path.dirname(__file__)) if ret: - fn = ret.splitlines()[-1].decode('utf-8') + fn = ret.splitlines()[-1] if os.path.exists(fn): if fn.endswith('.tar.gz'): mimetype = 'application/x-gzip' @@ -241,7 +241,6 @@ def bulk_change_patch_view(request, pk): except Exception as e: output = getattr(e, 'output', None) if output: - output = output.decode('utf-8', errors="ignore") if 'timeout' in output: return HttpResponse('Failed to generate patches: timed out waiting for lock. Please try again shortly.', content_type='text/plain') else: