Salome HOME
Copyright update 2021
[tools/yacsgen.git] / module_generator / astcompo.py
index fa23f9c568cdc0a645735e124e0188d996a576d5..f0c6f257c50ce8322deb9191589065820c478a69 100644 (file)
@@ -1,9 +1,9 @@
-# Copyright (C) 2009-2012  EDF R&D
+# Copyright (C) 2009-2021  EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 """
 import re, os, sys
 
-from gener import Component, Invalid, makedirs
+from module_generator.gener import Component, Invalid, makedirs
 
-from pyth_tmpl import pyinitEXEService, pyinitCEXEService, pyinitService
-import aster_tmpl
-from aster_tmpl import asterCEXEService, asterEXEService
-from aster_tmpl import asterService, asterEXECompo, asterCEXECompo, asterCompo
-from aster_tmpl import comm, make_etude, cexe, exeaster
-from aster_tmpl import container, component
+from module_generator.pyth_tmpl import pyinitEXEService, pyinitCEXEService, pyinitService
+from module_generator import aster_tmpl
+from module_generator.aster_tmpl import asterCEXEService, asterEXEService
+from module_generator.aster_tmpl import asterService, asterEXECompo, asterCEXECompo, asterCompo
+from module_generator.aster_tmpl import comm, make_etude, cexe, exeaster
+from module_generator.aster_tmpl import container, component
+from module_generator.aster_tmpl import cmake_src_compo_aster, cmake_src_compo_aster_lib
 
 class ASTERComponent(Component):
   """
@@ -60,13 +61,14 @@ class ASTERComponent(Component):
                                                           exe_path="launch.sh",
                                                           argv=["-memjeveux","4"])
   """
-  def __init__(self, name, services=None, libs="", rlibs="", aster_dir="", 
-                     python_path=None, argv=None, kind="lib", exe_path=None):
+  def __init__(self, name, services=None, libs="", rlibs="", aster_dir="",
+                     python_path=None, argv=None, kind="lib", exe_path=None, aster_version_type="stable"):
     """initialise component attributes"""
     self.aster_dir = aster_dir
     self.python_path = python_path or []
     self.argv = argv or []
     self.exe_path = exe_path
+    self.aster_version_type = aster_version_type
     Component.__init__(self, name, services, impl="ASTER", libs=libs, 
                              rlibs=rlibs, kind=kind)
 
@@ -99,6 +101,23 @@ class ASTERComponent(Component):
       if not found:
         serv.inport.insert(0, ("jdc", "string"))
 
