1 #! /usr/bin/env python3
2 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 # Create a %SALOME application (virtual Salome installation)
37 usage = """%(prog)s [options]
40 Typical use with options is:
41 python %(prog)s --verbose --prefix=<install directory> --config=<configuration file>
44 # --- names of tags in XML configuration file
45 appli_tag = "application"
46 prereq_tag = "prerequisites"
47 context_tag = "context"
48 venv_directory_tag = "venv_directory"
49 sha1_collect_tag = "sha1_collections"
50 system_conf_tag = "system_conf"
51 modules_tag = "modules"
53 samples_tag = "samples"
54 extra_tests_tag = "extra_tests"
55 extra_test_tag = "extra_test"
56 resources_tag = "resources"
57 env_modules_tag = "env_modules"
58 env_module_tag = "env_module"
61 # --- names of attributes in XML configuration file
65 version_att = "version"
66 # -----------------------------------------------------------------------------
69 # --- xml reader for SALOME application configuration file
72 def __init__(self, fileName):
73 print("Configure parser: processing %s ..." % fileName)
76 self.config["modules"] = []
77 self.config["guimodules"] = []
78 self.config["extra_tests"] = []
79 self.config["env_modules"] = []
80 parser = xml.sax.make_parser()
81 parser.setContentHandler(self)
82 parser.parse(fileName)
85 def boolValue(self, text):
86 if text in ("yes", "y", "1"):
88 elif text in ("no", "n", "0"):
94 def startElement(self, name, attrs):
95 self.space.append(name)
97 # --- if we are analyzing "prerequisites" element then store its "path" attribute
98 if self.space == [appli_tag, prereq_tag] and path_att in attrs.getNames():
99 self.config["prereq_path"] = attrs.getValue( path_att )
101 # --- if we are analyzing "context" element then store its "path" attribute
102 if self.space == [appli_tag, context_tag] and path_att in attrs.getNames():
103 self.config["context_path"] = attrs.getValue( path_att )
105 # --- if we are analyzing "venv_directory" element then store its "path" attribute
106 if self.space == [appli_tag, venv_directory_tag] and path_att in attrs.getNames():
107 self.config["venv_directory_path"] = attrs.getValue( path_att )
109 # --- if we are analyzing "sha1_collection" element then store its "path" attribute
110 if self.space == [appli_tag, sha1_collect_tag] and path_att in attrs.getNames():
111 self.config["sha1_collect_path"] = attrs.getValue( path_att )
113 # --- if we are analyzing "python" element then store its "version" attribute
114 if self.space == [appli_tag, python_tag] and version_att in attrs.getNames():
115 self.config["python_version"] = attrs.getValue( version_att )
117 # --- if we are analyzing "system_conf" element then store its "path" attribute
118 if self.space == [appli_tag, system_conf_tag] and path_att in attrs.getNames():
119 self.config["system_conf_path"] = attrs.getValue( path_att )
121 # --- if we are analyzing "resources" element then store its "path" attribute
122 if self.space == [appli_tag, resources_tag] and path_att in attrs.getNames():
123 self.config["resources_path"] = attrs.getValue( path_att )
125 # --- if we are analyzing "samples" element then store its "path" attribute
126 if self.space == [appli_tag, samples_tag] and path_att in attrs.getNames():
127 self.config["samples_path"] = attrs.getValue( path_att )
129 # --- if we are analyzing "module" element then store its "name" and "path" attributes
130 elif self.space == [appli_tag,modules_tag,module_tag] and \
131 nam_att in attrs.getNames() and \
132 path_att in attrs.getNames():
133 nam = attrs.getValue( nam_att )
134 path = attrs.getValue( path_att )
136 if gui_att in attrs.getNames():
137 gui = self.boolValue(attrs.getValue( gui_att ))
139 self.config["modules"].append(nam)
140 self.config[nam]=path
142 self.config["guimodules"].append(nam)
145 # --- if we are analyzing "env_module" element then store its "name" attribute
146 elif self.space == [appli_tag, env_modules_tag, env_module_tag] and \
147 nam_att in attrs.getNames():
148 nam = attrs.getValue( nam_att )
149 self.config["env_modules"].append(nam)
151 # --- if we are analyzing "extra_test" element then store its "name" and "path" attributes
152 elif self.space == [appli_tag,extra_tests_tag,extra_test_tag] and \
153 nam_att in attrs.getNames() and \
154 path_att in attrs.getNames():
155 nam = attrs.getValue( nam_att )
156 path = attrs.getValue( path_att )
157 self.config["extra_tests"].append(nam)
158 self.config[nam]=path
162 def endElement(self, name):
167 def characters(self, content):
170 def processingInstruction(self, target, data):
173 def setDocumentLocator(self, locator):
176 def startDocument(self):
180 def endDocument(self):
184 # -----------------------------------------------------------------------------
189 # -----------------------------------------------------------------------------
191 def makedirs(namedir):
192 if os.path.exists(namedir):
193 dirbak = namedir+".bak"
194 if os.path.exists(dirbak):
195 shutil.rmtree(dirbak)
196 os.rename(namedir, dirbak)
197 os.listdir(dirbak) #sert seulement a mettre a jour le systeme de fichier sur certaines machines
200 def install(prefix, config_file, verbose=0):
201 home_dir = os.path.abspath(os.path.expanduser(prefix))
202 filename = os.path.abspath(os.path.expanduser(config_file))
205 parser = xml_parser(filename)
206 _config = parser.config
207 except xml.sax.SAXParseException as inst:
208 print(inst.getMessage())
209 print("Configure parser: parse error in configuration file %s" % filename)
211 except xml.sax.SAXException as inst:
213 print("Configure parser: error in configuration file %s" % filename)
216 print("Configure parser: Error : can not read configuration file %s, check existence and rights" % filename)
220 for cle,val in _config.items():
224 # Remove CTestTestfile.cmake; this file will be filled by successive calls to link_module and link_extra_test
226 ctest_file = os.path.join(home_dir, 'bin', 'salome', 'test', "CTestTestfile.cmake")
227 os.remove(ctest_file)
231 for module in _config.get("modules", []):
232 if module in _config:
233 print("--- add module ", module, _config[module])
235 options.verbose = verbose
237 options.prefix = home_dir
238 options.module_name = module
239 options.module_path = _config[module]
240 virtual_salome.link_module(options)
241 # To fix GEOM_TestXAO issue https://codev-tuleap.cea.fr/plugins/tracker/?aid=16599
243 # link <appli_path>/bin/salome/test/<module> to <module_path>/bin/salome/test
244 test_dir=os.path.join(home_dir,'bin','salome', 'test')
245 module_dir=os.path.abspath(options.module_path)
246 xao_link=os.path.join(module_dir,'bin','salome', 'test', "xao")
247 print("link %s --> %s"%(os.path.join(test_dir, "xao"), xao_link))
248 virtual_salome.symlink(xao_link, os.path.join(test_dir, "xao"))
252 for extra_test in _config.get("extra_tests", []):
253 if extra_test in _config:
254 print("--- add extra test ", extra_test, _config[extra_test])
256 options.verbose = verbose
258 options.prefix = home_dir
259 options.extra_test_name = extra_test
260 options.extra_test_path = _config[extra_test]
261 virtual_salome.link_extra_test(options)
265 # Sort test labels by name in generated CTestTestfile.cmake
266 with open(ctest_file) as f:
267 lines = f.readlines()
269 with open(ctest_file, "w") as f:
270 f.write("".join(lines))
272 # Generate CTestCustom.cmake to handle long output
273 ctest_custom = os.path.join(home_dir, 'bin', 'salome', 'test', "CTestCustom.cmake")
274 with open(ctest_custom, 'w') as f:
275 f.write("SET(CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE 1048576) # 1MB\n")
276 f.write("SET(CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE 1048576) # 1MB\n")
278 appliskel_dir = os.path.join(prefix, 'bin', 'salome', 'appliskel')
282 'kill_remote_containers.py',
289 'update_catalogs.py',
292 virtual_salome.symlink( os.path.join( appliskel_dir, fn ), os.path.join( home_dir, fn) )
295 if filename != os.path.join(home_dir,"config_appli.xml"):
296 shutil.copyfile(filename, os.path.join(home_dir,"config_appli.xml"))
299 # Creation of env.d directory
300 virtual_salome.mkdir(os.path.join(home_dir,'env.d'))
302 venv_directory_path = _config.get('venv_directory_path')
303 if venv_directory_path and os.path.isdir(venv_directory_path):
304 virtual_salome.symlink(venv_directory_path, os.path.join(home_dir, "venv"))
306 # Get the env modules which will be loaded
307 # In the same way as: module load [MODULE_LIST]
308 env_modules = _config.get('env_modules', [])
310 with open(os.path.join(home_dir, 'env_modules.json'), 'w') as fd:
311 json.dump({"env_modules": env_modules}, fd)
312 with open(os.path.join(home_dir, 'env.d', 'envModules.sh'), 'w') as fd:
313 fd.write('#!/bin/bash\n')
314 fd.write('module load %s\n' % (' '.join(env_modules)))
316 # Add .salome-completion.sh file
317 shutil.copyfile(os.path.join(appliskel_dir, ".salome-completion.sh"),
318 os.path.join(home_dir, ".salome-completion.sh"))
320 if "prereq_path" in _config and os.path.isfile(_config["prereq_path"]):
321 shutil.copyfile(_config["prereq_path"],
322 os.path.join(home_dir, 'env.d', 'envProducts.sh'))
325 print("WARNING: prerequisite file does not exist")
328 if "context_path" in _config and os.path.isfile(_config["context_path"]):
329 shutil.copyfile(_config["context_path"],
330 os.path.join(home_dir, 'env.d', 'envProducts.cfg'))
333 print("WARNING: context file does not exist")
336 if "sha1_collect_path" in _config and os.path.isfile(_config["sha1_collect_path"]):
337 shutil.copyfile(_config["sha1_collect_path"],
338 os.path.join(home_dir, 'sha1_collections.txt'))
341 print("WARNING: sha1 collections file does not exist")
344 if "system_conf_path" in _config and os.path.isfile(_config["system_conf_path"]):
345 shutil.copyfile(_config["system_conf_path"],
346 os.path.join(home_dir, 'env.d', 'envConfSystem.sh'))
349 # Create environment file: configSalome.sh
351 if "python_version" in _config:
352 versionPython_split = _config["python_version"].split('.')
353 versionPython = versionPython_split[0] + "." + versionPython_split[1]
355 cmd='source %s && python3 -c "import sys ; sys.stdout.write(\\"{}.{}\\".format(sys.version_info.major,sys.version_info.minor))"' %(_config["prereq_path"])
356 versionPython=subprocess.check_output(['/bin/bash', '-l' ,'-c',cmd]).decode("utf-8")
358 venv_directory_path = None
359 if "venv_directory_path" in _config:
360 venv_directory_path = _config["venv_directory_path"]
361 venv_bin_directory_path = os.path.join(venv_directory_path, 'bin')
362 venv_pip_executable = os.path.join(venv_bin_directory_path, 'pip')
363 venv_python_executable = os.path.join(venv_bin_directory_path, 'python')
364 if os.path.isdir(venv_directory_path) and os.path.isfile(venv_pip_executable):
365 requirement_file = os.path.join(home_dir, 'requirements.txt')
366 with open(requirement_file, 'w') as fd:
367 subprocess.call([venv_python_executable, '-m', 'pip', 'freeze'], stdout=fd)
369 venv_directory_path = None
371 with open(os.path.join(home_dir, 'env.d', 'configSalome.sh'),'w') as f:
372 for module in _config.get("modules", []):
373 command = 'export '+ module + '_ROOT_DIR=${HOME}/${APPLI}\n'
376 if "samples_path" in _config:
377 command = 'export DATA_DIR=' + _config["samples_path"] +'\n'
380 if "resources_path" in _config and os.path.isfile(_config["resources_path"]):
381 command = 'export USER_CATALOG_RESOURCES_FILE=' + os.path.abspath(_config["resources_path"]) +'\n'
383 # 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)
384 command ="""export PATH=${HOME}/${APPLI}/bin/salome:$PATH
385 export PYTHONPATH=${HOME}/${APPLI}/lib/python%s/site-packages/salome:$PYTHONPATH
386 export PYTHONPATH=${HOME}/${APPLI}/lib/salome:$PYTHONPATH
387 export PYTHONPATH=${HOME}/${APPLI}/bin/salome:$PYTHONPATH
388 export LD_LIBRARY_PATH=${HOME}/${APPLI}/lib/salome:$LD_LIBRARY_PATH
391 # Create environment variable for the salome test
392 for module in _config.get("modules", []):
393 command = "export LD_LIBRARY_PATH=${HOME}/${APPLI}/bin/salome/test/" + module + "/lib:$LD_LIBRARY_PATH\n"
396 # Create environment for plugins GEOM
397 command = "export GEOM_PluginsList=BREPPlugin:STEPPlugin:IGESPlugin:STLPlugin:XAOPlugin:VTKPlugin:AdvancedGEOM\n"
399 # Create environment for Healing
400 command = "export CSF_ShHealingDefaults=${HOME}/${APPLI}/share/salome/resources/geom\n"
402 # Create environment for Meshers
403 command = "export SMESH_MeshersList=StdMeshers:HYBRIDPlugin:HexoticPLUGIN:GMSHPlugin:GHS3DPlugin:NETGENPlugin:HEXABLOCKPlugin:BLSURFPlugin:GHS3DPRLPlugin\nexport SALOME_StdMeshersResources=${HOME}/${APPLI}/share/salome/resources/smesh\n"
405 # Create environment for virtual env
406 if venv_directory_path:
407 command = """# SALOME venv Configuration
408 export SALOME_VENV_DIRECTORY=${HOME}/${APPLI}/venv
409 export PATH=${HOME}/${APPLI}/venv/bin:$PATH
410 export LD_LIBRARY_PATH=${HOME}/${APPLI}/venv/lib:$LD_LIBRARY_PATH
411 export PYTHONPATH=${HOME}/${APPLI}/venv/lib/python%s/site-packages
412 """ % (versionPython)
416 # Create configuration file: configSalome.cfg
417 with open(os.path.join(home_dir, 'env.d', 'configSalome.cfg'),'w') as f:
418 command = "[SALOME ROOT_DIR (modules) Configuration]\n"
420 for module in _config.get("modules", []):
421 command = module + '_ROOT_DIR=${HOME}/${APPLI}\n'
424 if "samples_path" in _config:
425 command = 'DATA_DIR=' + _config["samples_path"] +'\n'
428 if "resources_path" in _config and os.path.isfile(_config["resources_path"]):
429 command = 'USER_CATALOG_RESOURCES_FILE=' + os.path.abspath(_config["resources_path"]) +'\n'
431 command ="""ADD_TO_PATH: ${HOME}/${APPLI}/bin/salome
432 ADD_TO_PYTHONPATH: ${HOME}/${APPLI}/lib/python%s/site-packages/salome
433 ADD_TO_PYTHONPATH: ${HOME}/${APPLI}/lib/salome
434 ADD_TO_LD_LIBRARY_PATH: ${HOME}/${APPLI}/lib/salome
437 for module in _config.get("modules", []):
438 command = "ADD_TO_LD_LIBRARY_PATH: ${HOME}/${APPLI}/bin/salome/test/" + module + "/lib\n"
441 # Create environment for plugins GEOM
442 command = "GEOM_PluginsList=BREPPlugin:STEPPlugin:IGESPlugin:STLPlugin:XAOPlugin:VTKPlugin:AdvancedGEOM\n"
444 # Create environment for Healing
445 command = "CSF_ShHealingDefaults=${HOME}/${APPLI}/share/salome/resources/geom\n"
447 # Create environment for Meshers
448 command = "SMESH_MeshersList=StdMeshers:HYBRIDPlugin:HexoticPLUGIN:GMSHPlugin:GHS3DPlugin:NETGENPlugin:HEXABLOCKPlugin:BLSURFPlugin:GHS3DPRLPlugin\nSALOME_StdMeshersResources=${HOME}/${APPLI}/share/salome/resources/smesh\n"
450 # Create environment for virtual env
451 if venv_directory_path:
452 command = """[SALOME venv Configuration]
453 SALOME_VENV_DIRECTORY: ${HOME}/${APPLI}/venv
454 ADD_TO_PATH: ${HOME}/${APPLI}/venv/bin
455 ADD_TO_LD_LIBRARY_PATH: ${HOME}/${APPLI}/venv/lib
456 ADD_TO_PYTHONPATH: ${HOME}/${APPLI}/venv/lib/python%s/site-packages
457 """ % (versionPython)
461 # Create environment file: configGUI.sh
463 salomeappname = "SalomeApp"
464 with open(os.path.join(home_dir, 'env.d', 'configGUI.sh'),'w') as f:
465 for module in _config.get("modules", []):
466 if module not in ["KERNEL", "GUI", ""]:
467 d = os.path.join(_config[module],"share","salome","resources",module.lower())
468 d_appli = os.path.join("${HOME}","${APPLI}","share","salome","resources",module.lower())
469 if os.path.exists( os.path.join(d,"{0}.xml".format(salomeappname)) ):
470 dirs_ress_icon.append( d_appli )
471 AppConfig="export SalomeAppConfig=${HOME}/${APPLI}:${HOME}/${APPLI}/share/salome/resources/gui/"
472 for dir_module in dirs_ress_icon:
473 AppConfig=AppConfig+":"+dir_module
474 f.write(AppConfig+"\n")
475 command = """export SUITRoot=${HOME}/${APPLI}/share/salome
477 export MMGT_REENTRANT=1
481 # Create configuration file: configGUI.cfg
483 with open(os.path.join(home_dir, 'env.d', 'configGUI.cfg'),'w') as f:
484 command = """[SALOME GUI Configuration]\n"""
486 for module in _config.get("modules", []):
487 if module not in ["KERNEL", "GUI", ""]:
488 d = os.path.join(_config[module],"share","salome","resources",module.lower())
489 d_appli = os.path.join("${HOME}","${APPLI}","share","salome","resources",module.lower())
490 if os.path.exists( os.path.join(d,"{0}.xml".format(salomeappname)) ):
491 dirs_ress_icon.append( d_appli )
492 AppConfig="SalomeAppConfig=${HOME}/${APPLI}:${HOME}/${APPLI}/share/salome/resources/gui/"
493 for dir_module in dirs_ress_icon:
494 AppConfig=AppConfig+":"+dir_module
495 f.write(AppConfig+"\n")
496 command = """SUITRoot=${HOME}/${APPLI}/share/salome
503 with open(os.path.join(home_dir,'SalomeApp.xml'),'w') as f:
504 command = """<document>
505 <section name="launch">
506 <!-- SALOME launching parameters -->
507 <parameter name="gui" value="yes"/>
508 <parameter name="splash" value="yes"/>
509 <parameter name="file" value="no"/>
510 <parameter name="key" value="no"/>
511 <parameter name="interp" value="no"/>
512 <parameter name="logger" value="no"/>
513 <parameter name="xterm" value="no"/>
514 <parameter name="portkill" value="no"/>
515 <parameter name="killall" value="no"/>
516 <parameter name="noexcepthandler" value="no"/>
517 <parameter name="modules" value="%s"/>
518 <parameter name="pyModules" value=""/>
519 <parameter name="embedded" value="SalomeAppEngine,study,cppContainer,registry,moduleCatalog"/>
520 <parameter name="standalone" value=""/>
525 #Keep all modules except KERNEL and GUI
526 for module in _config.get("modules", []):
527 if module in ("KERNEL","GUI"):
530 f.write(command % ",".join(mods))
532 #Add USERS directory with 777 permission to store users configuration files
533 users_dir = os.path.join(home_dir,'USERS')
535 os.chmod(users_dir, 0o777)
538 parser = optparse.OptionParser(usage=usage)
540 parser.add_option('--prefix', dest="prefix", default='.',
541 help="Installation directory (default .)")
543 parser.add_option('--config', dest="config", default='config_appli.xml',
544 help="XML configuration file (default config_appli.xml)")
546 parser.add_option('-v', '--verbose', action='count', dest='verbose',
547 default=0, help="Increase verbosity")
549 options, args = parser.parse_args()
550 if not os.path.exists(options.config):
551 print("ERROR: config file %s does not exist. It is mandatory." % options.config)
554 install(prefix=options.prefix, config_file=options.config, verbose=options.verbose)
557 # -----------------------------------------------------------------------------
559 if __name__ == '__main__':