|
@@ -29,7 +29,7 @@ import codecs
|
|
|
import types as python_types
|
|
|
|
|
|
from utils import KeyValue, parse_key_val, basename, encode
|
|
|
-from grass.exceptions import ScriptError
|
|
|
+from grass.exceptions import ScriptError, CalledModuleError
|
|
|
|
|
|
# i18N
|
|
|
import gettext
|
|
@@ -344,7 +344,7 @@ def start_command(prog, flags="", overwrite=False, quiet=False,
|
|
|
|
|
|
def run_command(*args, **kwargs):
|
|
|
"""Passes all arguments to start_command(), then waits for the process to
|
|
|
- complete, returning its exit code. Similar to subprocess.call(), but
|
|
|
+ complete, returning its exit code. Similar to subprocess.check_call(), but
|
|
|
with the make_command() interface.
|
|
|
|
|
|
:param list args: list of unnamed arguments (see start_command() for details)
|
|
@@ -353,7 +353,15 @@ def run_command(*args, **kwargs):
|
|
|
:return: exit code (0 for success)
|
|
|
"""
|
|
|
ps = start_command(*args, **kwargs)
|
|
|
- return ps.wait()
|
|
|
+ returncode = ps.wait()
|
|
|
+ if returncode:
|
|
|
+ # TODO: construction of the whole command is far from perfect
|
|
|
+ args = make_command(*args, **kwargs)
|
|
|
+ raise CalledModuleError(module=None, code=' '.join(args),
|
|
|
+ returncode=returncode)
|
|
|
+ else:
|
|
|
+ # the else is just for compatibility, remove before 7.1
|
|
|
+ return 0
|
|
|
|
|
|
|
|
|
def pipe_command(*args, **kwargs):
|
|
@@ -402,8 +410,15 @@ def read_command(*args, **kwargs):
|
|
|
|
|
|
:return: stdout
|
|
|
"""
|
|
|
- ps = pipe_command(*args, **kwargs)
|
|
|
- return ps.communicate()[0]
|
|
|
+ process = pipe_command(*args, **kwargs)
|
|
|
+ stdout, unused = process.communicate()
|
|
|
+ returncode = process.poll()
|
|
|
+ if returncode:
|
|
|
+ # TODO: construction of the whole command is far from perfect
|
|
|
+ args = make_command(*args, **kwargs)
|
|
|
+ raise CalledModuleError(module=None, code=' '.join(args),
|
|
|
+ returncode=returncode)
|
|
|
+ return stdout
|
|
|
|
|
|
|
|
|
def parse_command(*args, **kwargs):
|
|
@@ -458,10 +473,17 @@ def write_command(*args, **kwargs):
|
|
|
:return: return code
|
|
|
"""
|
|
|
stdin = kwargs['stdin']
|
|
|
- p = feed_command(*args, **kwargs)
|
|
|
- p.stdin.write(stdin)
|
|
|
- p.stdin.close()
|
|
|
- return p.wait()
|
|
|
+ process = feed_command(*args, **kwargs)
|
|
|
+ process.communicate(stdin)
|
|
|
+ returncode = process.poll()
|
|
|
+ if returncode:
|
|
|
+ # TODO: construction of the whole command is far from perfect
|
|
|
+ args = make_command(*args, **kwargs)
|
|
|
+ raise CalledModuleError(module=None, code=' '.join(args),
|
|
|
+ returncode=returncode)
|
|
|
+ else:
|
|
|
+ # the else is just for compatibility, remove before 7.1
|
|
|
+ return 0
|
|
|
|
|
|
|
|
|
def exec_command(prog, flags="", overwrite=False, quiet=False, verbose=False,
|
|
@@ -1044,9 +1066,13 @@ def find_file(name, element='cell', mapset=None):
|
|
|
if element == 'raster' or element == 'rast':
|
|
|
verbose(_('Element type should be "cell" and not "%s"') % element)
|
|
|
element = 'cell'
|
|
|
- s = read_command("g.findfile", flags='n', element=element, file=name,
|
|
|
- mapset=mapset)
|
|
|
- return parse_key_val(s)
|
|
|
+ # g.findfile returns non-zero when file was not found
|
|
|
+ # se we ignore return code and just focus on stdout
|
|
|
+ process = start_command('g.findfile', flags='n',
|
|
|
+ element=element, file=name, mapset=mapset,
|
|
|
+ stdout=PIPE)
|
|
|
+ stdout = process.communicate()[0]
|
|
|
+ return parse_key_val(stdout)
|
|
|
|
|
|
# interface to g.list
|
|
|
|