+  def libraryName(self):
+    """ Name of the target library
+        No library for an aster component
+    """
+    return ""
+    
+  def getAsterPythonPath(self):
+    """Directory of aster python modules
+    """
+    python_version_dir = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
+    aster_python_path = os.path.join(self.aster_dir, "lib", python_version_dir, "site-packages")
+
+    if not os.path.exists(aster_python_path) :
+      aster_python_path = os.path.join(self.aster_dir, "bibpyt")
+      
+    return aster_python_path
+
   def makeCompo(self, gen):
     """drive the generation of SALOME module files and code files
        depending on the choosen component kind
@@ -107,21 +126,18 @@ class ASTERComponent(Component):
     #on suppose que les composants ASTER sont homogenes (utilisent meme install)
     gen.aster = self.aster_dir
 
-    #get ASTER version
-    f = os.path.join(self.aster_dir, "bibpyt", 'Accas', 'properties.py')
-    self.version=(0,0,0)
-    if os.path.isfile(f):
-      mydict = {}
-      execfile(f, mydict)
-      v,r,p = mydict['version'].split('.')
-      self.version=(int(v),int(r),int(p))
-
     if self.kind == "lib":
-      return {"Makefile.am":gen.makeMakefile(self.getMakefileItems(gen)),
+      f = self.name+".py"
+      return {"CMakeLists.txt":cmake_src_compo_aster_lib.substitute(sources=f),
               filename:self.makeaster(gen)}
     elif self.kind == "cexe":
       fdict=self.makecexepath(gen)
-      d= {"Makefile.am":gen.makeMakefile(self.getMakefileItems(gen)),
+      sources = self.name + ".py\n  " + self.name + "_container.py"
+      d= {"CMakeLists.txt":cmake_src_compo_aster.substitute(
+                                            sources=sources,
+                                            module=gen.module.name,
+                                            resources=self.name+"_config.txt",
+                                            scripts=self.name+".exe"),
            self.name+".exe":cexe.substitute(compoexe=self.exe_path),
            filename:self.makecexeaster(gen)
          }
@@ -129,59 +145,37 @@ class ASTERComponent(Component):
       return d
     elif self.kind == "exe":
       fdict=self.makeexepath(gen)
-      d= {"Makefile.am":gen.makeMakefile(self.getMakefileItems(gen)),
+      sources =  self.name + "_module.py\n  "
+      sources =  sources + self.name + "_component.py"
+      d= {"CMakeLists.txt":cmake_src_compo_aster.substitute(
+                                            sources=sources,
+                                            module=gen.module.name,
+                                            resources=self.name+"_config.txt",
+                                            scripts=self.name+".exe"),
            self.name+".exe":exeaster.substitute(compoexe=self.exe_path),
            self.name+"_module.py":self.makeexeaster(gen)
          }
       d.update(fdict)
       return d
 
-  def getMakefileItems(self,gen):
-    makefileItems={"header":"include $(top_srcdir)/adm_local/make_common_starter.am"}
-    if self.kind == "lib":
-      makefileItems["salomepython_PYTHON"]=[self.name+".py"]
-    elif self.kind == "exe":
-      makefileItems["salomepython_PYTHON"]=[self.name+"_module.py",self.name+"_component.py"]
-      if self.version < (10,1,2):
-        makefileItems["salomepython_PYTHON"].append("E_SUPERV.py")
-      makefileItems["dist_salomescript_SCRIPTS"]=[self.name+".exe"]
-      makefileItems["salomeres_DATA"]=[self.name+"_config.txt"]
-    elif self.kind == "cexe":
-      makefileItems["salomepython_PYTHON"]=[self.name+".py",self.name+"_container.py"]
-      if self.version < (10,1,2):
-        makefileItems["salomepython_PYTHON"].append("E_SUPERV.py")
-      makefileItems["dist_salomescript_SCRIPTS"]=[self.name+".exe"]
-      makefileItems["salomeres_DATA"]=[self.name+"_config.txt"]
-    return makefileItems
-
-
   def makeexepath(self, gen):
     """standalone component: generate files for calculation code"""
 
     fdict={}
-
-    if self.version < (10,1,2):
-      #patch to E_SUPERV.py
-      fil = open(os.path.join(self.aster_dir, "bibpyt", "Execution", "E_SUPERV.py"))
-      esuperv = fil.read()
-      fil.close()
-      esuperv = re.sub("def Execute\(self\)", "def Execute(self, params)", esuperv)
-      esuperv = re.sub("j=self.JdC", "self.jdc=j=self.JdC", esuperv)
-      esuperv = re.sub("\*\*args", "context_ini=params, **args", esuperv)
-      esuperv = re.sub("def main\(self\)", "def main(self,params={})", esuperv)
-      esuperv = re.sub("return self.Execute\(\)", "return self.Execute(params)", esuperv)
-      fdict["E_SUPERV.py"]=esuperv
-
     #use a specific main program (modification of config.txt file)
-    fil = open(os.path.join(self.aster_dir, "config.txt"))
-    config = fil.read()
-    fil.close()
-    config = re.sub(" profile.sh", os.path.join(self.aster_dir, "profile.sh"), config)
-
-    path=os.path.join(os.path.abspath(gen.module.prefix),'lib',
-                      'python%s.%s' % (sys.version_info[0], sys.version_info[1]),
-                      'site-packages','salome','%s_component.py'%self.name)
-    config = re.sub("Execution\/E_SUPERV.py", path, config)
+    config = ""
+    path_config = os.path.join(self.aster_dir, "config.txt")
+    if os.path.exists(path_config) :
+      fil = open(path_config)
+      config = fil.read()
+      fil.close()
+#      config = re.sub(" profile.sh", os.path.join(self.aster_dir, "profile.sh"), config)
+#      path=os.path.join(os.path.abspath(gen.module.prefix),'lib',
+#                      'python%s.%s' % (sys.version_info[0], sys.version_info[1]),
+#                      'site-packages','salome','%s_component.py'%self.name)
+#      config = re.sub("Execution\/E_SUPERV.py", path, config)
+    else :
+      config = "# a completer:%s n'existe pas" % path_config
 
     fdict["%s_config.txt" % self.name] = config
     fdict["%s_component.py" % self.name] = component.substitute(component=self.name)
@@ -192,33 +186,26 @@ class ASTERComponent(Component):
     """specific container: generate files"""
 
     fdict={}
+    fdict["%s_container.py" % self.name] = container
+    return fdict
 
-    if self.version < (10,1,2):
-      #patch to E_SUPERV.py
-      fil = open(os.path.join(self.aster_dir, "bibpyt", "Execution", "E_SUPERV.py"))
-      esuperv = fil.read()
-      fil.close()
-      esuperv = re.sub("def Execute\(self\)", "def Execute(self, params)", esuperv)
-      esuperv = re.sub("j=self.JdC", "self.jdc=j=self.JdC", esuperv)
-      esuperv = re.sub("\*\*args", "context_ini=params, **args", esuperv)
-      esuperv = re.sub("def main\(self\)", "def main(self,params={})", esuperv)
-      esuperv = re.sub("return self.Execute\(\)", "return self.Execute(params)", esuperv)
-      fdict["E_SUPERV.py"]=esuperv
-
-    #use a specific main program
-    fil = open(os.path.join(self.aster_dir, "config.txt"))
-    config = fil.read()
-    fil.close()
-    config = re.sub(" profile.sh", os.path.join(self.aster_dir, "profile.sh"), config)
-    path=os.path.join(os.path.abspath(gen.module.prefix),'lib',
-                      'python%s.%s' % (sys.version_info[0], sys.version_info[1]),
-                      'site-packages','salome','%s_container.py' % self.name)
-    config = re.sub("Execution\/E_SUPERV.py", path, config)
+  def getImportESuperv(self):
+    importesuperv="""
+VERS="%s"
+import os.path as osp
+from asrun.run import AsRunFactory
+from asrun.config import AsterConfig
 
