]> SALOME platform Git repositories - modules/kernel.git/blob - bin/launchConfigureParser.py
Salome HOME
SMH: Merge with development version = NEW GUI, POLY_WORK, OCC_development_02, BR_Lyfe...
[modules/kernel.git] / bin / launchConfigureParser.py
1 import os, glob, string, sys, re
2 import xml.sax
3
4 # names of tags in XML configuration file 
5 doc_tag = "document"
6 sec_tag = "section"
7 par_tag = "parameter"
8
9 # names of attributes in XML configuration file 
10 nam_att = "name"
11 val_att = "value"
12
13 # certain values in XML configuration file ("launch" section)
14 lanch_nam      = "launch"
15 gui_nam        = "gui"
16 logger_nam     = "logger"
17 xterm_nam      = "xterm"
18 file_nam       = "file"
19 portkill_nam   = "portkill"
20 killall_nam    = "killall"
21 modules_nam    = "modules"
22 pyModules_nam  = "pyModules"
23 embedded_nam   = "embedded"
24 standalone_nam = "standalone"
25 containers_nam = "containers"
26 key_nam        = "key"
27 interp_nam     = "interp"
28
29 # values passed as arguments, NOT read from XML config file, but set from within this script
30 appname_nam    = "appname"
31 port_nam       = "port"
32 appname        = "SalomeApp"
33
34 # values of boolean type (must be '0' or '1').
35 # xml_parser.boolValue() is used for correct setting
36 boolKeys = ( gui_nam, logger_nam, file_nam, xterm_nam, portkill_nam, killall_nam, interp_nam )
37
38 # values of list type
39 listKeys = ( containers_nam, embedded_nam, key_nam, modules_nam, standalone_nam )
40
41
42 # -----------------------------------------------------------------------------
43
44 ### xml reader for launch configuration file usage
45
46 class xml_parser:
47     def __init__(self, fileName, _opts ):
48         print "Processing ",fileName 
49         self.space = []
50         self.opts = _opts
51         self.do = 0
52         parser = xml.sax.make_parser()
53         parser.setContentHandler(self)
54         parser.parse(fileName)
55         pass
56
57     def boolValue( self, str ):
58         if str in ("yes", "y", "1"):
59             return 1
60         elif str in ("no", "n", "0"):
61             return 0
62         else:
63             return str
64         pass
65
66     def startElement(self, name, attrs):
67         self.space.append(name)
68         self.current = None
69
70         # if we are analyzing "section" element and its "name" attribute is "launch" -- set "do" to 1 
71         if self.space == [doc_tag, sec_tag] and \
72            nam_att in attrs.getNames() and \
73            attrs.getValue( nam_att ) == lanch_nam:
74             self.do = 1
75         # if we are analyzing "parameter" elements - children of "section launch" element, then store them
76         # in self.opts assiciative array (key = value of "name" attribute)
77         elif self.do == 1 and \
78              self.space == [doc_tag, sec_tag, par_tag] and \
79              nam_att in attrs.getNames() and \
80              val_att in attrs.getNames():
81             nam = attrs.getValue( nam_att )
82             val = attrs.getValue( val_att )
83             if nam in boolKeys:
84                 self.opts[nam] = self.boolValue( val )  # assign boolean value: 0 or 1
85             elif nam in listKeys:
86                 self.opts[nam] = val.split( ',' )       # assign list value: []
87             else:
88                 self.opts[nam] = val;
89             pass
90         pass
91
92     def endElement(self, name):
93         p = self.space.pop()
94         self.current = None
95         if self.do == 1 and name == sec_tag:
96             self.do = 0
97         pass
98
99     def characters(self, content):
100         pass
101
102     def processingInstruction(self, target, data):
103         pass
104
105     def setDocumentLocator(self, locator):
106         pass
107
108     def startDocument(self):
109         self.read = None
110         pass
111
112     def endDocument(self):
113         self.read = None
114         pass
115
116 # -----------------------------------------------------------------------------
117
118 ### searching for launch configuration files
119 # the rule:
120 # - environment variable {'appname'+'Config'} (SalomeAppConfig) contains list of directories (';' as devider)
121 # - these directories contain 'appname'+'.xml' (SalomeApp.xml) configuration files
122 # - these files are analyzed beginning with the last one (last directory in the list)
123 # - if a key is found in next analyzed cofiguration file - it will be replaced
124 # - the last configuration file to be analyzed - ~/.'appname'+'rc' (~/SalomeApprc) (if it exists)
125 # - but anyway, if user specifies a certain option in a command line - it will replace the values
126 # - specified in configuration file(s)
127 # - once again the order of settings (next setting replaces the previous ones):
128 # -     SalomeApp.xml files in directories specified by SalomeAppConfig env variable
129 # -     .SalomeApprc file in user's catalogue
130 # -     command line
131
132 config_var = appname+'Config'
133 dirs = os.environ[config_var]
134 dirs = dirs.split( ';' )
135 dirs.reverse() # reverse order, like in "path" variable - FILO-style processing
136
137 _opts = {} # assiciative array of options to be filled
138
139 # SalomeApp.xml files in directories specified by SalomeAppConfig env variable
140 for dir in dirs:
141     filename = dir+'/'+appname+'.xml'
142     try:
143         p = xml_parser(filename, _opts)
144     except:
145         print 'Can not read launch configuration file ', filename
146         continue
147     _opts = p.opts
148
149 # SalomeApprc file in user's catalogue
150 filename = os.environ['HOME']+'/.'+appname+'rc'
151 try:
152     p = xml_parser(filename, _opts)
153 except:
154     print 'Can not read launch configuration file ', filename
155
156
157 args = p.opts
158
159 # --- setting default values of keys if they were NOT set in config files ---
160 for aKey in listKeys:
161     if not args.has_key( aKey ):
162         args[aKey]=[]
163         
164 for aKey in boolKeys:
165     if not args.has_key( aKey ):
166         args[aKey]=0
167         
168 if args[file_nam]:
169     afile=args[file_nam]
170     args[file_nam]=[afile]
171     
172 args[appname_nam] = appname
173
174 ### searching for my port
175
176 my_port = 2809
177 try:
178   file = open(os.environ["OMNIORB_CONFIG"], "r")
179   s = file.read()
180   while len(s):
181     l = string.split(s, ":")
182     if string.split(l[0], " ")[0] == "ORBInitRef" or string.split(l[0], " ")[0] == "InitRef" :
183       my_port = int(l[len(l)-1])
184       pass
185     s = file.read()
186     pass
187 except:
188   pass
189
190 args[port_nam] = my_port
191
192 # -----------------------------------------------------------------------------
193
194 ### command line options reader
195
196 def options_parser(line):
197   source = line
198   list = []
199   for delimiter in [" ", ",", "="]:
200     for o in source:
201       list += string.split(o, delimiter)
202       pass
203     source = list
204     list = []
205     pass
206
207   print "source=",source
208   
209   result = {}
210   i = 0
211   while i < len(source):
212     if source[i][0] != '-':
213       key = None
214     elif source[i][1] == '-':
215       key = source[i][2]
216     else:
217       key = source[i][1]
218       pass
219     
220     result[key] = []
221     if key:
222       i += 1
223       pass
224     while i < len(source) and source[i][0] != '-':
225       result[key].append(source[i])
226       i += 1
227       pass
228     pass
229   return result
230
231 # -----------------------------------------------------------------------------
232
233 ### read command-line options : each arg given in command line supersedes arg from xml config file
234
235 try:
236     opts = options_parser(sys.argv[1:])
237     print "opts=",opts
238     kernel_root_dir=os.environ["KERNEL_ROOT_DIR"]
239 except:
240     opts["h"] = 1
241     pass
242
243 ### check all options are right
244
245 opterror=0
246 for opt in opts:
247     if not opt in ("h","g","l","f","x","m","e","s","c","p","k","t","i"):
248         print "command line error: -", opt
249         opterror=1
250
251 if opterror == 1:
252     opts["h"] = 1
253
254 if opts.has_key("h"):
255     print """USAGE: runSalome.py [options]
256     [command line options] :
257     --help or -h                  : print this help
258     --gui or -g                   : launching with GUI
259     --terminal -t                 : launching without gui (to deny --gui)
260     --logger or -l                : redirect messages in a CORBA collector
261     --file=filename or -f=filename: redirect messages in a log file  
262     --xterm or -x                 : execute servers in xterm console (messages appear in xterm windows)
263     --modules=module1,module2,... : salome module list (modulen is the name of Salome module to load)
264     or -m=module1,module2,...
265     --embedded=registry,study,moduleCatalog,cppContainer
266     or -e=registry,study,moduleCatalog,cppContainer
267                                   : embedded CORBA servers (default: registry,study,moduleCatalog,cppContainer)
268                                   : (logger,pyContainer,supervContainer can't be embedded
269     --standalone=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
270     or -s=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
271                                   : standalone CORBA servers (default: pyContainer,supervContainer)
272     --containers=cpp,python,superv: (obsolete) launching of containers cpp, python and supervision
273     or -c=cpp,python,superv       : = get default from -e and -s
274     --portkill or -p              : kill the salome with current port
275     --killall or -k               : kill all salome sessions
276     --interp=n or -i=n            : number of additional xterm to open, with session environment
277     
278     For each Salome module, the environment variable <modulen>_ROOT_DIR must be set.
279     The module name (<modulen>) must be uppercase.
280     KERNEL_ROOT_DIR is mandatory.
281     """
282     sys.exit(1)
283     pass
284
285 ### apply command-line options to the arguments
286 for opt in opts:
287     if opt == 'g':
288         args[gui_nam] = 1
289     elif opt == 'l':
290         args[logger_nam] = 1
291     elif opt == 'f':
292         args[file_nam] = opts['f']
293     elif opt == 'x':
294         args[xterm_nam] = 1
295     elif opt == 'i':
296         args[interp_nam] = opts['i']
297     elif opt == 'm':
298         args[modules_nam] = opts['m']
299     elif opt == 'e':
300         args[embedded_nam] = opts['e']
301     elif opt == 's':
302         args[standalone_nam] = opts['s']
303     elif opt == 'c':
304         args[containers_nam] = opts['c']
305     elif opt == 'p':
306         args[portkill_nam] = 1
307     elif opt == 'k':
308         args[killall_nam] = 1
309         pass
310     pass
311
312 # 'terminal' must be processed in the end: to deny any 'gui' options
313 if 't' in opts:
314     args[gui_nam] = 0
315     pass
316
317 print "args=",args