Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / bin / launchConfigureParser.py
1 import os, glob, string, sys
2 import xml.sax
3
4 # -----------------------------------------------------------------------------
5
6 ### xml reader for launch configuration file usage
7
8 class xml_parser:
9     def __init__(self, fileName):
10         self.space = []
11         self.opts = {}
12         parser = xml.sax.make_parser()
13         parser.setContentHandler(self)
14         parser.parse(fileName)
15         pass
16
17     def CorrectBoolean(self, str):
18         if str in ("yes", "y", "1"):
19             return 1
20         elif str in ("no", "n", "0"):
21             return 0
22         else:
23             return str
24         pass
25
26     def startElement(self, name, attrs):
27         #print "startElement name=",name
28         #print "startElement attrs=",attrs.getNames()
29         self.space.append(name)
30         self.current = None
31
32         if self.space[:2] == ["Configuration-list","launchoptions"] and len(self.space) == 3:
33             self.current = name
34         elif self.space == ["Configuration-list","modules-list"]:
35             self.opts["modules"] = []
36         elif self.space == ["Configuration-list","modules-list","module"] and "name" in attrs.getNames():
37             for field in attrs.getNames():
38                 if field == "name":
39                     self.currentModuleName = str(attrs.getValue("name"))
40                     self.opts["modules"].append(self.currentModuleName)
41                 else:
42                     self.opts[str(attrs.getValue("name"))+"_"+str(field)] = self.CorrectBoolean(attrs.getValue(field))
43                     pass
44                 pass
45         elif self.space == ["Configuration-list","modules-list","module","plugin"] and "name" in attrs.getNames():
46             key = str(self.currentModuleName)+"_plugins"
47             if not self.opts.has_key(key):
48                 self.opts[key]=[]
49                 pass
50             self.opts[key].append(attrs.getValue("name"))
51         elif self.space == ["Configuration-list","embedded-list"]:
52             self.opts["embedded"] = []
53             pass
54         elif self.space == ["Configuration-list","standalone-list"]:
55             self.opts["standalone"] = []
56             pass
57         elif self.space == ["Configuration-list","containers-list"]:
58             self.opts["containers"] = []
59             pass
60         pass
61
62     def endElement(self, name):
63         p = self.space.pop()
64         self.current = None
65         pass
66
67     def characters(self, content):
68         #print "Characters content:",content
69         if self.current:
70             self.opts[self.current] = self.CorrectBoolean(content)
71         elif self.space == ["Configuration-list","embedded-list", "embeddedserver"]:
72             self.opts["embedded"].append(content)
73         elif self.space == ["Configuration-list","standalone-list", "standaloneserver"]:
74             self.opts["standalone"].append(content)
75         elif self.space == ["Configuration-list","containers-list", "containertype"]:
76             self.opts["containers"].append(content)
77         pass
78
79     def processingInstruction(self, target, data):
80         pass
81
82     def setDocumentLocator(self, locator):
83         pass
84
85     def startDocument(self):
86         self.read = None
87         pass
88
89     def endDocument(self):
90         self.read = None
91         pass
92
93 # -----------------------------------------------------------------------------
94
95 ### searching for launch configuration file : $HOME/applipath()/salome.launch
96
97 appname="salome"
98 import Utils_Identity
99 versnb=Utils_Identity.version()
100 dirname = os.path.join(os.environ["HOME"],Utils_Identity.getapplipath())
101 filename=os.path.join(dirname,"salome.launch")
102
103 if not os.path.exists(filename):
104    print "Launch configuration file does not exist. Create default:",filename
105    os.system("mkdir -p "+dirname)
106    os.system("cp -f "+os.environ["KERNEL_ROOT_DIR"]+"/bin/salome/salome.launch "+filename)
107
108 ### get options from launch configuration file
109
110 try:
111     p = xml_parser(filename)
112 except:
113     print 'Can not read launch configuration file ', filename
114     filename = None
115     pass
116
117 if filename:
118     args = p.opts
119 else:
120     args = {}
121     pass
122
123 # --- args completion
124 for aKey in ("containers","embedded","key","modules","standalone"):
125     if not args.has_key(aKey):
126         args[aKey]=[]
127 for aKey in ("gui","logger","file","xterm","portkill","killall","interp"):
128     if not args.has_key(aKey):
129         args[aKey]=0
130 if args["file"]:
131     afile=args["file"]
132     args["file"]=[afile]
133 args["appname"] = appname
134
135 ### searching for my port
136
137 my_port = 2809
138 try:
139   file = open(os.environ["OMNIORB_CONFIG"], "r")
140   s = file.read()
141   while len(s):
142     l = string.split(s, ":")
143     if string.split(l[0], " ")[0] == "ORBInitRef" or string.split(l[0], " ")[0] == "InitRef" :
144       my_port = int(l[len(l)-1])
145       pass
146     s = file.read()
147     pass
148 except:
149   pass
150
151 args["port"] = my_port
152
153 # -----------------------------------------------------------------------------
154
155 ### command line options reader
156
157 def options_parser(line):
158   source = line
159   list = []
160   for delimiter in [" ", ",", "="]:
161     for o in source:
162       list += string.split(o, delimiter)
163       pass
164     source = list
165     list = []
166     pass
167
168   print "source=",source
169   
170   result = {}
171   i = 0
172   while i < len(source):
173     if source[i][0] != '-':
174       key = None
175     elif source[i][1] == '-':
176       key = source[i][2]
177     else:
178       key = source[i][1]
179       pass
180     
181     result[key] = []
182     if key:
183       i += 1
184       pass
185     while i < len(source) and source[i][0] != '-':
186       result[key].append(source[i])
187       i += 1
188       pass
189     pass
190   return result
191
192 # -----------------------------------------------------------------------------
193
194 ### read command-line options : each arg given in command line supersedes arg from xml config file
195
196 try:
197     opts = options_parser(sys.argv[1:])
198     print "opts=",opts
199     kernel_root_dir=os.environ["KERNEL_ROOT_DIR"]
200 except:
201     opts["h"] = 1
202     pass
203
204 ### check all options are right
205
206 opterror=0
207 for opt in opts:
208     if not opt in ("h","g","l","f","x","m","e","s","c","p","k","t","i"):
209         print "command line error: -", opt
210         opterror=1
211
212 if opterror == 1:
213     opts["h"] = 1
214
215 if opts.has_key("h"):
216     print """USAGE: runSalome.py [options]
217     [command line options] :
218     --help or -h                  : print this help
219     --gui or -g                   : launching with GUI
220     --terminal -t                 : launching without gui (to deny --gui)
221     --logger or -l                : redirect messages in a CORBA collector
222     --file=filename or -f=filename: redirect messages in a log file  
223     --xterm or -x                 : execute servers in xterm console (messages appear in xterm windows)
224     --modules=module1,module2,... : salome module list (modulen is the name of Salome module to load)
225     or -m=module1,module2,...
226     --embedded=registry,study,moduleCatalog,cppContainer
227     or -e=registry,study,moduleCatalog,cppContainer
228                                   : embedded CORBA servers (default: registry,study,moduleCatalog,cppContainer)
229                                   : (logger,pyContainer,supervContainer can't be embedded
230     --standalone=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
231     or -s=registry,study,moduleCatalog,cppContainer,pyContainer,supervContainer
232                                   : standalone CORBA servers (default: pyContainer,supervContainer)
233     --containers=cpp,python,superv: (obsolete) launching of containers cpp, python and supervision
234     or -c=cpp,python,superv       : = get default from -e and -s
235     --portkill or -p              : kill the salome with current port
236     --killall or -k               : kill all salome sessions
237     --interp=n or -i=n            : number of additional xterm to open, with session environment
238     
239     For each Salome module, the environment variable <modulen>_ROOT_DIR must be set.
240     The module name (<modulen>) must be uppercase.
241     KERNEL_ROOT_DIR is mandatory.
242     """
243     sys.exit(1)
244     pass
245
246 ### apply command-line options to the arguments
247 for opt in opts:
248     if opt == 'g':
249         args['gui'] = 1
250     elif opt == 'l':
251         args['logger'] = 1
252     elif opt == 'f':
253         args['file'] = opts['f']
254     elif opt == 'x':
255         args['xterm'] = 1
256     elif opt == 'i':
257         args['interp'] = opts['i']
258     elif opt == 'm':
259         args['modules'] = opts['m']
260     elif opt == 'e':
261         args['embedded'] = opts['e']
262     elif opt == 's':
263         args['standalone'] = opts['s']
264     elif opt == 'c':
265         args['containers'] = opts['c']
266     elif opt == 'p':
267         args['portkill'] = 1
268     elif opt == 'k':
269         args['killall'] = 1
270         pass
271     pass
272
273 # 'terminal' must be processed in the end: to deny any 'gui' options
274 if 't' in opts:
275     args['gui'] = 0
276     pass
277
278 print "args=",args