-    fdict["%s_container.py" % self.name] = container
-    fdict["%s_config.txt" % self.name] = config
+run = AsRunFactory()
+path = run.get_version_path(VERS)
+cfg = AsterConfig(osp.join(path, 'config.txt'))
+pypath = cfg['REPPY'][0]
+
+sys.path.insert(0, pypath)
+from Execution.E_SUPERV import SUPERV
+""" % self.aster_version_type
+    return importesuperv
 
-    return fdict
 
   def makeexeaster(self, gen):
     """standalone component: generate SALOME component source"""
@@ -233,7 +220,7 @@ class ASTERComponent(Component):
         if typ=="file":continue #files are not passed through service interface
         params.append(name)
         if typ == "pyobj":
-          datas.append('"%s":cPickle.loads(%s)' % (name, name))
+          datas.append('"%s":pickle.loads(%s)' % (name, name))
         else:
           datas.append('"%s":%s' % (name, name))
       #ajout de l'adresse du composant
@@ -247,7 +234,7 @@ class ASTERComponent(Component):
         if typ=="file":continue #files are not passed through service interface
         params.append(name)
         if typ == "pyobj":
-          datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
+          datas.append('pickle.dumps(j.g_context["%s"],-1)'%name)
         else:
           datas.append('j.g_context["%s"]'%name)
       outparams = ",".join(params)
@@ -273,12 +260,7 @@ class ASTERComponent(Component):
       services.append(service)
       inits.append(init)
 
-    if self.version < (10,1,2):
-      importesuperv="from E_SUPERV import SUPERV"
-    else:
-      importesuperv="""sys.path=["%s"]+sys.path
-from Execution.E_SUPERV import SUPERV
-""" % os.path.join(self.aster_dir, "bibpyt")
+    importesuperv = self.getImportESuperv()
 
     return asterEXECompo.substitute(component=self.name, module=gen.module.name,
                                     servicesdef="\n".join(defs),
@@ -301,7 +283,7 @@ from Execution.E_SUPERV import SUPERV
         if typ=="file":continue #files are not passed through service interface
         params.append(name)
         if typ == "pyobj":
-          datas.append('"%s":cPickle.loads(%s)' % (name, name))
+          datas.append('"%s":pickle.loads(%s)' % (name, name))
         else:
           datas.append('"%s":%s' % (name, name))
       #ajout de l'adresse du composant
@@ -314,7 +296,7 @@ from Execution.E_SUPERV import SUPERV
       for name, typ in serv.outport:
         params.append(name)
         if typ == "pyobj":
-          datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
+          datas.append('pickle.dumps(j.g_context["%s"],-1)'%name)
         else:
           datas.append('j.g_context["%s"]'%name)
       outparams = ",".join(params)
@@ -342,12 +324,7 @@ from Execution.E_SUPERV import SUPERV
       services.append(service)
       inits.append(init)
 
-    if self.version < (10,1,2):
-      importesuperv="from E_SUPERV import SUPERV"
-    else:
-      importesuperv="""sys.path=["%s"] +sys.path
-from Execution.E_SUPERV import SUPERV
-""" % os.path.join(self.aster_dir, "bibpyt")
+    importesuperv = self.getImportESuperv()
 
     return asterCEXECompo.substitute(component=self.name, 
                                      module=gen.module.name,
@@ -377,7 +354,7 @@ from Execution.E_SUPERV import SUPERV
         if typ=="file":continue #files are not passed through service interface
         params.append(name)
         if typ == "pyobj":
-          datas.append('"%s":cPickle.loads(%s)' % (name, name))
+          datas.append('"%s":pickle.loads(%s)' % (name, name))
         else:
           datas.append('"%s":%s' % (name, name))
       #ajout de l'adresse du composant
@@ -390,7 +367,7 @@ from Execution.E_SUPERV import SUPERV
       for name, typ in serv.outport:
         params.append(name)
         if typ == "pyobj":
-          datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
+          datas.append('pickle.dumps(j.g_context["%s"],-1)'%name)
         else:
           datas.append('j.g_context["%s"]'%name)
       outparams = ",".join(params)