diff mercurial/util.py @ 1880:05c7d75be925

fix broken environment save/restore when a hook runs. move "run commend with different env/cwd" code out to function in util. new function is called esystem.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Fri, 10 Mar 2006 22:24:19 -0800
parents d314a89fa4f1
children c0320567931f
line wrap: on
line diff
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -325,6 +325,29 @@ def system(cmd, errprefix=None):
             errmsg = "%s: %s" % (errprefix, errmsg)
         raise Abort(errmsg)
 
+def esystem(cmd, environ={}, cwd=None):
+    '''enhanced shell command execution.
+    run with environment maybe modified, maybe in different dir.'''
+    oldenv = {}
+    for k in environ:
+        oldenv[k] = os.environ.get(k)
+    if cwd is not None:
+        oldcwd = os.getcwd()
+    try:
+        for k, v in environ.iteritems():
+            os.environ[k] = str(v)
+        if cwd is not None and oldcwd != cwd:
+            os.chdir(cwd)
+        return os.system(cmd)
+    finally:
+        for k, v in oldenv.iteritems():
+            if v is None:
+                del os.environ[k]
+            else:
+                os.environ[k] = v
+        if cwd is not None and oldcwd != cwd:
+            os.chdir(oldcwd)
+
 def rename(src, dst):
     """forcibly rename a file"""
     try: