Salome HOME
6d70fe9392d966df41d987ae193eeae8c6d1fdfd
[modules/kernel.git] / bin / appli_gen.py
1 #!/usr/bin/env python
2 """Create a virtual Salome installation
3
4 """
5 usage="""usage: %prog [options]
6 Typical use is:
7   python appli_gen.py 
8 Use with options:
9   python appli_gen.py --prefix=<install directory> --config=<configuration file>
10 """
11
12 import os, glob, string, sys, re
13 import xml.sax
14 import optparse
15 import virtual_salome
16
17 # --- names of tags in XML configuration file
18 appli_tag   = "application"
19 prereq_tag  = "prerequisites"
20 modules_tag = "modules"
21 module_tag  = "module"
22 samples_tag = "samples"
23
24 # --- names of attributes in XML configuration file
25 nam_att  = "name"
26 path_att = "path"
27 gui_att  = "gui"
28
29 # -----------------------------------------------------------------------------
30
31 # --- xml reader for SALOME application configuration file
32
33 class xml_parser:
34     def __init__(self, fileName ):
35         print "Configure parser: processing %s ..." % fileName
36         self.space = []
37         self.config = {}
38         self.config["modules"] = []
39         self.config["guimodules"] = []
40         parser = xml.sax.make_parser()
41         parser.setContentHandler(self)
42         parser.parse(fileName)
43         pass
44
45     def boolValue( self, str ):
46         if str in ("yes", "y", "1"):
47             return 1
48         elif str in ("no", "n", "0"):
49             return 0
50         else:
51             return str
52         pass
53
54     def startElement(self, name, attrs):
55         self.space.append(name)
56         self.current = None
57         # --- if we are analyzing "prerequisites" element then store its "path" attribute
58         if self.space == [appli_tag, prereq_tag] and path_att in attrs.getNames():
59             self.config["prereq_path"] = attrs.getValue( path_att )
60             pass
61         # --- if we are analyzing "samples" element then store its "path" attribute
62         if self.space == [appli_tag, samples_tag] and path_att in attrs.getNames():
63             self.config["samples_path"] = attrs.getValue( path_att )
64             pass
65         # --- if we are analyzing "module" element then store its "name" and "path" attributes
66         elif self.space == [appli_tag,modules_tag,module_tag] and \
67             nam_att in attrs.getNames() and \
68             path_att in attrs.getNames():
69             nam = attrs.getValue( nam_att )
70             path = attrs.getValue( path_att )
71             gui = 1
72             if gui_att in attrs.getNames():
73                 gui = self.boolValue(attrs.getValue( gui_att ))
74                 pass
75             self.config["modules"].append(nam)
76             self.config[nam]=path
77             if gui:
78                 self.config["guimodules"].append(nam)
79                 pass
80             pass
81         pass
82
83     def endElement(self, name):
84         p = self.space.pop()
85         self.current = None
86         pass
87
88     def characters(self, content):
89         pass
90
91     def processingInstruction(self, target, data):
92         pass
93
94     def setDocumentLocator(self, locator):
95         pass
96
97     def startDocument(self):
98         self.read = None
99         pass
100
101     def endDocument(self):
102         self.read = None
103         pass
104
105 # -----------------------------------------------------------------------------
106
107 class params:
108     pass
109
110 # -----------------------------------------------------------------------------
111
112 def install(prefix,config_file):
113     home_dir=os.path.abspath(os.path.expanduser(prefix))
114     filename=os.path.abspath(os.path.expanduser(config_file))
115     _config={}
116     try:
117         p = xml_parser(filename)
118         _config = p.config
119     except xml.sax.SAXParseException, inst:
120         print inst.getMessage()
121         print "Configure parser: parse error in configuration file %s" % filename
122         pass
123     except xml.sax.SAXException, inst:
124         print inst.args   
125         print "Configure parser: error in configuration file %s" % filename
126         pass
127     except:
128         print "Configure parser: Error : can not read configuration file %s, check existence and rights" % filename
129         pass
130
131     for cle in _config.keys():
132         print cle, _config[cle]
133         pass
134
135     for module in _config["modules"]:
136         print "--- add module ", module, _config[module]
137         options = params()
138         options.verbose=0
139         options.clear=0
140         options.prefix=home_dir
141         options.module=_config[module]
142         virtual_salome.link_module(options)
143         pass
144
145     appliskel_dir=os.path.join(home_dir,'bin','salome','appliskel')
146
147     for fn in ('envd',
148                'setAppliPath.sh',
149                'searchFreePort.sh',
150                'runRemote.sh',
151                'runAppli',
152                'runConsole',
153                'runSession',
154                'runTests',
155                '.bashrc',
156                ):
157         virtual_salome.symlink(os.path.join(appliskel_dir, fn),os.path.join(home_dir, fn))
158         pass
159
160     if filename != os.path.join(home_dir,"config_appli.xml"):
161         command = "cp -p " + filename + ' ' + os.path.join(home_dir,"config_appli.xml")
162         os.system(command)
163         pass
164        
165     virtual_salome.mkdir(os.path.join(home_dir,'env.d'))
166     if os.path.isfile(_config["prereq_path"]):
167         command='cp -p ' + _config["prereq_path"] + ' ' + os.path.join(home_dir,'env.d','envProducts.sh')
168         os.system(command)
169         pass
170     else:
171         print "WARNING: prerequisite file does not exist"
172         pass
173
174
175     f =open(os.path.join(home_dir,'env.d','configSalome.sh'),'w')
176     for module in _config["modules"]:
177         command='export '+ module + '_ROOT_DIR=' + home_dir +'\n'
178         f.write(command)
179         pass
180     if _config.has_key("samples_path"):
181         command='export DATA_DIR=' + _config["samples_path"] +'\n'
182         f.write(command)
183         pass
184     f.close()
185
186
187     f =open(os.path.join(home_dir,'env.d','configGUI.sh'),'w')
188     command = 'export SalomeAppConfig=' + home_dir +'\n'
189     f.write(command)
190     command = 'export SUITRoot=' + os.path.join(home_dir,'share','salome') +'\n'
191     f.write(command)
192     f.write('export DISABLE_FPE=1\n')
193     f.write('export MMGT_REENTRANT=1\n')
194     f.close()
195
196
197     f =open(os.path.join(home_dir,'SalomeApp.xml'),'w')
198     command="""<document>
199   <section name="launch">
200     <!-- SALOME launching parameters -->
201     <parameter name="gui"        value="yes"/>
202     <parameter name="splash"     value="yes"/>
203     <parameter name="file"       value="no"/>
204     <parameter name="key"        value="no"/>
205     <parameter name="interp"     value="no"/>
206     <parameter name="logger"     value="no"/>
207     <parameter name="xterm"      value="no"/>
208     <parameter name="portkill"   value="no"/>
209     <parameter name="killall"    value="no"/>
210     <parameter name="noexcepthandler"  value="no"/>
211     <parameter name="modules"    value="""
212     f.write(command)    
213     f.write('"')
214     for module in _config["guimodules"][:-1]:
215         f.write(module)
216         f.write(',')
217         pass
218     f.write(_config["guimodules"][-1])
219     f.write('"/>')
220     command="""
221     <parameter name="pyModules"  value=""/>
222     <parameter name="embedded"   value="SalomeAppEngine,study,cppContainer,registry,moduleCatalog"/>
223     <parameter name="standalone" value="pyContainer,supervContainer"/>
224   </section>
225 </document>
226 """
227     f.write(command)    
228     f.close()
229
230 def main():
231     parser = optparse.OptionParser(usage=usage)
232
233     parser.add_option('--prefix', dest="prefix", default='.',
234                       help="Installation directory (default .)")
235
236     parser.add_option('--config', dest="config", default='config_appli.xml',
237                       help="XML configuration file (default config_appli.xml)")
238
239     options, args = parser.parse_args()
240     install(prefix=options.prefix,config_file=options.config)
241     pass
242
243 # -----------------------------------------------------------------------------
244
245 if __name__ == '__main__':
246     main()
247     pass