Salome HOME
f9dec546454652f2a166a602e4b2e0c0429de679
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 #  File   : SALOME_PyNode.py
22 #  Author : Christian CAREMOLI, EDF
23 #  Module : SALOME
24 #  $Header$
25 #
26 import sys,traceback
27 import linecache
28 import pickle
29 import Engines__POA
30 import SALOME__POA
31 import SALOME
32
33 class Generic(SALOME__POA.GenericObj):
34   """A Python implementation of the GenericObj CORBA IDL"""
35   def __init__(self,poa):
36     self.poa=poa
37     self.cnt=1
38
39   def Register(self):
40     #print("Register called : %d"%self.cnt)
41     self.cnt+=1
42
43   def UnRegister(self):
44     #print("UnRegister called : %d"%self.cnt)
45     self.cnt-=1
46     if self.cnt <= 0:
47       oid=self.poa.servant_to_id(self)
48       self.poa.deactivate_object(oid)
49
50   def Destroy(self):
51     print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
52     self.UnRegister()
53
54   def __del__(self):
55     #print("Destuctor called")
56     pass
57
58 class PyNode_i (Engines__POA.PyNode,Generic):
59   """The implementation of the PyNode CORBA IDL"""
60   def __init__(self, nodeName,code,poa,my_container):
61     """Initialize the node : compilation in the local context"""
62     Generic.__init__(self,poa)
63     self.nodeName=nodeName
64     self.code=code
65     self.my_container=my_container._container
66     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
67     ccode=compile(code,nodeName,'exec')
68     self.context={}
69     self.context["my_container"] = self.my_container
70     exec(ccode, self.context)
71
72   def getContainer(self):
73     return self.my_container
74
75   def getCode(self):
76     return self.code
77
78   def getName(self):
79     return self.nodeName
80
81   def defineNewCustomVar(self,varName,valueOfVar):
82     self.context[varName] = pickle.loads(valueOfVar)
83     pass
84
85   def executeAnotherPieceOfCode(self,code):
86     """Called for initialization of container lodging self."""
87     try:
88       ccode=compile(code,self.nodeName,'exec')
89       exec(ccode, self.context)
90     except Exception:
91       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
92
93   def execute(self,funcName,argsin):
94     """Execute the function funcName found in local context with pickled args (argsin)"""
95     try:
96       argsin,kws=pickle.loads(argsin)
97       func=self.context[funcName]
98       argsout=func(*argsin,**kws)
99       argsout=pickle.dumps(argsout,-1)
100       return argsout
101     except Exception:
102       exc_typ,exc_val,exc_fr=sys.exc_info()
103       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
104       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
105
106 class SenderByte_i(SALOME__POA.SenderByte,Generic):
107   def __init__(self,poa,bytesToSend):
108     Generic.__init__(self,poa)
109     self.bytesToSend = bytesToSend
110
111   def getSize(self):
112     return len(self.bytesToSend)
113
114   def sendPart(self,n1,n2):
115     return self.bytesToSend[n1:n2]
116
117 SALOME_FILE_BIG_OBJ_DIR = "SALOME_FILE_BIG_OBJ_DIR"
118     
119 SALOME_BIG_OBJ_ON_DISK_THRES_VAR = "SALOME_BIG_OBJ_ON_DISK_THRES"
120
121 # default is 50 MB
122 SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000
123
124 def GetBigObjectOnDiskThreshold():
125   import os
126   if SALOME_BIG_OBJ_ON_DISK_THRES_VAR in os.environ:
127     return int( os.environ[SALOME_BIG_OBJ_ON_DISK_THRES_VAR] )
128   else:
129     return SALOME_BIG_OBJ_ON_DISK_THRES_DFT
130
131 def GetBigObjectDirectory():
132   import os
133   if SALOME_FILE_BIG_OBJ_DIR not in os.environ:
134     raise RuntimeError("An object of size higher than limit detected and no directory specified to dump it in file !")
135   return os.path.expanduser( os.path.expandvars( os.environ[SALOME_FILE_BIG_OBJ_DIR] ) )
136
137 def GetBigObjectFileName():
138   """
139   Return a filename in the most secure manner (see tempfile documentation)
140   """
141   import tempfile
142   with tempfile.NamedTemporaryFile(dir=GetBigObjectDirectory(),prefix="mem_",suffix=".pckl") as f:
143     ret = f.name
144   return ret
145
146 class BigObjectOnDiskBase:
147   def __init__(self, fileName, objSerialized):
148     """
149     :param fileName: the file used to dump into.
150     :param objSerialized: the object in pickeled form
151     :type objSerialized: bytes
152     """
153     self._filename = fileName
154     self._destroy = False
155     self.__dumpIntoFile(objSerialized)
156
157   def getDestroyStatus(self):
158     return self._destroy
159
160   def unlinkOnDestructor(self):
161     self._destroy = True
162
163   def doNotTouchFile(self):
164     """
165     Method called slave side. The life cycle management of file is client side not slave side.
166     """
167     self._destroy = False
168
169   def __del__(self):
170     if self._destroy:
171       import os
172       os.unlink( self._filename )
173
174   def getFileName(self):
175     return self._filename
176   
177   def __dumpIntoFile(self, objSerialized):
178     with open(self._filename,"wb") as f:
179       f.write(objSerialized)
180
181   def get(self):
182     import pickle
183     with open(self._filename,"rb") as f:
184       return pickle.load(f)
185
186   def __float__(self):
187     return float( self.get() )
188     
189   def __int__(self):
190     return int( self.get() )
191     
192   def __str__(self):
193     obj = self.get()
194     if isinstance(obj,str):
195         return obj
196     else:
197         raise RuntimeError("Not a string")
198       
199 class BigObjectOnDisk(BigObjectOnDiskBase):
200   def __init__(self, fileName, objSerialized):
201     BigObjectOnDiskBase.__init__(self, fileName, objSerialized)
202     
203 class BigObjectOnDiskListElement(BigObjectOnDiskBase):
204   def __init__(self, pos, length, fileName):
205     self._filename = fileName
206     self._destroy = False
207     self._pos = pos
208     self._length = length
209
210   def get(self):
211     fullObj = BigObjectOnDiskBase.get(self)
212     return fullObj[ self._pos ]
213     
214 class BigObjectOnDiskSequence(BigObjectOnDiskBase):
215   def __init__(self, length, fileName, objSerialized):
216     BigObjectOnDiskBase.__init__(self, fileName, objSerialized)
217     self._length = length
218
219   def __getitem__(self, i):
220     return BigObjectOnDiskListElement(i, self._length, self.getFileName())
221
222   def __len__(self):
223     return self._length
224
225 class BigObjectOnDiskList(BigObjectOnDiskSequence):
226   def __init__(self, length, fileName, objSerialized):
227     BigObjectOnDiskSequence.__init__(self, length, fileName, objSerialized)
228     
229 class BigObjectOnDiskTuple(BigObjectOnDiskSequence):
230   def __init__(self, length, fileName, objSerialized):
231     BigObjectOnDiskSequence.__init__(self, length, fileName, objSerialized)
232
233 def SpoolPickleObject( obj ):
234   import pickle
235   pickleObjInit = pickle.dumps( obj , pickle.HIGHEST_PROTOCOL )
236   if len(pickleObjInit) < GetBigObjectOnDiskThreshold():
237     return pickleObjInit
238   else:
239     if isinstance( obj, list):
240       proxyObj = BigObjectOnDiskList( len(obj), GetBigObjectFileName() , pickleObjInit )
241     elif isinstance( obj, tuple):
242       proxyObj = BigObjectOnDiskTuple( len(obj), GetBigObjectFileName() , pickleObjInit )
243     else:
244       proxyObj = BigObjectOnDisk( GetBigObjectFileName() , pickleObjInit )
245     pickleProxy = pickle.dumps( proxyObj , pickle.HIGHEST_PROTOCOL )
246     return pickleProxy
247
248 def UnProxyObjectSimple( obj ):
249   if isinstance(obj,BigObjectOnDiskBase):
250     return obj.get()
251   elif isinstance( obj, list):
252     retObj = []
253     for elt in obj:
254       retObj.append( UnProxyObjectSimple(elt) )
255     return retObj
256   else:
257     return obj
258
259 def UnProxyObject( obj ):
260   if isinstance(obj,BigObjectOnDiskBase):
261     obj.doNotTouchFile()
262     return obj.get()
263   if isinstance(obj,list) or isinstance(obj,tuple):
264     for elt in obj:
265       if isinstance(elt,BigObjectOnDiskBase):
266         elt.doNotTouchFile()
267     return obj
268   else:
269     return obj
270     
271 class SeqByteReceiver:
272   # 2GB limit to trigger split into chunks
273   CHUNK_SIZE = 2000000000
274   def __init__(self,sender):
275     self._obj = sender
276   def __del__(self):
277     self._obj.UnRegister()
278     pass
279   def data(self):
280     size = self._obj.getSize()
281     if size <= SeqByteReceiver.CHUNK_SIZE:
282       return self.fetchOneShot( size )
283     else:
284       return self.fetchByChunks( size )
285   def fetchOneShot(self,size):
286     return self._obj.sendPart(0,size)
287   def fetchByChunks(self,size):
288       """
289       To avoid memory peak parts over 2GB are sent using EFF_CHUNK_SIZE size.
290       """
291       data_for_split_case = bytes(0)
292       EFF_CHUNK_SIZE = SeqByteReceiver.CHUNK_SIZE // 8
293       iStart = 0 ; iEnd = EFF_CHUNK_SIZE
294       while iStart!=iEnd and iEnd <= size:
295         part = self._obj.sendPart(iStart,iEnd)
296         data_for_split_case = bytes(0).join( [data_for_split_case,part] )
297         iStart = iEnd; iEnd = min(iStart + EFF_CHUNK_SIZE,size)
298       return data_for_split_case
299
300 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
301   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
302   def __init__(self, nodeName,code,poa,my_container):
303     """Initialize the node : compilation in the local context"""
304     Generic.__init__(self,poa)
305     self.nodeName=nodeName
306     self.code=code
307     self.my_container=my_container._container
308     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
309     self.ccode=compile(code,nodeName,'exec')
310     self.context={}
311     self.context["my_container"] = self.my_container
312
313   def getContainer(self):
314     return self.my_container
315
316   def getCode(self):
317     return self.code
318
319   def getName(self):
320     return self.nodeName
321
322   def defineNewCustomVar(self,varName,valueOfVar):
323     self.context[varName] = pickle.loads(valueOfVar)
324     pass
325
326   def executeAnotherPieceOfCode(self,code):
327     """Called for initialization of container lodging self."""
328     try:
329       ccode=compile(code,self.nodeName,'exec')
330       exec(ccode, self.context)
331     except Exception:
332       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
333
334   def assignNewCompiledCode(self,codeStr):
335     try:
336       self.code=codeStr
337       self.ccode=compile(codeStr,self.nodeName,'exec')
338     except Exception:
339       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
340
341   def execute(self,outargsname,argsin):
342     """Execute the script stored in attribute ccode with pickled args (argsin)"""
343     try:
344       argsname,kws=pickle.loads(argsin)
345       self.context.update(kws)
346       exec(self.ccode, self.context)
347       argsout=[]
348       for arg in outargsname:
349         if arg not in self.context:
350           raise KeyError("There is no variable %s in context" % arg)
351         argsout.append(self.context[arg])
352       argsout=pickle.dumps(tuple(argsout),-1)
353       return argsout
354     except Exception:
355       exc_typ,exc_val,exc_fr=sys.exc_info()
356       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
357       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))
358
359   def executeFirst(self,argsin):
360     """ Same than first part of self.execute to reduce memory peak."""
361     import time
362     try:
363       data = None
364       if True: # to force call of SeqByteReceiver's destructor
365         argsInPy = SeqByteReceiver( argsin )
366         data = argsInPy.data()
367       _,kws=pickle.loads(data)
368       for elt in kws:
369         # fetch real data if necessary
370         kws[elt] = UnProxyObject( kws[elt] )
371       self.context.update(kws)
372     except Exception:
373       exc_typ,exc_val,exc_fr=sys.exc_info()
374       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
375       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:First %s" % (self.nodeName),0))
376
377   def executeSecond(self,outargsname):
378     """ Same than second part of self.execute to reduce memory peak."""
379     try:
380       exec(self.ccode, self.context)
381       argsout=[]
382       for arg in outargsname:
383         if arg not in self.context:
384           raise KeyError("There is no variable %s in context" % arg)
385         argsout.append(self.context[arg])
386       ret = [ ]
387       for arg in argsout:
388         # the proxy mecanism is catched here
389         argPickle = SpoolPickleObject( arg )
390         retArg = SenderByte_i( self.poa,argPickle )
391         id_o = self.poa.activate_object(retArg)
392         retObj = self.poa.id_to_reference(id_o)
393         ret.append( retObj._narrow( SALOME.SenderByte ) )
394       return ret
395     except Exception:
396       exc_typ,exc_val,exc_fr=sys.exc_info()
397       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
398       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:Second %s, outargsname: %s" % (self.nodeName,outargsname),0))
399
400   def listAllVarsInContext(self):
401       import re
402       pat = re.compile("^__([a-z]+)__$")
403       return [elt for elt in self.context if not pat.match(elt)]
404       
405   def removeAllVarsInContext(self):
406       for elt in self.listAllVarsInContext():
407         del self.context[elt]
408
409   def getValueOfVarInContext(self,varName):
410     try:
411       return pickle.dumps(self.context[varName],-1)
412     except Exception:
413       exc_typ,exc_val,exc_fr=sys.exc_info()
414       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
415       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
416     pass
417   
418   def assignVarInContext(self, varName, value):
419     try:
420       self.context[varName][0] = pickle.loads(value)
421     except Exception:
422       exc_typ,exc_val,exc_fr=sys.exc_info()
423       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
424       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
425     pass
426
427   def callMethodOnVarInContext(self, varName, methodName, args):
428     try:
429       return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
430     except Exception:
431       exc_typ,exc_val,exc_fr=sys.exc_info()
432       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
433       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
434     pass