]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
set returncode after Python scripts execution
authorCédric Aguerre <cedric.aguerre@edf.fr>
Wed, 23 Apr 2014 13:42:17 +0000 (15:42 +0200)
committerCédric Aguerre <cedric.aguerre@edf.fr>
Wed, 23 Apr 2014 13:42:17 +0000 (15:42 +0200)
bin/appliskel/salome
bin/salomeContext.py

index ae57dae12f78c471fa4f96f7d4eaad80e01822ac..b9819eb146adb5d22239b716ead0f36950d5f761 100755 (executable)
@@ -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)
index b773833c70d970c4dab4135ce1d3cd125ae7f5aa..c8b5f6d418b2a0a8fb8629f11da3746a8d2242c0 100644 (file)
@@ -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)