From: Cédric Aguerre Date: Wed, 23 Apr 2014 13:42:17 +0000 (+0200) Subject: set returncode after Python scripts execution X-Git-Tag: V7_4_0rc1~11 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c5187011571e6f983ee421289366066b9fd0370d;p=modules%2Fkernel.git set returncode after Python scripts execution --- diff --git a/bin/appliskel/salome b/bin/appliskel/salome index ae57dae12..b9819eb14 100755 --- a/bin/appliskel/salome +++ b/bin/appliskel/salome @@ -87,8 +87,9 @@ def main(args): sys.exit(1) # Create a SalomeContext which parses configFileNames to initialize environment + from salomeContextUtils import SalomeContextException try: - from salomeContext import SalomeContext, SalomeContextException + from salomeContext import SalomeContext context = SalomeContext(configFileNames) # Here set specific variables, if needed @@ -98,9 +99,16 @@ def main(args): # context.setVariable('myvarname', 'value') # Start SALOME, parsing command line arguments - context.runSalome(args) + (out, err), returncode = context.runSalome(args) #print 'Thank you for using SALOME!' + if out: + sys.stdout.write(out) + + if err: + sys.stderr.write(err) + + sys.exit(returncode) except SalomeContextException, e: import logging logging.getLogger("salome").error(e) diff --git a/bin/salomeContext.py b/bin/salomeContext.py index b773833c7..c8b5f6d41 100644 --- a/bin/salomeContext.py +++ b/bin/salomeContext.py @@ -112,7 +112,7 @@ class SalomeContext: msg = proc.communicate() if kill: self._killAll(args) - return msg + return msg, proc.returncode # """Append value to PATH environment variable""" @@ -217,12 +217,10 @@ class SalomeContext: try: res = getattr(self, command)(options) # run appropriate method return res or (None, None) - except SystemExit, exc: - if exc==0: - sys.exit(0) #catch sys.exit(0) happy end no warning - if exc==1: - self.getLogger().warning("SystemExit 1 in method %s.", command) - sys.exit(1) + except SystemExit, returncode: + if returncode != 0: + self.getLogger().warning("SystemExit %s in method %s.", returncode, command) + sys.exit(returncode) except StandardError: self.getLogger().error("Unexpected error:") import traceback @@ -307,14 +305,24 @@ class SalomeContext: outmsg = [] errmsg = [] for cmd in command: + save_cmd = cmd cmd = cmd.strip().split(' ') #proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc = subprocess.Popen(cmd) (stdoutdata, stderrdata) = proc.communicate() - if stdoutdata or stderrdata: + if stdoutdata: outmsg.append(stdoutdata) + if stderrdata: errmsg.append(stderrdata) + if proc.returncode != 0: + errmsg.append("Error raised when executing command: %s\n"%save_cmd) + if outmsg: + sys.stdout.write("".join(outmsg)) + if errmsg: + sys.stderr.write("".join(errmsg)) + sys.exit(proc.returncode) + return ("".join(outmsg), "".join(errmsg)) else: absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','') @@ -411,12 +419,12 @@ class SalomeContext: return self._logger # -### import pickle if __name__ == "__main__": if len(sys.argv) == 3: context = pickle.loads(sys.argv[1]) args = pickle.loads(sys.argv[2]) + (out, err) = context._startSalome(args) if out: sys.stdout.write(out)