[damned-lies] Decode output from run_shell_command



commit c5b979de0c0da66af192436dcad54d9c1146d0eb
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Feb 22 20:57:44 2017 +0100

    Decode output from run_shell_command

 stats/utils.py |   12 ++++--------
 stats/views.py |    2 +-
 2 files changed, 5 insertions(+), 9 deletions(-)
---
diff --git a/stats/utils.py b/stats/utils.py
index c433c8c..57904a0 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -132,24 +132,20 @@ def run_shell_command(cmd, input_data=None, raise_on_error=False, env=None, **po
     if env is not None:
         env = dict(os.environ, **env)
     shell = not isinstance(cmd, list)
-    if isinstance(cmd, str):
-        cmd = cmd.encode('utf-8')
-    elif isinstance(cmd, list):
-        cmd = [c.encode('utf-8') for c in cmd]
     pipe = Popen(cmd, shell=shell, stdin=stdin, stdout=PIPE, stderr=PIPE, env=env, **popen_kwargs)
     if input_data:
         try:
-            pipe.stdin.write(force_bytes(input_data))
+            pipe.stdin.write(input_data)
         except IOError as e:
             if e.errno != errno.EPIPE:
                 raise
-    (output, errout) = pipe.communicate()
+    output, errout = pipe.communicate()
     status = pipe.returncode
     logging.debug(output + errout)
     if raise_on_error and status != STATUS_OK:
         raise OSError(status, errout if errout else output)
 
-    return (status, output, errout)
+    return (status, output.decode('utf-8'), errout.decode('utf-8'))
 
 def check_program_presence(prog_name):
     """ Test if prog_name is an available command on the system """
@@ -504,7 +500,7 @@ def get_fig_stats(pofile, doc_format):
     if status != STATUS_OK:
         # FIXME: something should be logged here
         return []
-    lines = output.decode('utf-8').split('\n')
+    lines = output.split('\n')
     while lines[0][0] != "#":
         lines = lines[1:] # skip warning messages at the top of the output
 
diff --git a/stats/views.py b/stats/views.py
index fff21c4..2ccbc4e 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -243,7 +243,7 @@ def dynamic_po(request, module_name, domain, branch_name, filename):
 
     command = "msginit --locale=%s --no-translator --input=%s --output-file=-" % (locale, file_path)
     status, output, err = utils.run_shell_command(command, raise_on_error=True)
-    lines = output.decode('utf-8').split("\n")
+    lines = output.split("\n")
     skip_next_line = False
     for i, line in enumerate(lines):
         if skip_next_line or (line and line[0] == '#'):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]