Back to index

salome-kernel  6.5.0
appli_gen.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 ## \file appli_gen.py
00026 #  Create a %SALOME application (virtual Salome installation)
00027 #
00028 usage="""usage: %prog [options]
00029 Typical use is:
00030   python appli_gen.py
00031 Typical use with options is:
00032   python appli_gen.py --verbose --prefix=<install directory> --config=<configuration file>
00033 """
00034 
00035 import os, glob, string, sys, re
00036 import shutil
00037 import xml.sax
00038 import optparse
00039 import virtual_salome
00040 
00041 # --- names of tags in XML configuration file
00042 appli_tag   = "application"
00043 prereq_tag  = "prerequisites"
00044 modules_tag = "modules"
00045 module_tag  = "module"
00046 samples_tag = "samples"
00047 resources_tag = "resources"
00048 
00049 # --- names of attributes in XML configuration file
00050 nam_att  = "name"
00051 path_att = "path"
00052 gui_att  = "gui"
00053 
00054 # -----------------------------------------------------------------------------
00055 
00056 # --- xml reader for SALOME application configuration file
00057 
00058 class xml_parser:
00059     def __init__(self, fileName ):
00060         print "Configure parser: processing %s ..." % fileName
00061         self.space = []
00062         self.config = {}
00063         self.config["modules"] = []
00064         self.config["guimodules"] = []
00065         parser = xml.sax.make_parser()
00066         parser.setContentHandler(self)
00067         parser.parse(fileName)
00068         pass
00069 
00070     def boolValue( self, str ):
00071         if str in ("yes", "y", "1"):
00072             return 1
00073         elif str in ("no", "n", "0"):
00074             return 0
00075         else:
00076             return str
00077         pass
00078 
00079     def startElement(self, name, attrs):
00080         self.space.append(name)
00081         self.current = None
00082         # --- if we are analyzing "prerequisites" element then store its "path" attribute
00083         if self.space == [appli_tag, prereq_tag] and path_att in attrs.getNames():
00084             self.config["prereq_path"] = attrs.getValue( path_att )
00085             pass
00086         # --- if we are analyzing "resources" element then store its "path" attribute
00087         if self.space == [appli_tag, resources_tag] and path_att in attrs.getNames():
00088             self.config["resources_path"] = attrs.getValue( path_att )
00089             pass
00090         # --- if we are analyzing "samples" element then store its "path" attribute
00091         if self.space == [appli_tag, samples_tag] and path_att in attrs.getNames():
00092             self.config["samples_path"] = attrs.getValue( path_att )
00093             pass
00094         # --- if we are analyzing "module" element then store its "name" and "path" attributes
00095         elif self.space == [appli_tag,modules_tag,module_tag] and \
00096             nam_att in attrs.getNames() and \
00097             path_att in attrs.getNames():
00098             nam = attrs.getValue( nam_att )
00099             path = attrs.getValue( path_att )
00100             gui = 1
00101             if gui_att in attrs.getNames():
00102                 gui = self.boolValue(attrs.getValue( gui_att ))
00103                 pass
00104             self.config["modules"].append(nam)
00105             self.config[nam]=path
00106             if gui:
00107                 self.config["guimodules"].append(nam)
00108                 pass
00109             pass
00110         pass
00111 
00112     def endElement(self, name):
00113         p = self.space.pop()
00114         self.current = None
00115         pass
00116 
00117     def characters(self, content):
00118         pass
00119 
00120     def processingInstruction(self, target, data):
00121         pass
00122 
00123     def setDocumentLocator(self, locator):
00124         pass
00125 
00126     def startDocument(self):
00127         self.read = None
00128         pass
00129 
00130     def endDocument(self):
00131         self.read = None
00132         pass
00133 
00134 # -----------------------------------------------------------------------------
00135 
00136 class params:
00137     pass
00138 
00139 # -----------------------------------------------------------------------------
00140 
00141 def makedirs(namedir):
00142   if os.path.exists(namedir):
00143     dirbak=namedir+".bak"
00144     if os.path.exists(dirbak):
00145       shutil.rmtree(dirbak)
00146     os.rename(namedir,dirbak)
00147     os.listdir(dirbak) #sert seulement a mettre a jour le systeme de fichier sur certaines machines
00148   os.makedirs(namedir)
00149 
00150 def install(prefix,config_file,verbose=0):
00151     home_dir=os.path.abspath(os.path.expanduser(prefix))
00152     filename=os.path.abspath(os.path.expanduser(config_file))
00153     _config={}
00154     try:
00155         p = xml_parser(filename)
00156         _config = p.config
00157     except xml.sax.SAXParseException, inst:
00158         print inst.getMessage()
00159         print "Configure parser: parse error in configuration file %s" % filename
00160         pass
00161     except xml.sax.SAXException, inst:
00162         print inst.args
00163         print "Configure parser: error in configuration file %s" % filename
00164         pass
00165     except:
00166         print "Configure parser: Error : can not read configuration file %s, check existence and rights" % filename
00167         pass
00168 
00169     if verbose:
00170         for cle in _config.keys():
00171             print cle, _config[cle]
00172             pass
00173 
00174     for module in _config["modules"]:
00175         print "--- add module ", module, _config[module]
00176         options = params()
00177         options.verbose=verbose
00178         options.clear=0
00179         options.prefix=home_dir
00180         options.module=_config[module]
00181         virtual_salome.link_module(options)
00182         pass
00183 
00184     appliskel_dir=os.path.join(home_dir,'bin','salome','appliskel')
00185 
00186     for fn in ('envd',
00187                'getAppliPath.py',
00188                'searchFreePort.sh',
00189                'runRemote.sh',
00190                'runAppli',
00191                'runConsole',
00192                'runSession',
00193                'runSalomeScript',
00194                'runTests',
00195                'update_catalogs.py',
00196                'kill_remote_containers.py',
00197                '.bashrc',
00198                ):
00199         virtual_salome.symlink("./bin/salome/appliskel/"+fn,os.path.join(home_dir, fn))
00200         pass
00201 
00202     if filename != os.path.join(home_dir,"config_appli.xml"):
00203         command = "cp -p " + filename + ' ' + os.path.join(home_dir,"config_appli.xml")
00204         os.system(command)
00205         pass
00206 
00207     virtual_salome.mkdir(os.path.join(home_dir,'env.d'))
00208     if os.path.isfile(_config["prereq_path"]):
00209         command='cp -p ' + _config["prereq_path"] + ' ' + os.path.join(home_dir,'env.d','envProducts.sh')
00210         os.system(command)
00211         pass
00212     else:
00213         print "WARNING: prerequisite file does not exist"
00214         pass
00215 
00216     #environment file: configSalome.sh
00217     f =open(os.path.join(home_dir,'env.d','configSalome.sh'),'w')
00218     for module in _config["modules"]:
00219         command='export '+ module + '_ROOT_DIR=${HOME}/${APPLI}\n'
00220         f.write(command)
00221         pass
00222     if _config.has_key("samples_path"):
00223         command='export DATA_DIR=' + _config["samples_path"] +'\n'
00224         f.write(command)
00225         pass
00226     if _config.has_key("resources_path") and os.path.isfile(_config["resources_path"]):
00227         command='export USER_CATALOG_RESOURCES_FILE=' + os.path.abspath(_config["resources_path"]) +'\n'
00228         f.write(command)
00229 
00230     f.close()
00231 
00232 
00233     #environment file: configGUI.sh
00234     f =open(os.path.join(home_dir,'env.d','configGUI.sh'),'w')
00235     command = """export SalomeAppConfig=${HOME}/${APPLI}
00236 export SUITRoot=${HOME}/${APPLI}/share/salome
00237 export DISABLE_FPE=1
00238 export MMGT_REENTRANT=1
00239 """
00240     f.write(command)
00241     f.close()
00242 
00243     #SalomeApp.xml file
00244     f =open(os.path.join(home_dir,'SalomeApp.xml'),'w')
00245     command="""<document>
00246   <section name="launch">
00247     <!-- SALOME launching parameters -->
00248     <parameter name="gui"        value="yes"/>
00249     <parameter name="splash"     value="yes"/>
00250     <parameter name="file"       value="no"/>
00251     <parameter name="key"        value="no"/>
00252     <parameter name="interp"     value="no"/>
00253     <parameter name="logger"     value="no"/>
00254     <parameter name="xterm"      value="no"/>
00255     <parameter name="portkill"   value="no"/>
00256     <parameter name="killall"    value="no"/>
00257     <parameter name="noexcepthandler"  value="no"/>
00258     <parameter name="modules"    value="%s"/>
00259     <parameter name="pyModules"  value=""/>
00260     <parameter name="embedded"   value="SalomeAppEngine,study,cppContainer,registry,moduleCatalog"/>
00261     <parameter name="standalone" value="pyContainer"/>
00262   </section>
00263 </document>
00264 """
00265     mods=[]
00266     #Keep all modules except KERNEL and GUI
00267     for m in _config["modules"]:
00268       if m in ("KERNEL","GUI"):continue
00269       mods.append(m)
00270     f.write(command % ",".join(mods))
00271     f.close()
00272 
00273     #Add default CatalogResources.xml file
00274     f =open(os.path.join(home_dir,'CatalogResources.xml'),'w')
00275     command="""<!DOCTYPE ResourcesCatalog>
00276 <resources>
00277    <machine name="localhost"
00278             hostname="localhost" />
00279 </resources>
00280 """
00281     f.write(command)
00282     f.close()
00283 
00284     #Add USERS directory with 777 permission to store users configuration files
00285     users_dir=os.path.join(home_dir,'USERS')
00286     makedirs(users_dir)
00287     os.chmod(users_dir, 0777)
00288 
00289 def main():
00290     parser = optparse.OptionParser(usage=usage)
00291 
00292     parser.add_option('--prefix', dest="prefix", default='.',
00293                       help="Installation directory (default .)")
00294 
00295     parser.add_option('--config', dest="config", default='config_appli.xml',
00296                       help="XML configuration file (default config_appli.xml)")
00297 
00298     parser.add_option('-v', '--verbose', action='count', dest='verbose',
00299                       default=0, help="Increase verbosity")
00300 
00301     options, args = parser.parse_args()
00302     if not os.path.exists(options.config):
00303       print "ERROR: config file %s does not exist. It is mandatory." % options.config
00304       sys.exit(1)
00305 
00306     install(prefix=options.prefix,config_file=options.config,verbose=options.verbose)
00307     pass
00308 
00309 # -----------------------------------------------------------------------------
00310 
00311 if __name__ == '__main__':
00312     main()
00313     pass