1 # Copyright (C) 2005 OPEN CASCADE, CEA, EDF R&D, LEG
2 # PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 import os, glob, string, sys, re
22 # names of tags in XML configuration file
27 # names of attributes in XML configuration file
31 # certain values in XML configuration file ("launch" section)
38 portkill_nam = "portkill"
39 killall_nam = "killall"
40 modules_nam = "modules"
41 embedded_nam = "embedded"
42 standalone_nam = "standalone"
43 containers_nam = "containers"
46 except_nam = "noexcepthandler"
48 # values in XML configuration file giving specific module parameters (<module_name> section)
49 # which are stored in opts with key <module_name>_<parameter> (eg SMESH_plugins)
50 plugins_nam = "plugins"
52 # values passed as arguments, NOT read from XML config file, but set from within this script
53 appname_nam = "appname"
57 # values of boolean type (must be '0' or '1').
58 # xml_parser.boolValue() is used for correct setting
59 boolKeys = ( gui_nam, splash_nam, logger_nam, file_nam, xterm_nam, portkill_nam, killall_nam, interp_nam, except_nam )
62 listKeys = ( containers_nam, embedded_nam, key_nam, modules_nam, standalone_nam, plugins_nam )
64 # return application version (uses GUI_ROOT_DIR (or KERNEL_ROOT_DIR in batch mode) +/bin/salome/VERSION)
66 root_dir = os.environ.get( 'KERNEL_ROOT_DIR', '' ) # KERNEL_ROOT_DIR or "" if not found
67 root_dir = os.environ.get( 'GUI_ROOT_DIR', root_dir ) # GUI_ROOT_DIR or KERNEL_ROOT_DIR or "" if both not found
68 filename = root_dir+'/bin/salome/VERSION'
69 str = open( filename, "r" ).readline() # str = "THIS IS SALOME - SALOMEGUI VERSION: 3.0.0"
70 match = re.search( r':\s+([a-zA-Z0-9.]+)\s*$', str )
72 return match.group( 1 )
75 # calculate and return configuration file id in order to unically identify it
76 # for example: for 3.1.0a1 the id is 301000101
77 def version_id( fname ):
78 vers = fname.split(".")
81 mr = re.search(r'^([0-9]+)([A-Za-z]?)([0-9]*)',vers[2])
84 release = int(mr.group(1))
86 if len(mr.group(2)): dev1 = ord(mr.group(2))
87 if len(mr.group(3)): dev2 = int(mr.group(3))
88 dev = dev1 * 100 + dev2
92 ver = ver * 100 + minor
93 ver = ver * 100 + release
94 ver = ver * 10000 + dev
97 # get user configuration file name
101 return "" # not unknown version
102 filename = "%s/.%src.%s" % (os.environ['HOME'], appname, v)
103 if os.path.exists(filename):
104 return filename # user preferences file for the current version exists
106 id0 = version_id( v )
107 # get all existing user preferences files
108 files = glob.glob( os.environ['HOME'] + "/." + appname + "rc.*" )
111 match = re.search( r'\.%src\.([a-zA-Z0-9.]+)$'%appname, file )
112 if match: f2v[file] = match.group(1)
116 ver = version_id( f2v[file] )
117 if ver and abs(last_version-id0) > abs(ver-id0):
122 # -----------------------------------------------------------------------------
124 ### xml reader for launch configuration file usage
129 def __init__(self, fileName, _opts ):
130 print "Configure parser: processing %s ..." % fileName
133 self.section = section_to_skip
134 parser = xml.sax.make_parser()
135 parser.setContentHandler(self)
136 parser.parse(fileName)
139 def boolValue( self, str ):
140 if str in ("yes", "y", "1"):
142 elif str in ("no", "n", "0"):
148 def startElement(self, name, attrs):
149 self.space.append(name)
152 # if we are analyzing "section" element and its "name" attribute is
153 # either "launch" or module name -- set section_name
154 if self.space == [doc_tag, sec_tag] and nam_att in attrs.getNames():
155 section_name = attrs.getValue( nam_att )
156 if section_name == lanch_nam:
157 self.section = section_name # launch section
158 elif self.opts.has_key( modules_nam ) and \
159 section_name in self.opts[ modules_nam ]:
160 self.section = section_name # <module> section
162 self.section = section_to_skip # any other section
165 # if we are analyzing "parameter" elements - children of either
166 # "section launch" or "section "<module>"" element, then store them
167 # in self.opts assiciative array (key = [<module>_ + ] value of "name" attribute)
168 elif self.section != section_to_skip and \
169 self.space == [doc_tag, sec_tag, par_tag] and \
170 nam_att in attrs.getNames() and \
171 val_att in attrs.getNames():
172 nam = attrs.getValue( nam_att )
173 val = attrs.getValue( val_att )
174 if self.section == lanch_nam: # key for launch section
176 else: # key for <module> section
177 key = self.section + "_" + nam
179 self.opts[key] = self.boolValue( val ) # assign boolean value: 0 or 1
180 elif nam in listKeys:
181 self.opts[key] = val.split( ',' ) # assign list value: []
183 self.opts[key] = val;
187 def endElement(self, name):
190 if self.section != section_to_skip and name == sec_tag:
191 self.section = section_to_skip
194 def characters(self, content):
197 def processingInstruction(self, target, data):
200 def setDocumentLocator(self, locator):
203 def startDocument(self):
207 def endDocument(self):
211 # -----------------------------------------------------------------------------
213 ### searching for launch configuration files
215 # - environment variable {'appname'+'Config'} (SalomeAppConfig) contains list of directories (';' as devider)
216 # - these directories contain 'appname'+'.xml' (SalomeApp.xml) configuration files
217 # - these files are analyzed beginning with the last one (last directory in the list)
218 # - if a key is found in next analyzed cofiguration file - it will be replaced
219 # - the last configuration file to be analyzed - ~/.'appname'+'rc' (~/SalomeApprc) (if it exists)
220 # - but anyway, if user specifies a certain option in a command line - it will replace the values
221 # - specified in configuration file(s)
222 # - once again the order of settings (next setting replaces the previous ones):
223 # - SalomeApp.xml files in directories specified by SalomeAppConfig env variable
224 # - .SalomeApprc file in user's catalogue
227 config_var = appname+'Config'
228 # set resources variables if not yet set
230 if os.getenv(config_var):
231 dirs += re.split('[;|:]', os.getenv(config_var))
232 if os.getenv("GUI_ROOT_DIR"):
233 dirs += [os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"]
234 os.environ[config_var] = ":".join(dirs)
236 dirs.reverse() # reverse order, like in "path" variable - FILO-style processing
238 _opts = {} # assiciative array of options to be filled
240 # SalomeApp.xml files in directories specified by SalomeAppConfig env variable
242 filename = dir+'/'+appname+'.xml'
243 if not os.path.exists(filename):
244 print "Configure parser: Warning : could not find configuration file %s" % filename
247 p = xml_parser(filename, _opts)
250 print "Configure parser: Error : can not read configuration file %s" % filename
253 # SalomeApprc file in user's catalogue
254 filename = userFile()
255 if filename and not os.path.exists(filename):
256 print "Configure parser: Warning : could not find user configuration file"
259 p = xml_parser(filename, _opts)
262 print 'Configure parser: Error : can not read user configuration file'
266 # --- setting default values of keys if they were NOT set in config files ---
267 for aKey in listKeys:
268 if not args.has_key( aKey ):
271 for aKey in boolKeys:
272 if not args.has_key( aKey ):
277 args[file_nam]=[afile]
279 args[appname_nam] = appname
281 ### searching for my port
285 file = open(os.environ["OMNIORB_CONFIG"], "r")
288 l = string.split(s, ":")
289 if string.split(l[0], " ")[0] == "ORBInitRef" or string.split(l[0], " ")[0] == "InitRef" :
290 my_port = int(l[len(l)-1])
297 args[port_nam] = my_port
299 # -----------------------------------------------------------------------------
301 ### command line options reader
303 def options_parser(line):
306 for delimiter in [" ", ",", "="]:
308 list += string.split(o, delimiter)
316 while i < len(source):
317 if source[i][0] != '-':
319 elif source[i][1] == '-':
329 while i < len(source) and source[i][0] != '-':
330 result[key].append(source[i])
336 # -----------------------------------------------------------------------------
338 ### read command-line options : each arg given in command line supersedes arg from xml config file
341 cmd_opts = options_parser(sys.argv[1:])
342 kernel_root_dir=os.environ["KERNEL_ROOT_DIR"]
347 ### check all options are right
351 if not opt in ("h","g","l","f","x","m","e","s","c","p","k","t","i","r"):
352 print "Configure parser: Error : command line error : -%s" % opt
358 if cmd_opts.has_key("h"):
359 print """USAGE: runSalome.py [options]
360 [command line options] :
361 --help or -h : print this help
362 --gui or -g : launching with GUI
363 --terminal -t : launching without gui (to deny --gui)
364 --logger or -l : redirect messages in a CORBA collector
365 --file=filename or -f=filename: redirect messages in a log file
366 --xterm or -x : execute servers in xterm console (messages appear in xterm windows)
367 --modules=module1,module2,... : salome module list (modulen is the name of Salome module to load)
368 or -m=module1,module2,...
369 --embedded=registry,study,moduleCatalog,cppContainer
370 or -e=registry,study,moduleCatalog,cppContainer
371 : embedded CORBA servers (default: registry,study,moduleCatalog,cppContainer)
372 : (logger,pyContainer,supervContainer can't be embedded
373 --standalone=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
374 or -s=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
375 : standalone CORBA servers (default: pyContainer,supervContainer)
376 --containers=cpp,python,superv: (obsolete) launching of containers cpp, python and supervision
377 or -c=cpp,python,superv : = get default from -e and -s
378 --portkill or -p : kill the salome with current port
379 --killall or -k : kill all salome sessions
380 --interp=n or -i=n : number of additional xterm to open, with session environment
381 -z : display splash screen
382 -r : disable centralized exception handling mechanism
384 For each Salome module, the environment variable <modulen>_ROOT_DIR must be set.
385 The module name (<modulen>) must be uppercase.
386 KERNEL_ROOT_DIR is mandatory.
391 ### apply command-line options to the arguments
402 args[file_nam] = cmd_opts['f']
406 args[interp_nam] = cmd_opts['i']
408 args[modules_nam] = cmd_opts['m']
410 args[embedded_nam] = cmd_opts['e']
412 args[standalone_nam] = cmd_opts['s']
414 args[containers_nam] = cmd_opts['c']
416 args[portkill_nam] = 1
418 args[killall_nam] = 1
422 # if --modules (-m) command line option is not given
423 # try SALOME_MODULES environment variable
424 if not cmd_opts.has_key( "m" ) and os.getenv( "SALOME_MODULES" ):
425 args[modules_nam] = re.split( "[:;,]", os.getenv( "SALOME_MODULES" ) )
428 # 'terminal' must be processed in the end: to deny any 'gui' options
433 # now modify SalomeAppConfig environment variable
434 dirs = re.split('[;|:]', os.environ[config_var] )
436 for m in args[modules_nam]:
437 if m not in ["KERNEL", "GUI", ""] and os.getenv("%s_ROOT_DIR"%m):
438 d1 = os.getenv("%s_ROOT_DIR"%m) + "/share/salome/resources/" + m.lower()
439 d2 = os.getenv("%s_ROOT_DIR"%m) + "/share/salome/resources"
440 if os.path.exists( "%s/%s.xml"%(d1, appname) ):
442 elif os.path.exists( "%s/%s.xml"%(d2, appname) ):
444 os.environ[config_var] = ":".join(dirs)