Bug 1377216 - Accept environment variables to check_cmd_output; r?glandium draft
authorGregory Szorc <gps@mozilla.com>
Tue, 18 Jul 2017 18:06:03 -0700
changeset 610964 212f03efaa5d4a10f288ba69711d99f934225949
parent 610942 c84fee99ef97481f241c478c58453df2ece03bdd
child 610965 e50d6f4d3a8123bc62fbcdcb9252d4a1d1d36d0e
push id69070
push userbmo:gps@mozilla.com
push dateWed, 19 Jul 2017 01:11:03 +0000
reviewersglandium
bugs1377216
milestone56.0a1
Bug 1377216 - Accept environment variables to check_cmd_output; r?glandium And include code to work around a bug on older Python versions. MozReview-Commit-ID: 4pBnMQQJOGB
build/moz.configure/util.configure
--- a/build/moz.configure/util.configure
+++ b/build/moz.configure/util.configure
@@ -17,27 +17,44 @@ def configure_error(message):
     Primarily for use in moz.configure templates to sanity check
     their inputs from moz.configure usage.'''
     raise ConfigureError(message)
 
 # A wrapper to obtain a process' output that returns the output generated
 # by running the given command if it exits normally, and streams that
 # output to log.debug and calls die or the given error callback if it
 # does not.
+@imports(_from='__builtin__', _import='unicode')
 @imports('subprocess')
 @imports('sys')
 @imports(_from='mozbuild.configure.util', _import='LineIO')
 @imports(_from='mozbuild.shellutil', _import='quote')
 def check_cmd_output(*args, **kwargs):
     onerror = kwargs.pop('onerror', None)
 
+    # subprocess on older Pythons can't handle unicode keys or values in
+    # environment dicts. Normalize automagically so callers don't have to
+    # deal with this.
+    if 'env' in kwargs:
+        normalized_env = {}
+        for k, v in kwargs['env'].items():
+            if isinstance(k, unicode):
+                k = k.encode('utf-8', 'strict')
+
+            if isinstance(v, unicode):
+                v = v.encode('utf-8', 'strict')
+
+            normalized_env[k] = v
+
+        kwargs['env'] = normalized_env
+
     with log.queue_debug():
         log.debug('Executing: `%s`', quote(*args))
         proc = subprocess.Popen(args, stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE)
+                                stderr=subprocess.PIPE, **kwargs)
         stdout, stderr = proc.communicate()
         retcode = proc.wait()
         if retcode == 0:
             return stdout
 
         log.debug('The command returned non-zero exit status %d.',
                   retcode)
         for out, desc in ((stdout, 'output'), (stderr, 'error output')):