1 # Copyright (C) 2009-2014 EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 This module defines the ASTERComponent class for ASTER component generation
22 An ASTER component comes in 3 flavors :
23 - implemented as a dynamic library (kind='lib')
24 - implemented as a standalone component (kind='exe')
25 - implemented as a specific container (kind='cexe')
29 from gener import Component, Invalid, makedirs
31 from pyth_tmpl import pyinitEXEService, pyinitCEXEService, pyinitService
33 from aster_tmpl import asterCEXEService, asterEXEService
34 from aster_tmpl import asterService, asterEXECompo, asterCEXECompo, asterCompo
35 from aster_tmpl import comm, make_etude, cexe, exeaster
36 from aster_tmpl import container, component
37 from aster_tmpl import cmake_src_compo_aster, cmake_src_compo_aster_lib
39 class ASTERComponent(Component):
41 A :class:`ASTERComponent` instance represents an ASTER SALOME component (special component for Code_Aster that is a mix of
42 Fortran and Python code) with services given as a list of :class:`Service` instances with the parameter *services*.
44 :param name: gives the name of the component.
46 :param services: the list of services (:class:`Service`) of the component.
47 :param kind: If it is given and has the value "exe", the component will be built as a standalone
48 component (executable or shell script). The default is to build the component as a dynamic library.
49 :param libs: gives all the libraries options to add when linking the generated component (-L...).
50 :param rlibs: gives all the runtime libraries options to add when linking the generated component (-R...).
51 :param exe_path: is only used when kind is "exe" and gives the path to the standalone component.
52 :param aster_dir: gives the Code_Aster installation directory.
53 :param python_path: If it is given (as a list of paths), all the paths are added to the python path (sys.path).
54 :param argv: is a list of strings that gives the command line parameters for Code_Aster. This parameter is only useful when
57 For example, the following call defines a Code_Aster component named "mycompo" with one service s1 (it must have been defined before).
58 This standalone component takes some command line arguments::
60 >>> c1 = module_generator.ASTERComponent('mycompo', services=[s1,], kind="exe",
62 argv=["-memjeveux","4"])
64 def __init__(self, name, services=None, libs="", rlibs="", aster_dir="",
65 python_path=None, argv=None, kind="lib", exe_path=None, aster_version_type="stable"):
66 """initialise component attributes"""
67 self.aster_dir = aster_dir
68 self.python_path = python_path or []
69 self.argv = argv or []
70 self.exe_path = exe_path
71 self.aster_version_type = aster_version_type
72 Component.__init__(self, name, services, impl="ASTER", libs=libs,
73 rlibs=rlibs, kind=kind)
76 """validate the component definition"""
77 Component.validate(self)
78 if not self.aster_dir:
79 raise Invalid("aster_dir must be defined for component %s" % self.name)
81 kinds = ("lib", "cexe", "exe")
82 if self.kind not in kinds:
83 raise Invalid("kind must be one of %s for component %s" % (kinds,self.name))
84 if self.kind == "lib" and not self.python_path:
85 raise Invalid("python_path must be defined for component %s" % self.name)
86 if self.kind == "cexe" :
88 raise Invalid("exe_path must be defined for component %s" % self.name)
89 if self.kind == "exe" :
91 raise Invalid("exe_path must be defined for component %s" % self.name)
93 #Si un port de nom jdc n'est pas defini dans la liste des inports du service,
94 #on en ajoute un de type string en premiere position
95 for serv in self.services:
97 for port_name,port_type in serv.inport:
98 if port_name == "jdc":
102 serv.inport.insert(0, ("jdc", "string"))
104 def libraryName(self):
105 """ Name of the target library
106 No library for an aster component
110 def getAsterPythonPath(self):
111 """Directory of aster python modules
113 python_version_dir = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
114 aster_python_path = os.path.join(self.aster_dir, "lib", python_version_dir, "site-packages")
116 if not os.path.exists(aster_python_path) :
117 aster_python_path = os.path.join(self.aster_dir, "bibpyt")
119 return aster_python_path
121 def makeCompo(self, gen):
122 """drive the generation of SALOME module files and code files
123 depending on the choosen component kind
125 filename = "%s.py" % self.name
126 #on suppose que les composants ASTER sont homogenes (utilisent meme install)
127 gen.aster = self.aster_dir
129 if self.kind == "lib":
131 return {"CMakeLists.txt":cmake_src_compo_aster_lib.substitute(sources=f),
132 filename:self.makeaster(gen)}
133 elif self.kind == "cexe":
134 fdict=self.makecexepath(gen)
135 sources = self.name + ".py\n " + self.name + "_container.py"
136 d= {"CMakeLists.txt":cmake_src_compo_aster.substitute(
138 module=gen.module.name,
139 resources=self.name+"_config.txt",
140 scripts=self.name+".exe"),
141 self.name+".exe":cexe.substitute(compoexe=self.exe_path),
142 filename:self.makecexeaster(gen)
146 elif self.kind == "exe":
147 fdict=self.makeexepath(gen)
148 sources = self.name + "_module.py\n "
149 sources = sources + self.name + "_component.py"
150 d= {"CMakeLists.txt":cmake_src_compo_aster.substitute(
152 module=gen.module.name,
153 resources=self.name+"_config.txt",
154 scripts=self.name+".exe"),
155 self.name+".exe":exeaster.substitute(compoexe=self.exe_path),
156 self.name+"_module.py":self.makeexeaster(gen)
161 def makeexepath(self, gen):
162 """standalone component: generate files for calculation code"""
165 fdict["%s_config.txt" % self.name] = "# a completer"
166 fdict["%s_component.py" % self.name] = component.substitute(component=self.name)
170 def makecexepath(self, gen):
171 """specific container: generate files"""
174 fdict["%s_container.py" % self.name] = container
177 def getImportESuperv(self):
180 import os.path as osp
181 from asrun.run import AsRunFactory
182 from asrun.config import AsterConfig
185 path = run.get_version_path(VERS)
186 cfg = AsterConfig(osp.join(path, 'config.txt'))
187 pypath = cfg['REPPY'][0]
189 sys.path.insert(0, pypath)
190 from Execution.E_SUPERV import SUPERV
191 """ % self.aster_version_type
195 def makeexeaster(self, gen):
196 """standalone component: generate SALOME component source"""
200 for serv in self.services:
201 defs.append(serv.defs)
204 for name, typ in serv.inport:
205 if typ=="file":continue #files are not passed through service interface
208 datas.append('"%s":cPickle.loads(%s)' % (name, name))
210 datas.append('"%s":%s' % (name, name))
211 #ajout de l'adresse du composant
212 datas.append('"component":self.proxy.ptr()')
213 dvars = "{"+','.join(datas)+"}"
214 inparams = ",".join(params)
218 for name, typ in serv.outport:
219 if typ=="file":continue #files are not passed through service interface
222 datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
224 datas.append('j.g_context["%s"]'%name)
225 outparams = ",".join(params)
226 rvars = ",".join(datas)
228 service = asterEXEService.substitute(component=self.name,
233 dvars=dvars, rvars=rvars)
235 for name, typ, dep in serv.instream:
236 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","IN","%s")'% (name, typ, dep))
237 instream = "\n".join(streams)
239 for name, typ, dep in serv.outstream:
240 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","OUT","%s")'% (name, typ, dep))
241 outstream = "\n".join(streams)
243 init = pyinitEXEService.substitute(component=self.name, service=serv.name,
244 instream=instream, outstream=outstream)
245 services.append(service)
248 importesuperv = self.getImportESuperv()
250 return asterEXECompo.substitute(component=self.name, module=gen.module.name,
251 servicesdef="\n".join(defs),
252 servicesimpl="\n".join(services),
253 initservice='\n'.join(inits),
254 aster_dir=self.aster_dir,
255 importesuperv=importesuperv,
258 def makecexeaster(self, gen):
259 """specific container: generate SALOME component source"""
263 for serv in self.services:
264 defs.append(serv.defs)
267 for name, typ in serv.inport:
268 if typ=="file":continue #files are not passed through service interface
271 datas.append('"%s":cPickle.loads(%s)' % (name, name))
273 datas.append('"%s":%s' % (name, name))
274 #ajout de l'adresse du composant
275 datas.append('"component":self.proxy.ptr()')
276 dvars = "{"+','.join(datas)+"}"
277 inparams = ",".join(params)
281 for name, typ in serv.outport:
284 datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
286 datas.append('j.g_context["%s"]'%name)
287 outparams = ",".join(params)
288 rvars = ",".join(datas)
290 service = asterCEXEService.substitute(component=self.name,
295 dvars=dvars, rvars=rvars)
297 for name, typ, dep in serv.instream:
298 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","IN","%s")'% (name, typ, dep))
299 instream = "\n".join(streams)
301 for name, typ, dep in serv.outstream:
302 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","OUT","%s")'% (name, typ, dep))
303 outstream = "\n".join(streams)
305 init = pyinitCEXEService.substitute(component=self.name,
309 services.append(service)
312 importesuperv = self.getImportESuperv()
314 return asterCEXECompo.substitute(component=self.name,
315 module=gen.module.name,
316 servicesdef="\n".join(defs),
317 servicesimpl="\n".join(services),
318 initservice='\n'.join(inits),
319 aster_dir=self.aster_dir,
320 importesuperv=importesuperv,
324 if self.kind == "cexe":
325 return "CEXE", self.name+".exe"
329 def makeaster(self, gen):
330 """library component: generate SALOME component source"""
334 for serv in self.services:
335 defs.append(serv.defs)
338 for name, typ in serv.inport:
339 if typ=="file":continue #files are not passed through service interface
342 datas.append('"%s":cPickle.loads(%s)' % (name, name))
344 datas.append('"%s":%s' % (name, name))
345 #ajout de l'adresse du composant
346 datas.append('"component":self.proxy.ptr()')
347 dvars = "{"+','.join(datas)+"}"
348 inparams = ",".join(params)
352 for name, typ in serv.outport:
355 datas.append('cPickle.dumps(j.g_context["%s"],-1)'%name)
357 datas.append('j.g_context["%s"]'%name)
358 outparams = ",".join(params)
359 rvars = ",".join(datas)
361 service = asterService.substitute(component=self.name, service=serv.name,
362 inparams=inparams, outparams=outparams,
364 dvars=dvars, rvars=rvars)
366 for name, typ, dep in serv.instream:
367 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","IN","%s")'% (name, typ, dep))
368 instream = "\n".join(streams)
370 for name, typ, dep in serv.outstream:
371 streams.append(' calcium.create_calcium_port(self.proxy,"%s","%s","OUT","%s")'% (name, typ, dep))
372 outstream = "\n".join(streams)
374 init = pyinitService.substitute(component=self.name, service=serv.name,
375 instream=instream, outstream=outstream)
376 services.append(service)
379 python_path = ",".join([repr(p) for p in self.python_path])
380 argv = ",".join([repr(p) for p in self.argv])
381 return asterCompo.substitute(component=self.name, module=gen.module.name,
382 servicesdef="\n".join(defs),
383 servicesimpl="\n".join(services),
384 initservice='\n'.join(inits),
385 aster_dir=self.aster_dir,
386 python_path=python_path, argv=argv)