Skip to content

Commit

Permalink
wxGUI: add env parameter in gcmd.py RunCommand (#1113)
Browse files Browse the repository at this point in the history
Co-authored-by: Vaclav Petras <wenzeslaus@gmail.com>
  • Loading branch information
2 people authored and neteler committed Nov 24, 2020
1 parent 259df6f commit 59ef33b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions gui/wxpython/core/gcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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)))
Expand Down

0 comments on commit 59ef33b

Please sign in to comment.