[lnst] ExecCmd: Adding some info to ExecCmdFail exception

Jiří Pírko jirka at fedoraproject.org
Wed Aug 1 11:15:25 UTC 2012


commit 252bb2624ab2bb0c97be1a28a0777cb8b8c76db1
Author: Radek Pazdera <rpazdera at redhat.com>
Date:   Wed Aug 1 13:00:06 2012 +0200

    ExecCmd: Adding some info to ExecCmdFail exception
    
    This commit extends ExecCmdFail exception with some information of
    what went wrong, so the parent code that can catch this error can
    react appropriatelly.
    
    ExecCmdFail now contains return value of the command, the command
    itself (for reporting purposes - which command failed) and conents
    of standard error output, that usualy contains a clue or some error
    message.
    
    Signed-off-by: Radek Pazdera <rpazdera at redhat.com>

 Common/ExecCmd.py |   25 ++++++++++++++++++++++---
 1 files changed, 22 insertions(+), 3 deletions(-)
---
diff --git a/Common/ExecCmd.py b/Common/ExecCmd.py
index e2e7f61..702ad73 100644
--- a/Common/ExecCmd.py
+++ b/Common/ExecCmd.py
@@ -14,7 +14,25 @@ import logging
 import subprocess
 
 class ExecCmdFail(Exception):
-    pass
+    _cmd = None
+    _retval = None
+    _stderr = None
+
+    def __init__(self, cmd=None, retval=None, err=""):
+        self._stderr = err
+        self._retval = retval
+
+    def get_cmd(self):
+        return self._cmd
+
+    def get_stderr(self):
+        return self._stderr
+
+    def __str__(self):
+        retval = ""
+        if self._retval:
+            retval = " (exited with %d)" % self._retval
+        return "Command execution failed%s" % retval
 
 def log_output(log_func, out_type, out):
     log_func("%s:\n"
@@ -40,7 +58,8 @@ def exec_cmd(cmd, die_on_err=True, log_outputs=True):
         if data_stderr:
             log_output(logging.error, "Stderr", data_stderr)
     if subp.returncode and die_on_err:
-        logging.error("Command failed with error \"%d\"" % subp.returncode)
-        raise ExecCmdFail
+        err = ExecCmdFail(cmd, subp.returncode, data_stderr)
+        logging.error(err)
+        raise err
 
     return data_stdout, data_stderr


More information about the LNST-developers mailing list