Salome HOME
63ef704368b98086317e147f5dc0b4225c41b517
[modules/kernel.git] / bin / appli_gen.py
1 #! /usr/bin/env python3
2 # Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 ## \file appli_gen.py
25 #  Create a %SALOME application (virtual Salome installation)
26 #
27 usage = """%(prog)s [options]
28 Typical use is:
29   python %(prog)s
30 Typical use with options is:
31   python %(prog)s --verbose --prefix=<install directory> --config=<configuration file>
32 """
33
34 import os
35 import sys
36 import shutil
37 import virtual_salome
38 import xml.sax
39 import optparse
40 import subprocess
41
42 # --- names of tags in XML configuration file
43 appli_tag   = "application"
44 prereq_tag  = "prerequisites"
45 context_tag = "context"
46 sha1_collect_tag = "sha1_collections"
47 system_conf_tag  = "system_conf"
48 modules_tag = "modules"
49 module_tag  = "module"
50 samples_tag = "samples"
51 extra_tests_tag = "extra_tests"
52 extra_test_tag = "extra_test"
53 resources_tag = "resources"
54 env_modules_tag = "env_modules"
55 env_module_tag = "env_module"
56 python_tag = "python"
57
58 # --- names of attributes in XML configuration file
59 nam_att  = "name"
60 path_att = "path"
61 gui_att  = "gui"
62 version_att = "version"
63
64 # -----------------------------------------------------------------------------
65
66 # --- xml reader for SALOME application configuration file
67
68 class xml_parser:
69     def __init__(self, fileName ):
70         print("Configure parser: processing %s ..." % fileName)
71         self.space = []
72         self.config = {}
73         self.config["modules"] = []
74         self.config["guimodules"] = []
75         self.config["extra_tests"] = []
76         self.config["env_modules"] = []
77         parser = xml.sax.make_parser()
78         parser.setContentHandler(self)
79         parser.parse(fileName)
80         pass
81
82     def boolValue( self, text):
83         if text in ("yes", "y", "1"):
84             return 1
85         elif text in ("no", "n", "0"):
86             return 0
87         else:
88             return text
89         pass
90
91     def startElement(self, name, attrs):
92         self.space.append(name)
93         self.current = None
94         # --- if we are analyzing "prerequisites" element then store its "path" attribute
95         if self.space == [appli_tag, prereq_tag] and path_att in attrs.getNames():
96             self.config["prereq_path"] = attrs.getValue( path_att )
97             pass
98         # --- if we are analyzing "context" element then store its "path" attribute
99         if self.space == [appli_tag, context_tag] and path_att in attrs.getNames():
100             self.config["context_path"] = attrs.getValue( path_att )
101             pass
102         # --- if we are analyzing "sha1_collection" element then store its "path" attribute
103         if self.space == [appli_tag, sha1_collect_tag] and path_att in attrs.getNames():
104             self.config["sha1_collect_path"] = attrs.getValue( path_att )
105             pass
106         # --- if we are analyzing "python" element then store its "version" attribute
107         if self.space == [appli_tag, python_tag] and version_att in attrs.getNames():
108             self.config["python_version"] = attrs.getValue( version_att )
109             pass
110         # --- if we are analyzing "system_conf" element then store its "path" attribute
111         if self.space == [appli_tag, system_conf_tag] and path_att in attrs.getNames():
112             self.config["system_conf_path"] = attrs.getValue( path_att )
113             pass
114         # --- if we are analyzing "resources" element then store its "path" attribute
115         if self.space == [appli_tag, resources_tag] and path_att in attrs.getNames():
116             self.config["resources_path"] = attrs.getValue( path_att )
117             pass
118         # --- if we are analyzing "samples" element then store its "path" attribute
119         if self.space == [appli_tag, samples_tag] and path_att in attrs.getNames():
120             self.config["samples_path"] = attrs.getValue( path_att )
121             pass
122         # --- if we are analyzing "module" element then store its "name" and "path" attributes
123         elif self.space == [appli_tag,modules_tag,module_tag] and \
124             nam_att in attrs.getNames() and \
125             path_att in attrs.getNames():
126             nam = attrs.getValue( nam_att )
127             path = attrs.getValue( path_att )
128             gui = 1
129             if gui_att in attrs.getNames():
130                 gui = self.boolValue(attrs.getValue( gui_att ))
131                 pass
132             self.config["modules"].append(nam)
133             self.config[nam]=path
134             if gui:
135                 self.config["guimodules"].append(nam)
136                 pass
137             pass
138         # --- if we are analyzing "env_module" element then store its "name" attribute
139         elif self.space == [appli_tag, env_modules_tag, env_module_tag] and \
140                 nam_att in attrs.getNames():
141             nam = attrs.getValue( nam_att )
142             self.config["env_modules"].append(nam)
143             pass
144         # --- if we are analyzing "extra_test" element then store its "name" and "path" attributes
145         elif self.space == [appli_tag,extra_tests_tag,extra_test_tag] and \
146             nam_att in attrs.getNames() and \
147             path_att in attrs.getNames():
148             nam = attrs.getValue( nam_att )
149             path = attrs.getValue( path_att )
150             self.config["extra_tests"].append(nam)
151             self.config[nam]=path
152             pass
153         pass
154
155     def endElement(self, name):
156         self.space.pop()
157         self.current = None
158         pass
159
160     def characters(self, content):
161         pass
162
163     def processingInstruction(self, target, data):
164         pass
165
166     def setDocumentLocator(self, locator):
167         pass
168
169     def startDocument(self):
170         self.read = None
171         pass
172
173     def endDocument(self):
174         self.read = None
175         pass
176
177 # -----------------------------------------------------------------------------
178
179 class params:
180     pass
181
182 # -----------------------------------------------------------------------------
183
184 def makedirs(namedir):
185   if os.path.exists(namedir):
186     dirbak = namedir+".bak"
187     if os.path.exists(dirbak):
188       shutil.rmtree(dirbak)
189     os.rename(namedir, dirbak)
190     os.listdir(dirbak) #sert seulement a mettre a jour le systeme de fichier sur certaines machines
191   os.makedirs(namedir)
192
193 def install(prefix, config_file, verbose=0):
194     home_dir = os.path.abspath(os.path.expanduser(prefix))
195     filename = os.path.abspath(os.path.expanduser(config_file))
196     _config = {}
197     try:
198         parser = xml_parser(filename)
199         _config = parser.config
200     except xml.sax.SAXParseException as inst:
201         print(inst.getMessage())
202         print("Configure parser: parse error in configuration file %s" % filename)
203         pass
204     except xml.sax.SAXException as inst:
205         print(inst.args)
206         print("Configure parser: error in configuration file %s" % filename)
207         pass
208     except:
209         print("Configure parser: Error : can not read configuration file %s, check existence and rights" % filename)
210         pass
211
212     if verbose:
213         for cle,val in _config.items():
214             print(cle, val)
215             pass
216
217     # Remove CTestTestfile.cmake; this file will be filled by successive calls to link_module and link_extra_test
218     try:
219       ctest_file = os.path.join(home_dir, 'bin', 'salome', 'test', "CTestTestfile.cmake")
220       os.remove(ctest_file)
221     except:
222       pass
223
224     for module in _config.get("modules", []):
225         if module in _config:
226             print("--- add module ", module, _config[module])
227             options = params()
228             options.verbose = verbose
229             options.clear = 0
230             options.prefix = home_dir
231             options.module_name = module
232             options.module_path = _config[module]
233             virtual_salome.link_module(options)
234             # To fix GEOM_TestXAO issue https://codev-tuleap.cea.fr/plugins/tracker/?aid=16599
235             if module == "GEOM":
236                 # link <appli_path>/bin/salome/test/<module> to <module_path>/bin/salome/test
237                 test_dir=os.path.join(home_dir,'bin','salome', 'test')
238                 module_dir=os.path.abspath(options.module_path)
239                 xao_link=os.path.join(module_dir,'bin','salome', 'test', "xao")
240                 print("link %s --> %s"%(os.path.join(test_dir, "xao"), xao_link))
241                 virtual_salome.symlink(xao_link, os.path.join(test_dir, "xao"))
242             pass
243         pass
244
245     for extra_test in _config.get("extra_tests", []):
246         if extra_test in _config:
247             print("--- add extra test ", extra_test, _config[extra_test])
248             options = params()
249             options.verbose = verbose
250             options.clear = 0
251             options.prefix = home_dir
252             options.extra_test_name = extra_test
253             options.extra_test_path = _config[extra_test]
254             virtual_salome.link_extra_test(options)
255             pass
256         pass
257
258     # Sort test labels by name in generated CTestTestfile.cmake
259     with open(ctest_file) as f:
260         lines = f.readlines()
261     lines.sort()
262     with open(ctest_file, "w") as f:
263         f.write("".join(lines))
264
265     # Generate CTestCustom.cmake to handle long output
266     ctest_custom = os.path.join(home_dir, 'bin', 'salome', 'test', "CTestCustom.cmake")
267     with open(ctest_custom, 'w') as f:
268       f.write("SET(CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE 1048576) # 1MB\n")
269       f.write("SET(CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE 1048576) # 1MB\n")
270
271     appliskel_dir = os.path.join(prefix, 'bin', 'salome', 'appliskel')
272
273     for fn in ('envd',
274                'getAppliPath.py',
275                'kill_remote_containers.py',
276                'runRemote.sh',
277                'runRemoteSSL.sh',
278                '.salome_run',
279                'update_catalogs.py',
280                '.bashrc',
281                ):
282         virtual_salome.symlink( os.path.join( appliskel_dir, fn ), os.path.join( home_dir, fn) )
283         pass
284
285     if filename != os.path.join(home_dir,"config_appli.xml"):
286         shutil.copyfile(filename, os.path.join(home_dir,"config_appli.xml"))
287         pass
288
289     # Creation of env.d directory
290     virtual_salome.mkdir(os.path.join(home_dir,'env.d'))
291
292     # Get the env modules which will be loaded
293     # In the same way as: module load [MODULE_LIST]
294     env_modules = _config.get('env_modules', [])
295     if env_modules:
296         with open(os.path.join(home_dir, 'env.d', 'envModules.sh'), 'w') as fd:
297             fd.write('#!/bin/bash\n')
298             fd.write('module load %s\n' % (' '.join(env_modules)))
299
300     # Copy salome / salome_mesa scripts:
301
302     for scripts in ('salome', 'salome_mesa', 'salome_common.py'):
303         salome_script = open(os.path.join(appliskel_dir, scripts)).read()
304         salome_file = os.path.join(home_dir, scripts)
305         try:
306             os.remove(salome_file)
307         except Exception:
308             pass
309         with open(salome_file, 'w') as fd:
310             fd.write(salome_script.replace('MODULES = []', 'MODULES = {}'.format(env_modules)))
311             os.chmod(salome_file, 0o755)
312
313     # Add .salome-completion.sh file
314     shutil.copyfile(os.path.join(appliskel_dir, ".salome-completion.sh"),
315                     os.path.join(home_dir, ".salome-completion.sh"))
316
317     if "prereq_path" in _config and os.path.isfile(_config["prereq_path"]):
318         shutil.copyfile(_config["prereq_path"],
319                         os.path.join(home_dir, 'env.d', 'envProducts.sh'))
320         pass
321     else:
322         print("WARNING: prerequisite file does not exist")
323         pass
324
325     if "context_path" in _config and os.path.isfile(_config["context_path"]):
326         shutil.copyfile(_config["context_path"],
327                         os.path.join(home_dir, 'env.d', 'envProducts.cfg'))
328         pass
329     else:
330         print("WARNING: context file does not exist")
331         pass
332
333     if "sha1_collect_path" in _config and os.path.isfile(_config["sha1_collect_path"]):
334         shutil.copyfile(_config["sha1_collect_path"],
335                         os.path.join(home_dir, 'sha1_collections.txt'))
336         pass
337     else:
338         print("WARNING: sha1 collections file does not exist")
339         pass
340
341     if "system_conf_path" in _config and os.path.isfile(_config["system_conf_path"]):
342         shutil.copyfile(_config["system_conf_path"],
343                         os.path.join(home_dir, 'env.d', 'envConfSystem.sh'))
344         pass
345
346     # Create environment file: configSalome.sh
347
348     if "python_version" in _config:
349        versionPython_split = _config["python_version"].split('.')
350        versionPython = versionPython_split[0] + "." + versionPython_split[1]
351     else:
352        cmd='source %s && python3 -c "import sys ; sys.stdout.write(\\"{}.{}\\".format(sys.version_info.major,sys.version_info.minor))"' %(_config["prereq_path"])
353        versionPython=subprocess.check_output(['/bin/bash', '-l' ,'-c',cmd]).decode("utf-8")
354
355     with open(os.path.join(home_dir, 'env.d', 'configSalome.sh'),'w') as f:
356         for module in _config.get("modules", []):
357             command = 'export '+ module + '_ROOT_DIR=${HOME}/${APPLI}\n'
358             f.write(command)
359             pass
360         if "samples_path" in _config:
361             command = 'export DATA_DIR=' + _config["samples_path"] +'\n'
362             f.write(command)
363             pass
364         if "resources_path" in _config and os.path.isfile(_config["resources_path"]):
365             command = 'export USER_CATALOG_RESOURCES_FILE=' + os.path.abspath(_config["resources_path"]) +'\n'
366             f.write(command)
367         # Note: below, PYTHONPATH should not be extended to bin/salome! Python modules must be installed in lib/pythonX.Y, to be fixed (e.g. Kernel SALOME_Container.py)
368         command ="""export PATH=${HOME}/${APPLI}/bin/salome:$PATH
369 export PYTHONPATH=${HOME}/${APPLI}/lib/python%s/site-packages/salome:$PYTHONPATH
370 export PYTHONPATH=${HOME}/${APPLI}/lib/salome:$PYTHONPATH
371 export PYTHONPATH=${HOME}/${APPLI}/bin/salome:$PYTHONPATH
372 export LD_LIBRARY_PATH=${HOME}/${APPLI}/lib/salome:$LD_LIBRARY_PATH
373 """ %versionPython
374         f.write(command)
375         # Create environment variable for the salome test
376         for module in _config.get("modules", []):
377             command = "export LD_LIBRARY_PATH=${HOME}/${APPLI}/bin/salome/test/" + module + "/lib:$LD_LIBRARY_PATH\n"
378             f.write(command)
379             pass
380         # Create environment for plugins GEOM
381         command = "export GEOM_PluginsList=BREPPlugin:STEPPlugin:IGESPlugin:STLPlugin:XAOPlugin:VTKPlugin:AdvancedGEOM\n"
382         f.write(command)
383         # Create environment for Healing
384         command = "export CSF_ShHealingDefaults=${HOME}/${APPLI}/share/salome/resources/geom\n"
385         f.write(command)
386         # Create environment for Meshers
387         command = "export SMESH_MeshersList=StdMeshers:HYBRIDPlugin:HexoticPLUGIN:GMSHPlugin:GHS3DPlugin:NETGENPlugin:HEXABLOCKPlugin:BLSURFPlugin:GHS3DPRLPlugin\nexport SALOME_StdMeshersResources=${HOME}/${APPLI}/share/salome/resources/smesh\n"
388         f.write(command)
389
390     # Create configuration file: configSalome.cfg
391     with open(os.path.join(home_dir, 'env.d', 'configSalome.cfg'),'w') as f:
392         command = "[SALOME ROOT_DIR (modules) Configuration]\n"
393         f.write(command)
394         for module in _config.get("modules", []):
395             command = module + '_ROOT_DIR=${HOME}/${APPLI}\n'
396             f.write(command)
397             pass
398         if "samples_path" in _config:
399             command = 'DATA_DIR=' + _config["samples_path"] +'\n'
400             f.write(command)
401             pass
402         if "resources_path" in _config and os.path.isfile(_config["resources_path"]):
403             command = 'USER_CATALOG_RESOURCES_FILE=' + os.path.abspath(_config["resources_path"]) +'\n'
404             f.write(command)
405         command ="""ADD_TO_PATH: ${HOME}/${APPLI}/bin/salome
406 ADD_TO_PYTHONPATH: ${HOME}/${APPLI}/lib/python%s/site-packages/salome
407 ADD_TO_PYTHONPATH: ${HOME}/${APPLI}/lib/salome
408 ADD_TO_LD_LIBRARY_PATH: ${HOME}/${APPLI}/lib/salome
409 """%versionPython
410         f.write(command)
411         for module in _config.get("modules", []):
412             command = "ADD_TO_LD_LIBRARY_PATH: ${HOME}/${APPLI}/bin/salome/test/" + module + "/lib\n"
413             f.write(command)
414             pass
415         # Create environment for plugins GEOM
416         command = "GEOM_PluginsList=BREPPlugin:STEPPlugin:IGESPlugin:STLPlugin:XAOPlugin:VTKPlugin:AdvancedGEOM\n"
417         f.write(command)
418         # Create environment for Healing
419         command = "CSF_ShHealingDefaults=${HOME}/${APPLI}/share/salome/resources/geom\n"
420         f.write(command)
421         # Create environment for Meshers
422         command = "SMESH_MeshersList=StdMeshers:HYBRIDPlugin:HexoticPLUGIN:GMSHPlugin:GHS3DPlugin:NETGENPlugin:HEXABLOCKPlugin:BLSURFPlugin:GHS3DPRLPlugin\nSALOME_StdMeshersResources=${HOME}/${APPLI}/share/salome/resources/smesh\n"
423         f.write(command)
424
425
426     # Create environment file: configGUI.sh
427     dirs_ress_icon = []
428     salomeappname  = "SalomeApp"
429     with open(os.path.join(home_dir, 'env.d', 'configGUI.sh'),'w') as f:
430         for module in _config.get("modules", []):
431             if module not in ["KERNEL", "GUI", ""]:
432                 d = os.path.join(_config[module],"share","salome","resources",module.lower())
433                 d_appli = os.path.join("${HOME}","${APPLI}","share","salome","resources",module.lower())
434                 if os.path.exists( os.path.join(d,"{0}.xml".format(salomeappname)) ):
435                    dirs_ress_icon.append( d_appli )
436         AppConfig="export SalomeAppConfig=${HOME}/${APPLI}:${HOME}/${APPLI}/share/salome/resources/gui/"
437         for dir_module in dirs_ress_icon:
438              AppConfig=AppConfig+":"+dir_module
439         f.write(AppConfig+"\n")
440         command = """export SUITRoot=${HOME}/${APPLI}/share/salome
441 export DISABLE_FPE=1
442 export MMGT_REENTRANT=1
443 """
444         f.write(command)
445
446     # Create configuration file: configGUI.cfg
447     dirs_ress_icon = []
448     with open(os.path.join(home_dir, 'env.d', 'configGUI.cfg'),'w') as f:
449         command = """[SALOME GUI Configuration]\n"""
450         f.write(command)
451         for module in _config.get("modules", []):
452             if module not in ["KERNEL", "GUI", ""]:
453                 d = os.path.join(_config[module],"share","salome","resources",module.lower())
454                 d_appli = os.path.join("${HOME}","${APPLI}","share","salome","resources",module.lower())
455                 if os.path.exists( os.path.join(d,"{0}.xml".format(salomeappname)) ):
456                    dirs_ress_icon.append( d_appli )
457         AppConfig="SalomeAppConfig=${HOME}/${APPLI}:${HOME}/${APPLI}/share/salome/resources/gui/"
458         for dir_module in dirs_ress_icon:
459              AppConfig=AppConfig+":"+dir_module
460         f.write(AppConfig+"\n")
461         command = """SUITRoot=${HOME}/${APPLI}/share/salome
462 DISABLE_FPE=1
463 MMGT_REENTRANT=1
464 """
465         f.write(command)
466
467     #SalomeApp.xml file
468     with open(os.path.join(home_dir,'SalomeApp.xml'),'w') as f:
469         command = """<document>
470   <section name="launch">
471     <!-- SALOME launching parameters -->
472     <parameter name="gui"        value="yes"/>
473     <parameter name="splash"     value="yes"/>
474     <parameter name="file"       value="no"/>
475     <parameter name="key"        value="no"/>
476     <parameter name="interp"     value="no"/>
477     <parameter name="logger"     value="no"/>
478     <parameter name="xterm"      value="no"/>
479     <parameter name="portkill"   value="no"/>
480     <parameter name="killall"    value="no"/>
481     <parameter name="noexcepthandler"  value="no"/>
482     <parameter name="modules"    value="%s"/>
483     <parameter name="pyModules"  value=""/>
484     <parameter name="embedded"   value="SalomeAppEngine,study,cppContainer,registry,moduleCatalog"/>
485     <parameter name="standalone" value=""/>
486   </section>
487 </document>
488 """
489         mods = []
490         #Keep all modules except KERNEL and GUI
491         for module in _config.get("modules", []):
492             if module in ("KERNEL","GUI"):
493                 continue
494             mods.append(module)
495         f.write(command % ",".join(mods))
496
497     #Add USERS directory with 777 permission to store users configuration files
498     users_dir = os.path.join(home_dir,'USERS')
499     makedirs(users_dir)
500     os.chmod(users_dir, 0o777)
501
502 def main():
503     parser = optparse.OptionParser(usage=usage)
504
505     parser.add_option('--prefix', dest="prefix", default='.',
506                       help="Installation directory (default .)")
507
508     parser.add_option('--config', dest="config", default='config_appli.xml',
509                       help="XML configuration file (default config_appli.xml)")
510
511     parser.add_option('-v', '--verbose', action='count', dest='verbose',
512                       default=0, help="Increase verbosity")
513
514     options, args = parser.parse_args()
515     if not os.path.exists(options.config):
516         print("ERROR: config file %s does not exist. It is mandatory." % options.config)
517         sys.exit(1)
518
519     install(prefix=options.prefix, config_file=options.config, verbose=options.verbose)
520     pass
521
522 # -----------------------------------------------------------------------------
523
524 if __name__ == '__main__':
525     main()
526     pass