浏览代码

wxGUI: add env parameter in gcmd.py RunCommand (#1113)

Co-authored-by: Vaclav Petras <wenzeslaus@gmail.com>
Anna Petrasova 4 年之前
父节点
当前提交
1b695d6cd3
共有 1 个文件被更改,包括 12 次插入7 次删除
  1. 12 7
      gui/wxpython/core/gcmd.py

+ 12 - 7
gui/wxpython/core/gcmd.py

@@ -671,7 +671,7 @@ def _formatMsg(text):
 
 def RunCommand(prog, flags="", overwrite=False, quiet=False,
                verbose=False, parent=None, read=False,
-               parse=None, stdin=None, getErrorMsg=False, **kwargs):
+               parse=None, stdin=None, getErrorMsg=False, env=None, **kwargs):
     """Run GRASS command
 
     :param prog: program to run
@@ -682,8 +682,11 @@ def RunCommand(prog, flags="", overwrite=False, quiet=False,
     :param parse: fn to parse stdout (e.g. grass.parse_key_val) or None
     :param stdin: stdin or None
     :param getErrorMsg: get error messages on failure
+    :param env: environment (optional, uses os.environ if not provided)
     :param kwargs: program parameters
 
+    The environment passed to the function (env or os.environ) is not modified (a copy is used internally).
+
     :return: returncode (read == False and getErrorMsg == False)
     :return: returncode, messages (read == False and getErrorMsg == True)
     :return: stdout (read == True and getErrorMsg == False)
@@ -703,13 +706,18 @@ def RunCommand(prog, flags="", overwrite=False, quiet=False,
     if stdin:
         kwargs['stdin'] = subprocess.PIPE
 
+    # Do not change the environment, only a local copy.
+    if env:
+        env = env.copy()
+    else:
+        env = os.environ.copy()
+
     if parent:
-        messageFormat = os.getenv('GRASS_MESSAGE_FORMAT', 'gui')
-        os.environ['GRASS_MESSAGE_FORMAT'] = 'standard'
+        env['GRASS_MESSAGE_FORMAT'] = 'standard'
 
     start = time.time()
 
-    ps = grass.start_command(prog, flags, overwrite, quiet, verbose, **kwargs)
+    ps = grass.start_command(prog, flags, overwrite, quiet, verbose, env=env, **kwargs)
 
     if stdin:
         ps.stdin.write(encode(stdin))
@@ -720,9 +728,6 @@ def RunCommand(prog, flags="", overwrite=False, quiet=False,
     stderr = decode(stderr)
     stdout = decode(stdout) if read else stdout
 
-    if parent:  # restore previous settings
-        os.environ['GRASS_MESSAGE_FORMAT'] = messageFormat
-
     ret = ps.returncode
     Debug.msg(1, "gcmd.RunCommand(): get return code %d (%.6f sec)" %
               (ret, (time.time() - start)))