Patchwork [1,of,8] util: extract the logic calculating environment variables

login
register
mail settings
Submitter Jun Wu
Date Jan. 9, 2017, 11:12 p.m.
Message ID <25760899b0ad12660b6a.1484003541@x1c>
Download mbox | patch
Permalink /patch/18154/
State Accepted
Headers show

Comments

Jun Wu - Jan. 9, 2017, 11:12 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1484002682 -28800
#      Tue Jan 10 06:58:02 2017 +0800
# Node ID 25760899b0ad12660b6a9e8c7b928dd2c334ec80
# Parent  7438cb35979a81e3d0aca5be9a6e4397186a509e
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 25760899b0ad
util: extract the logic calculating environment variables

The method will be reused in chgserver. Move it out so it can be reused.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -975,4 +975,19 @@  def _isstdout(f):
     return fileno and fileno() == sys.__stdout__.fileno()
 
+def shellenviron(environ=None):
+    """return environ with optional override, useful for shelling out"""
+    def py2shell(val):
+        'convert python object into string that is useful to shell'
+        if val is None or val is False:
+            return '0'
+        if val is True:
+            return '1'
+        return str(val)
+    env = dict(encoding.environ)
+    if environ:
+        env.update((k, py2shell(v)) for k, v in environ.iteritems())
+    env['HG'] = hgexecutable()
+    return env
+
 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
     '''enhanced shell command execution.
@@ -984,17 +999,8 @@  def system(cmd, environ=None, cwd=None, 
     if out is specified, it is assumed to be a file-like object that has a
     write() method. stdout and stderr will be redirected to out.'''
-    if environ is None:
-        environ = {}
     try:
         stdout.flush()
     except Exception:
         pass
-    def py2shell(val):
-        'convert python object into string that is useful to shell'
-        if val is None or val is False:
-            return '0'
-        if val is True:
-            return '1'
-        return str(val)
     origcmd = cmd
     cmd = quotecommand(cmd)
@@ -1007,7 +1013,5 @@  def system(cmd, environ=None, cwd=None, 
         rc = os.system(cmd)
     else:
-        env = dict(encoding.environ)
-        env.update((k, py2shell(v)) for k, v in environ.iteritems())
-        env['HG'] = hgexecutable()
+        env = shellenviron(environ)
         if out is None or _isstdout(out):
             rc = subprocess.call(cmd, shell=True, close_fds=closefds,