+ 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