Salome HOME
Remove use of mutable as default parameter value
[modules/kernel.git] / bin / runSession.py
index bd2256b0b65ec1fae430b18fe97e20c5ba2cade6..f698e323d252914a436b0cbc298a2c3cbb9d49b8 100644 (file)
@@ -26,6 +26,7 @@ import sys
 from optparse import OptionParser
 from NSparam import getNSparams
 import socket
+import subprocess
 
 # Use to display newlines (\n) in epilog
 class MyParser(OptionParser):
@@ -33,7 +34,9 @@ class MyParser(OptionParser):
         return self.epilog
 #
 
-def configureSession(args=[]):
+def configureSession(args=None):
+  if args is None:
+    args = []
   usage = "Usage: %prog [options] [command]"
   epilog  = """\n
 If the command is not given a shell is opened; else execute the given command.
@@ -104,3 +107,40 @@ def _writeConfigFile(port, host):
 
   os.environ['OMNIORB_CONFIG'] = filename
 #
+
+# command looks like a Bash command-line:
+# script1.py [args] ; script2.py [args] ; ...
+def runSession(command):
+  if command:
+    sep = ";"
+    if sys.platform == "win32":
+      sep= "&"
+    command = command.split(sep)
+    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:
+        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','')
+    cmd = ["/bin/bash",  "--rcfile", absoluteAppliPath + "/.bashrc" ]
+    proc = subprocess.Popen(cmd, shell=False, close_fds=True)
+    return proc.communicate()
+#