+
+# command looks like a Bash command-line:
+# script1.py [args] ; script2.py [args] ; ...
+def __runLocalSession(command):
+ if command:
+ sep = ";"
+ if sys.platform == "win32":
+ sep= "&"
+ command = command.split(sep)
+ outmsg = []
+ errmsg = []
+ for cmd in command:
+ single_cmd = cmd.strip().split(' ')
+ any_error = False
+ error_code = 1
+ try:
+ proc = subprocess.Popen(single_cmd)
+ (stdoutdata, stderrdata) = proc.communicate() # Wait for process to terminate
+ if stdoutdata:
+ outmsg.append(stdoutdata)
+ if stderrdata:
+ errmsg.append(stderrdata)
+
+ if proc.returncode != 0:
+ any_error = True
+ error_code = proc.returncode
+ except:
+ any_error = True
+ pass
+
+ if any_error:
+ errmsg.append("Error raised when executing command: %s\n"%cmd)
+ if outmsg:
+ sys.stdout.write("".join(outmsg))
+ if errmsg:
+ sys.stderr.write("".join(errmsg))
+ sys.exit(error_code)
+
+ return 0
+ else:
+ absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
+ if sys.platform == "win32":
+ cmd = ["cmd", "/K", "set PROMPT=[SALOME] $P$G"]
+ else:
+ cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ]
+ proc = subprocess.Popen(cmd, shell=False, close_fds=True)
+ proc.communicate()
+ return proc.returncode
+#
+
+def __copyFiles(user, machine, script, infiles, outfiles):
+ """Modify script, copy files to remote computer and return lists of copied files."""
+
+ namescript = os.path.basename(script)
+ import getpass
+ logname = getpass.getuser()
+ tmp_script = "/tmp/%s_%s_%s" % (logname, os.getpid(), namescript)
+ with open(script, 'r') as fscript:
+ script_text = fscript.read()
+
+ list_infiles = []
+ list_outfiles = []
+ n = 0
+ for infile in infiles:
+ # generate a temporary file name
+ namefile = os.path.basename(infile)
+ tmp_file = "/tmp/%s_%s_i%s_%s" % (logname, os.getpid(), n, namefile)
+
+ # modify the salome script
+ script_text = re.sub(infile, tmp_file, script_text)
+
+ # copy the infile to the remote server
+ cmd = "scp %s %s@%s:%s" % (infile, user, machine, tmp_file)
+ print("[ SCP ]", cmd)
+ os.system(cmd)
+
+ list_infiles.append(tmp_file)
+ n = n + 1
+ #
+ n = 0
+ for outfile in outfiles:
+ # generate a temporary file name
+ namefile = os.path.basename(outfile)
+ tmp_file = "/tmp/%s_%s_o%s_%s" % (logname, os.getpid(), n, namefile)
+
+ # modify the salome script
+ script_text = re.sub(outfile, tmp_file, script_text)
+
+ list_outfiles.append(tmp_file)
+ n = n + 1
+ #
+
+ with open(tmp_script,'w') as fscript:
+ fscript.write(script_text)
+
+ # copy the salome script on the remote server
+ cmd = "scp %s %s@%s:%s" % (tmp_script, user, machine, tmp_script)
+ print("[ SCP ]", cmd)
+ os.system(cmd)
+
+ return list_infiles, list_outfiles, tmp_script
+#
+
+# sa_obj is a ScriptAndArgs object (from salomeContextUtils)
+def __runRemoteSession(sa_obj, params):
+ if not params.user:
+ print("ERROR: The user login on remote machine MUST be given.")
+ return 1
+ if not params.directory:
+ print("ERROR: The remote directory MUST be given.")
+ return 1
+
+ # sa_obj.script may be 'python script.py' --> only process .py file
+ header = " ".join(sa_obj.script.split()[:-1])
+ script = sa_obj.script.split()[-1]
+
+ tmp_in, tmp_out, tmp_script = __copyFiles(params.user, params.machine, script, sa_obj.args or [], sa_obj.out or [])
+
+ # execute command on the remote SALOME application
+ command = "ssh %s@%s %s/salome shell " % (params.user, params.machine, params.directory)
+ if params.port:
+ command = command + "-p %s "%params.port
+ command = command + " %s %s args:%s"%(header, tmp_script, ",".join(tmp_in))
+ print('[ SSH ] ' + command)
+ os.system(command)
+
+ # Get remote files and clean
+ temp_files = tmp_in + tmp_out + [tmp_script]
+
+ # get the outfiles
+ for outfile in (sa_obj.out or []):
+ remote_outfile = tmp_out.pop(0)
+ command = "scp %s@%s:%s %s" %(params.user, params.machine, remote_outfile, outfile)
+ print("[ SCP ] " + command)
+ os.system(command)
+
+ # clean temporary files
+ command = "ssh %s@%s \\rm -f %s" % (params.user, params.machine, " ".join(temp_files))
+ print('[ SSH ] ' + command)
+ os.system(command)
+ os.remove(tmp_script)
+
+ return 0
+#
+
+def runSession(params, args):
+ scriptArgs = getScriptsAndArgs(args)
+
+ if params.mode == "local":
+ command = formatScriptsAndArgs(scriptArgs)
+ return __runLocalSession(command)
+
+ elif params.mode == "remote":
+ any_error = 0
+ for sa_obj in scriptArgs:
+ ok = __runRemoteSession(sa_obj, params)
+ if not ok:
+ any_error = 1
+ return any_error
+#