2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 ###############################################
26 ############### IMPORTANT NOTE ################
27 ###############################################
28 # The runSalomeScript script is obsolete. #
29 # Please consider the new salome launcher. #
30 ###############################################
32 import os,sys,optparse
37 Connect to a SALOME session (local or remote) and execute a Python script with data files in argument (optional)
39 Usage: %prog [ options ] script
41 script : The Python script to execute in the SALOME session
45 If PORT and MACHINE are not given, try to connect to the last active session on the local machine
46 If PORT and MACHINE are given, try to connect to the remote session associated with PORT on MACHINE
47 If MACHINE is not given, try to connect to the session associated to PORT on the local machine
48 If PORT is not given, try to connect to the remote session associated to port 2810 on MACHINE
50 If MACHINE isn't localhost or hostname, USER is needed
52 If Salome isn't installed on the local machine, use APPLI_DIST to execute a distant session
57 script is needed in all case
58 INPUT and OUTPUT are used only when Salome isn't on local machine. It defines a list of script (full path, blank separated)
59 Actions done with these two lists are :
60 - adjustment of path in a temporary script file
61 - copy of the input files and script file on the distant machine
65 appli_local=os.path.realpath(os.path.dirname(__file__))
68 return socket.gethostname().split('.')[0]
70 def vararg_callback(option, opt_str, value, parser):
71 """optparse callback for variable length arguments"""
80 # Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f",
81 # etc. Note that this also stops on "-3" or "-3.0", so if
82 # your option takes numeric values, you will need to handle this.
83 if ((arg[:2] == "--" and len(arg) > 2) or
84 (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
90 setattr(parser.values, option.dest, value)
93 """parse arguments, check validity and set defaults"""
94 parser = optparse.OptionParser(usage=usage)
95 parser.add_option('-p','--port', dest="port", default='', help="The port to connect to")
96 parser.add_option('-m','--machine', dest="machine", default='', help="The computer to connect to")
97 parser.add_option('-d','--directory', dest="directory", help="[Distant Mode] The APPLI_HOME path on the distant machine (must be used if Salome isn't on the local machine)")
98 parser.add_option('-u','--user', dest="user", default='', help="[Distant Mode] The user on the computer to connect to")
99 parser.add_option('-i','--infiles', dest="infiles", default=[], action="callback", callback=vararg_callback,
100 help="[Distant Mode] The list of input files (blank separated) used in the Python script")
101 parser.add_option('-o','--outfiles', dest="outfiles", default=[], action="callback", callback=vararg_callback,
102 help="[Distant Mode] The list of output files (blank separated) generated by the Python script")
110 options, args = parser.parse_args(args)
113 if not os.path.exists(script):
114 print "ERROR: the script file is mandatory"
117 machine=options.machine
120 infiles = options.infiles
121 outfiles = options.outfiles
122 directory=options.directory
128 if machine != here and machine != "localhost":
129 #SALOME server is on a remote computer
133 print "ERROR: the remote execution needs -u user argument"
136 if not os.path.exists(os.path.join(appli_local,"runSession")):
138 print "ERROR: the remote execution without SALOME installation needs -d directory argument"
141 return mode,user,machine,port,directory,infiles,outfiles,script
143 def copy_files(user,machine,script,infiles,outfiles,directory):
144 """modify script, copy files to remote computer and return lists of copied files"""
146 namescript=os.path.basename(script)
148 logname = getpass.getuser()
149 tmp_script="/tmp/%s_%s_%s" % (logname,os.getpid(),namescript)
150 with open(script, 'r') as fscript:
151 script_text=fscript.read()
157 for infile in infiles:
158 # generate a temporary file name
159 namefile=os.path.basename(infile)
160 tmp_file="/tmp/%s_%s_i%s_%s" % (logname,os.getpid(),n,namefile)
162 #modify the salome script
163 script_text = re.sub(infile,tmp_file,script_text)
165 # copy the infile to the remote server
166 cmd="scp %s %s@%s:%s" % (infile,user,machine,tmp_file)
170 list_infiles.append(tmp_file)
174 for outfile in outfiles:
175 # generate a temporary file name
176 namefile=os.path.basename(outfile)
177 tmp_file="/tmp/%s_%s_o%s_%s" % (logname,os.getpid(),n,namefile)
179 #modify the salome script
180 script_text = re.sub(outfile,tmp_file,script_text)
182 list_outfiles.append(tmp_file)
185 with open(tmp_script,'w') as fscript:
186 fscript.write(script_text)
189 #copy the salome script on the remote server
190 cmd="scp %s %s@%s:%s" % (tmp_script,user,machine,tmp_script)
194 return list_infiles, list_outfiles, tmp_script
198 mode,user,machine,port,directory,infiles,outfiles,script = parse_args()
203 list_infiles, list_outfiles, tmp_script = copy_files(user,machine,script,infiles,outfiles,directory)
205 #################################################
207 #################################################
211 # execute runSession from the remote SALOME application
212 cmd="ssh %s@%s %s/runSession " % (user,machine,directory)
214 cmd=cmd+"-p %s "%port
215 cmd = cmd +"python " + tmp_script
216 print '[ SSH ] ' + cmd
222 # execute runSession from the local SALOME application
223 cmd="%s/runSession " % appli_local
225 cmd=cmd+"-m %s "%machine
227 cmd=cmd+"-p %s "%port
228 cmd = cmd +"python " + tmp_script
229 print '[ SH ] ' + cmd
232 #################################################
233 # Get remote files and clean #
234 #################################################
236 temp_files=list_infiles+list_outfiles+[tmp_script]
239 for outfile in outfiles:
240 remote_outfile=list_outfiles.pop(0)
241 cmd="scp %s@%s:%s %s" %(user,machine,remote_outfile,outfile)
245 #clean temporary files
246 cmd="ssh %s@%s \\rm -f %s" % (user,machine," ".join(temp_files))
247 print '[ SSH ] ' + cmd
249 os.remove(tmp_script)
251 if __name__ == '__main__':