[GRASS-SVN] r45649 - grass/branches/develbranch_6/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Mar 12 16:08:38 EST 2011
Author: martinl
Date: 2011-03-12 13:08:38 -0800 (Sat, 12 Mar 2011)
New Revision: 45649
Modified:
grass/branches/develbranch_6/lib/python/core.py
Log:
pythonlib: raise ScriptError (run/read/write_command)
(merge r45648 from trunk)
Modified: grass/branches/develbranch_6/lib/python/core.py
===================================================================
--- grass/branches/develbranch_6/lib/python/core.py 2011-03-12 21:05:50 UTC (rev 45648)
+++ grass/branches/develbranch_6/lib/python/core.py 2011-03-12 21:08:38 UTC (rev 45649)
@@ -20,7 +20,7 @@
@author Glynn Clements
@author Martin Landa <landa.martin gmail.com>
- at author Michael Barton <michael.barton at asu.edu>
+ at author Michael Barton <michael.barton asu.edu>
"""
import os
@@ -40,15 +40,15 @@
# subprocess wrapper that uses shell on Windows
class Popen(subprocess.Popen):
- def __init__(self, args, bufsize=0, executable=None,
- stdin=None, stdout=None, stderr=None,
- preexec_fn=None, close_fds=False, shell=None,
- cwd=None, env=None, universal_newlines=False,
- startupinfo=None, creationflags=0):
-
+ def __init__(self, args, bufsize = 0, executable = None,
+ stdin = None, stdout = None, stderr = None,
+ preexec_fn = None, close_fds = False, shell = None,
+ cwd = None, env = None, universal_newlines = False,
+ startupinfo = None, creationflags = 0):
+
if shell == None:
shell = (sys.platform == "win32")
-
+
subprocess.Popen.__init__(self, args, bufsize, executable,
stdin, stdout, stderr,
preexec_fn, close_fds, shell,
@@ -58,7 +58,7 @@
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
-class ScriptException(Exception):
+class ScriptError(Exception):
def __init__(self, msg):
self.value = msg
@@ -94,6 +94,12 @@
return _make_val(list(val))
return str(val)
+def _get_error(string):
+ try:
+ return string.split('\n')[-2]
+ except:
+ return ''
+
def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
"""!Return a list of strings suitable for use as the args parameter to
Popen() or call(). Example:
@@ -181,9 +187,17 @@
@return exit code (0 for success)
"""
+ if get_raise_on_error():
+ kwargs['stderr'] = PIPE
ps = start_command(*args, **kwargs)
- return ps.wait()
-
+ if get_raise_on_error():
+ err = ps.communicate()[1]
+ if ps.returncode != 0:
+ raise ScriptError(_("Error in %s(%s): %s") % ('run_command', args[0], _get_error(err)))
+ return ps.returncode
+ else:
+ return ps.wait()
+
def pipe_command(*args, **kwargs):
"""!Passes all arguments to start_command(), but also adds
"stdout = PIPE". Returns the Popen object.
@@ -230,8 +244,16 @@
@return stdout
"""
+ if get_raise_on_error():
+ kwargs['stderr'] = PIPE
ps = pipe_command(*args, **kwargs)
- return _decode(ps.communicate()[0])
+ if get_raise_on_error():
+ out, err = ps.communicate()
+ if ps.returncode != 0:
+ raise ScriptError(_("Error in %s(%s): %s") % ('read_command', args[0], _get_error(err)))
+ return _decode(out)
+ else:
+ return _decode(ps.communicate()[0])
def parse_command(*args, **kwargs):
"""!Passes all arguments to read_command, then parses the output by
@@ -274,10 +296,19 @@
@return return code
"""
stdin = kwargs['stdin']
+ if get_raise_on_error():
+ kwargs['stderr'] = PIPE
p = feed_command(*args, **kwargs)
p.stdin.write(stdin)
- p.stdin.close()
- return p.wait()
+ if get_raise_on_error():
+ err = p.communicate()[1]
+ p.stdin.close()
+ if p.returncode != 0:
+ raise ScriptError(_("Error in %s(%s): %s") % ('write_command', args[0], _get_error(err)))
+ return p.returncode
+ else:
+ p.stdin.close()
+ return p.wait()
def exec_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, env = None, **kwargs):
"""!Interface to os.execvpe(), but with the make_command() interface.
@@ -361,7 +392,7 @@
"""
global raise_on_error
if raise_on_error:
- raise ScriptException(msg)
+ raise ScriptError(msg)
else:
message(msg, flag = 'e')
@@ -376,7 +407,7 @@
def set_raise_on_error(raise_exp = True):
"""!Define behaviour on error (error() called)
- @param raise_exp True to raise ScriptException instead of calling
+ @param raise_exp True to raise ScriptError instead of calling
error()
@return current status
@@ -384,7 +415,22 @@
global raise_on_error
tmp_raise = raise_on_error
raise_on_error = raise_exp
+
+ if raise_on_error:
+ os.environ['GRASS_MESSAGE_FORMAT'] = 'plain'
+ else:
+ os.environ['GRASS_MESSAGE_FORMAT'] = 'standard'
+
+ return tmp_raise
+def get_raise_on_error():
+ """!Get raise_on_error status
+
+ @return True to raise exception
+ """
+ global raise_on_error
+ return raise_on_error
+
# interface to g.parser
def _parse_opts(lines):
@@ -953,7 +999,7 @@
datum = None, desc = None):
"""!Create new location
- Raise ScriptException on error.
+ Raise ScriptError on error.
@param dbase path to GRASS database
@param location location name to create
@@ -1015,7 +1061,7 @@
set = 'GISDBASE=%s' % gisdbase)
if ps.returncode != 0 and error:
- raise ScriptException(repr(error))
+ raise ScriptError(repr(error))
try:
fd = codecs.open(os.path.join(dbase, location,
@@ -1027,12 +1073,12 @@
fd.write(os.linesep)
fd.close()
except OSError, e:
- raise ScriptException(repr(e))
+ raise ScriptError(repr(e))
def _create_location_xy(database, location):
"""!Create unprojected location
- Raise ScriptException on error.
+ Raise ScriptError on error.
@param database GRASS database where to create new location
@param location location name
@@ -1074,7 +1120,7 @@
os.chdir(cur_dir)
except OSError, e:
- raise ScriptException(repr(e))
+ raise ScriptError(repr(e))
# interface to g.version
More information about the grass-commit
mailing list