Salome HOME
CCAR: split file __init__.py into several files : templates + implementation specific
[tools/yacsgen.git] / module_generator / aster_tmpl.py
1
2 try:
3  from string import Template
4 except:
5  from compat import Template,set
6
7 astercompoMakefile="""include $$(top_srcdir)/adm_local/make_common_starter.am
8 salomepython_PYTHON = ${component}.py
9
10 """
11 astercompoMakefile=Template(astercompoMakefile)
12 astercexeMakefile=astercompoMakefile
13
14 asterexeMakefile="""include $$(top_srcdir)/adm_local/make_common_starter.am
15 salomepython_PYTHON = ${component}_module.py
16 # These files are executable scripts
17 dist_salomescript_SCRIPTS= ${component}.exe
18 """
19 asterexeMakefile=Template(asterexeMakefile)
20
21 asterCompo="""
22 import sys,traceback,os
23 import ${module}__POA
24 import calcium
25 import dsccalcium
26 import SALOME
27 import linecache
28 import shutil
29
30 sys.path=sys.path+[${python_path}]
31 import aster
32 import Accas
33 import Cata.cata
34 from Execution.E_SUPERV import SUPERV
35
36 aster_dir="${aster_dir}"
37
38 try:
39   import numpy
40 except:
41   numpy=None
42
43 #DEFS
44 ${servicesdef}
45 #ENDDEF
46
47 class ${component}(${module}__POA.${component},dsccalcium.PyDSCComponent,SUPERV):
48   '''
49      To be identified as a SALOME component this Python class
50      must have the same name as the component, inherit omniorb
51      class ${module}__POA.${component} and DSC class dsccalcium.PyDSCComponent
52      that implements DSC API.
53   '''
54   def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
55     print "${component}.__init__: ", containerName, ';', instanceName,interfaceName
56     dsccalcium.PyDSCComponent.__init__(self, orb, poa,contID,containerName,instanceName,interfaceName)
57     self.argv=[${argv}]
58     #modif pour aster 9.0
59     if hasattr(self,"init_timer"):
60       self.init_timer()
61     #fin modif pour aster 9.0
62     if os.path.exists(os.path.join(aster_dir,"elements")):
63       shutil.copyfile(os.path.join(aster_dir,"elements"),"elem.1")
64     else:
65       shutil.copyfile(os.path.join(aster_dir,"catobj","elements"),"elem.1")
66
67   def init_service(self,service):
68 ${initservice}
69     return False
70
71 ${servicesimpl}
72 """
73 asterCompo=Template(asterCompo)
74
75 asterCEXECompo="""
76 import sys,traceback,os
77 import string
78 import ${module}__POA
79 import calcium
80 import dsccalcium
81 import SALOME
82 import linecache
83 from E_SUPERV import SUPERV
84
85 try:
86   import numpy
87 except:
88   numpy=None
89
90 #DEFS
91 ${servicesdef}
92 #ENDDEF
93
94 class ${component}(${module}__POA.${component},dsccalcium.PyDSCComponent,SUPERV):
95   '''
96      To be identified as a SALOME component this Python class
97      must have the same name as the component, inherit omniorb
98      class ${module}__POA.${component} and DSC class dsccalcium.PyDSCComponent
99      that implements DSC API.
100   '''
101   def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
102     print "${component}.__init__: ", containerName, ';', instanceName,interfaceName
103     self.init=0
104     dsccalcium.PyDSCComponent.__init__(self, orb, poa,contID,containerName,instanceName,interfaceName)
105
106   def init_service(self,service):
107 ${initservice}
108     return False
109
110 ${servicesimpl}
111 """
112
113 asterEXECompo=asterCEXECompo+"""
114   def destroy(self):
115      dsccalcium.PyDSCComponent.destroy(self)
116      self._orb.shutdown(0)
117 """
118
119 asterCEXECompo=Template(asterCEXECompo)
120 asterEXECompo=Template(asterEXECompo)
121
122 asterService="""
123   def ${service}(self,${inparams}):
124     print "${component}.${service}"
125     self.beginService("${component}.${service}")
126     self.jdc=Cata.cata.JdC(procedure=jdc,cata=Cata.cata,nom="Salome",context_ini=${dvars})
127     j=self.jdc
128     #modif pour aster 9.0
129     if hasattr(self,"init_timer"):
130       j.timer = self.timer
131     #fin modif pour aster 9.0
132
133     # On compile le texte Python
134     j.compile()
135
136     #modif pour aster 9.0
137     # On initialise les tops de mesure globale de temps d'execution du jdc
138     if hasattr(self,"init_timer"):
139        j.cpu_user=os.times()[0]
140        j.cpu_syst=os.times()[1]
141     #fin modif pour aster 9.0
142
143     if not j.cr.estvide():
144        msg="ERREUR DE COMPILATION DANS ACCAS - INTERRUPTION"
145        self.MESSAGE(msg)
146        print ">> JDC.py : DEBUT RAPPORT"
147        print j.cr
148        print ">> JDC.py : FIN RAPPORT"
149        j.supprime()
150        sys.stdout.flush()
151        raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,msg+'\\n'+str(j.cr),"${component}.py",0))
152
153     #surcharge des arguments de la ligne de commande (defaut stocke dans le composant) par un eventuel port de nom argv
154     try:
155       self.argv=self.argv+argv.split()
156     except:
157       pass
158
159     #initialisation des arguments de la ligne de commande (remplace la methode initexec de B_JDC.py)
160     aster.argv(self.argv)
161     aster.init(CONTEXT.debug)
162     j.setmode(1)
163     j.ini=1
164
165     try:
166       j.exec_compile()
167     except:
168       sys.stdout.flush()
169       exc_typ,exc_val,exc_fr=sys.exc_info()
170       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
171       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"${component}.py",0))
172
173     ier=0
174     if not j.cr.estvide():
175        msg="ERREUR A L'INTERPRETATION DANS ACCAS - INTERRUPTION"
176        self.MESSAGE(msg)
177        ier=1
178        print ">> JDC.py : DEBUT RAPPORT"
179        print j.cr
180        print ">> JDC.py : FIN RAPPORT"
181        sys.stdout.flush()
182        raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,msg+'\\n'+str(j.cr), "${component}.py",0))
183        
184     if j.par_lot == 'NON':
185        print "FIN EXECUTION"
186        err=calcium.cp_fin(self.proxy,calcium.CP_ARRET)
187        #retour sans erreur (il faut pousser les variables de sortie)
188        print "End of ${component}.${service}"
189        sys.stdout.flush()
190        self.endService("${component}.${service}")
191        return ${rvars}
192
193     # Verification de la validite du jeu de commande
194     cr=j.report()
195     if not cr.estvide():
196        msg="ERREUR A LA VERIFICATION SYNTAXIQUE - INTERRUPTION"
197        self.MESSAGE(msg)
198        print ">> JDC.py : DEBUT RAPPORT"
199        print cr
200        print ">> JDC.py : FIN RAPPORT"
201        sys.stdout.flush()
202        raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,msg+'\\n'+str(cr),"${component}.py",0))
203
204     j.set_par_lot("NON")
205     try:
206        j.BuildExec()
207        ier=0
208        if not j.cr.estvide():
209           msg="ERREUR A L'EXECUTION - INTERRUPTION"
210           self.MESSAGE(msg)
211           ier=1
212           print ">> JDC.py : DEBUT RAPPORT"
213           print j.cr
214           print ">> JDC.py : FIN RAPPORT"
215           sys.stdout.flush()
216           raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,msg+'\\n'+str(j.cr),"${component}.py",0))
217        else:
218          #retour sans erreur (il faut pousser les variables de sortie)
219          err=calcium.cp_fin(self.proxy,calcium.CP_ARRET)
220          print "End of ${component}.${service}"
221          sys.stdout.flush()
222          self.endService("${component}.${service}")
223          return ${rvars}
224     except :
225       self.MESSAGE("ERREUR INOPINEE - INTERRUPTION")
226       sys.stdout.flush()
227       exc_typ,exc_val,exc_fr=sys.exc_info()
228       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
229       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"${component}.py",0))
230 """
231 asterService=Template(asterService)
232
233 asterCEXEService="""
234   def ${service}(self,${inparams}):
235     print "${component}.${service}"
236     self.beginService("${component}.${service}")
237     if not self.init:
238       self.init=1
239       ier=self.main()
240     j=self.jdc
241     self.jdc.g_context.update(${dvars})
242     try:
243       CONTEXT.set_current_step(self.jdc)
244       linecache.cache['<string>']=0,0,string.split(jdc,'\\n'),'<string>'
245       exec jdc in self.jdc.g_context
246       CONTEXT.unset_current_step()
247       self.endService("${component}.${service}")
248     except EOFError:
249       self.endService("${component}.${service}")
250     except:
251       sys.stdout.flush()
252       exc_typ,exc_val,exc_fr=sys.exc_info()
253       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
254       self.endService("${component}.${service}")
255       CONTEXT.unset_current_step()
256       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"${component}.py",0))
257     return ${rvars}
258 """
259 asterCEXEService=Template(asterCEXEService)
260 asterEXEService=asterCEXEService
261
262
263 check_aster="""
264 #
265 # Check availability of Aster binary distribution
266 #
267
268 AC_DEFUN([AC_CHECK_ASTER],[
269
270 AC_CHECKING(for Aster)
271
272 Aster_ok=no
273
274 AC_ARG_WITH(aster,
275       [AC_HELP_STRING([--with-aster=DIR],[root directory path of Aster installation])],
276       [ASTER_DIR="$withval"],[ASTER_DIR=""])
277
278 if test -f ${ASTER_DIR}/asteru ; then
279    Aster_ok=yes
280    AC_MSG_RESULT(Using Aster distribution in ${ASTER_DIR})
281
282    ASTER_INCLUDES=-I$ASTER_DIR/bibc/include
283
284    AC_SUBST(ASTER_DIR)
285    AC_SUBST(ASTER_INCLUDES)
286
287 else
288    AC_MSG_WARN("Cannot find Aster distribution")
289 fi
290
291 AC_MSG_RESULT(for Aster: $Aster_ok)
292
293 ])dnl
294 """
295
296 comm="""
297 DEBUT(PAR_LOT='NON')
298 """
299
300 make_etude="""P actions make_etude
301 P version NEW9
302 P nomjob salome
303 P ncpus 1
304 A memjeveux 4.000000
305 P mem_aster 100
306 A tpmax 60
307 P memjob 32768
308 P mpi_nbcpu 1
309 P mpi_nbnoeud 1
310 P tpsjob 1
311 P mode batch
312 P soumbtc oui
313 P consbtc oui
314 F conf ${config} D 0
315 F comm ${comm} D 1
316 ${extras}
317 """
318 make_etude=Template(make_etude)
319
320 make_etude_exe="""P actions make_etude
321 P version NEW9
322 P nomjob salome
323 P ncpus 1
324 A memjeveux 4.000000
325 P mem_aster 100
326 A tpmax 60
327 P memjob 32768
328 P mpi_nbcpu 1
329 P mpi_nbnoeud 1
330 P tpsjob 1
331 P mode batch
332 P soumbtc oui
333 P consbtc oui
334 F comm ${comm} D 1
335 """
336 make_etude_exe=Template(make_etude_exe)
337
338 cexe="""#!/bin/sh
339
340 export SALOME_CONTAINERNAME=$$1
341
342 cp ${export} temp.export
343 cat >> temp.export << END
344 F mess $$PWD/messages R 6
345 F resu $$PWD/resu R 8
346 F erre $$PWD/erre R 9
347 END
348
349 ${asrun} temp.export
350 """
351 cexe=Template(cexe)
352
353 exeaster="""#!/bin/sh
354
355 export SALOME_CONTAINER=$$1
356 export SALOME_CONTAINERNAME=$$2
357 export SALOME_INSTANCE=$$3
358
359 cp ${export} temp.export
360 cat >> temp.export << END
361 F mess $$PWD/messages R 6
362 F resu $$PWD/resu R 8
363 F erre $$PWD/erre R 9
364 END
365
366 ${asrun} temp.export
367 """
368 exeaster=Template(exeaster)
369
370 container="""import sys,os
371 from omniORB import CORBA
372 from SALOME_ContainerPy import SALOME_ContainerPy_i
373
374 if __name__ == '__main__':
375
376   print sys.argv
377   orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
378   poa = orb.resolve_initial_references("RootPOA")
379   print "ORB and POA initialized"
380   containerName=os.getenv("SALOME_CONTAINERNAME")
381   cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
382   print "SALOME_ContainerPy_i instance created ",cpy_i
383   cpy_o = cpy_i._this()
384   print "SALOME_ContainerPy_i instance activated ",cpy_o
385   sys.stdout.flush()
386   sys.stderr.flush()
387
388   #activate the POA
389   poaManager = poa._get_the_POAManager()
390   poaManager.activate()
391
392   #Block for ever
393   orb.run()
394   print "fin container aster"
395   sys.stdout.flush()
396   sys.stderr.flush()
397 """
398
399 component="""import sys,os
400 from omniORB import CORBA
401 from ${component}_module import ${component}
402
403 if __name__ == '__main__':
404
405   print sys.argv
406   orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
407   poa = orb.resolve_initial_references("RootPOA")
408   print "ORB and POA initialized",orb,poa
409   sys.stdout.flush()
410   sys.stderr.flush()
411
412   container=orb.string_to_object(os.getenv("SALOME_CONTAINER"))
413   containerName=os.getenv("SALOME_CONTAINERNAME")
414   instanceName=os.getenv("SALOME_INSTANCE")
415
416   compo=${component}(orb,poa,container,containerName, instanceName, "${component}")
417   comp_o = compo._this()
418   comp_iors = orb.object_to_string(comp_o)
419   print "ior aster",comp_iors
420
421   sys.stdout.flush()
422   sys.stderr.flush()
423
424   #activate the POA
425   poaManager = poa._get_the_POAManager()
426   poaManager.activate()
427
428   orb.run()
429   print "fin du composant aster standalone"
430
431 """
432 component=Template(component)
433