1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # File : SALOME_PyNode.py
22 # Author : Christian CAREMOLI, EDF
33 class Generic(SALOME__POA.GenericObj):
34 """A Python implementation of the GenericObj CORBA IDL"""
35 def __init__(self,poa):
40 #print("Register called : %d"%self.cnt)
44 #print("UnRegister called : %d"%self.cnt)
47 oid=self.poa.servant_to_id(self)
48 self.poa.deactivate_object(oid)
51 print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
55 #print("Destuctor called")
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
65 self.my_container=my_container._container
66 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
67 ccode=compile(code,nodeName,'exec')
69 self.context["my_container"] = self.my_container
70 exec(ccode, self.context)
72 def getContainer(self):
73 return self.my_container
81 def defineNewCustomVar(self,varName,valueOfVar):
82 self.context[varName] = pickle.loads(valueOfVar)
85 def executeAnotherPieceOfCode(self,code):
86 """Called for initialization of container lodging self."""
88 ccode=compile(code,self.nodeName,'exec')
89 exec(ccode, self.context)
91 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
93 def execute(self,funcName,argsin):
94 """Execute the function funcName found in local context with pickled args (argsin)"""
96 argsin,kws=pickle.loads(argsin)
97 func=self.context[funcName]
98 argsout=func(*argsin,**kws)
99 argsout=pickle.dumps(argsout,-1)
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))
106 class SenderByte_i(SALOME__POA.SenderByte,Generic):
107 def __init__(self,poa,bytesToSend):
108 Generic.__init__(self,poa)
109 self.bytesToSend = bytesToSend
112 return len(self.bytesToSend)
114 def sendPart(self,n1,n2):
115 return self.bytesToSend[n1:n2]
117 SALOME_FILE_BIG_OBJ_DIR = "SALOME_FILE_BIG_OBJ_DIR"
119 SALOME_BIG_OBJ_ON_DISK_THRES_VAR = "SALOME_BIG_OBJ_ON_DISK_THRES"
122 SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000
124 def GetBigObjectOnDiskThreshold():
126 if SALOME_BIG_OBJ_ON_DISK_THRES_VAR in os.environ:
127 return int( os.environ[SALOME_BIG_OBJ_ON_DISK_THRES_VAR] )
129 return SALOME_BIG_OBJ_ON_DISK_THRES_DFT
131 def GetBigObjectDirectory():
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] ) )
137 def GetBigObjectFileName():
139 Return a filename in the most secure manner (see tempfile documentation)
142 with tempfile.NamedTemporaryFile(dir=GetBigObjectDirectory(),prefix="mem_",suffix=".pckl") as f:
146 class BigObjectOnDiskBase:
147 def __init__(self, fileName, objSerialized):
149 :param fileName: the file used to dump into.
150 :param objSerialized: the object in pickeled form
151 :type objSerialized: bytes
153 self._filename = fileName
154 self._destroy = False
155 self.__dumpIntoFile(objSerialized)
157 def getDestroyStatus(self):
160 def unlinkOnDestructor(self):
163 def doNotTouchFile(self):
165 Method called slave side. The life cycle management of file is client side not slave side.
167 self._destroy = False
172 os.unlink( self._filename )
174 def getFileName(self):
175 return self._filename
177 def __dumpIntoFile(self, objSerialized):
178 with open(self._filename,"wb") as f:
179 f.write(objSerialized)
183 with open(self._filename,"rb") as f:
184 return pickle.load(f)
187 return float( self.get() )
190 return int( self.get() )
194 if isinstance(obj,str):
197 raise RuntimeError("Not a string")
199 class BigObjectOnDisk(BigObjectOnDiskBase):
200 def __init__(self, fileName, objSerialized):
201 BigObjectOnDiskBase.__init__(self, fileName, objSerialized)
203 class BigObjectOnDiskListElement(BigObjectOnDiskBase):
204 def __init__(self, pos, length, fileName):
205 self._filename = fileName
206 self._destroy = False
208 self._length = length
211 fullObj = BigObjectOnDiskBase.get(self)
212 return fullObj[ self._pos ]
214 class BigObjectOnDiskSequence(BigObjectOnDiskBase):
215 def __init__(self, length, fileName, objSerialized):
216 BigObjectOnDiskBase.__init__(self, fileName, objSerialized)
217 self._length = length
219 def __getitem__(self, i):
220 return BigObjectOnDiskListElement(i, self._length, self.getFileName())
225 class BigObjectOnDiskList(BigObjectOnDiskSequence):
226 def __init__(self, length, fileName, objSerialized):
227 BigObjectOnDiskSequence.__init__(self, length, fileName, objSerialized)
229 class BigObjectOnDiskTuple(BigObjectOnDiskSequence):
230 def __init__(self, length, fileName, objSerialized):
231 BigObjectOnDiskSequence.__init__(self, length, fileName, objSerialized)
233 def SpoolPickleObject( obj ):
235 pickleObjInit = pickle.dumps( obj , pickle.HIGHEST_PROTOCOL )
236 if len(pickleObjInit) < GetBigObjectOnDiskThreshold():
239 if isinstance( obj, list):
240 proxyObj = BigObjectOnDiskList( len(obj), GetBigObjectFileName() , pickleObjInit )
241 elif isinstance( obj, tuple):
242 proxyObj = BigObjectOnDiskTuple( len(obj), GetBigObjectFileName() , pickleObjInit )
244 proxyObj = BigObjectOnDisk( GetBigObjectFileName() , pickleObjInit )
245 pickleProxy = pickle.dumps( proxyObj , pickle.HIGHEST_PROTOCOL )
248 def UnProxyObject( obj ):
249 if isinstance(obj,BigObjectOnDiskBase):
252 if isinstance(obj,list) or isinstance(obj,tuple):
254 if isinstance(elt,BigObjectOnDiskBase):
260 class SeqByteReceiver:
261 # 2GB limit to trigger split into chunks
262 CHUNK_SIZE = 2000000000
263 def __init__(self,sender):
266 self._obj.UnRegister()
269 size = self._obj.getSize()
270 if size <= SeqByteReceiver.CHUNK_SIZE:
271 return self.fetchOneShot( size )
273 return self.fetchByChunks( size )
274 def fetchOneShot(self,size):
275 return self._obj.sendPart(0,size)
276 def fetchByChunks(self,size):
278 To avoid memory peak parts over 2GB are sent using EFF_CHUNK_SIZE size.
280 data_for_split_case = bytes(0)
281 EFF_CHUNK_SIZE = SeqByteReceiver.CHUNK_SIZE // 8
282 iStart = 0 ; iEnd = EFF_CHUNK_SIZE
283 while iStart!=iEnd and iEnd <= size:
284 part = self._obj.sendPart(iStart,iEnd)
285 data_for_split_case = bytes(0).join( [data_for_split_case,part] )
286 iStart = iEnd; iEnd = min(iStart + EFF_CHUNK_SIZE,size)
287 return data_for_split_case
289 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
290 """The implementation of the PyScriptNode CORBA IDL that executes a script"""
291 def __init__(self, nodeName,code,poa,my_container):
292 """Initialize the node : compilation in the local context"""
293 Generic.__init__(self,poa)
294 self.nodeName=nodeName
296 self.my_container=my_container._container
297 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
298 self.ccode=compile(code,nodeName,'exec')
300 self.context["my_container"] = self.my_container
302 def getContainer(self):
303 return self.my_container
311 def defineNewCustomVar(self,varName,valueOfVar):
312 self.context[varName] = pickle.loads(valueOfVar)
315 def executeAnotherPieceOfCode(self,code):
316 """Called for initialization of container lodging self."""
318 ccode=compile(code,self.nodeName,'exec')
319 exec(ccode, self.context)
321 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
323 def assignNewCompiledCode(self,codeStr):
326 self.ccode=compile(codeStr,self.nodeName,'exec')
328 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
330 def execute(self,outargsname,argsin):
331 """Execute the script stored in attribute ccode with pickled args (argsin)"""
333 argsname,kws=pickle.loads(argsin)
334 self.context.update(kws)
335 exec(self.ccode, self.context)
337 for arg in outargsname:
338 if arg not in self.context:
339 raise KeyError("There is no variable %s in context" % arg)
340 argsout.append(self.context[arg])
341 argsout=pickle.dumps(tuple(argsout),-1)
344 exc_typ,exc_val,exc_fr=sys.exc_info()
345 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
346 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))
348 def executeFirst(self,argsin):
349 """ Same than first part of self.execute to reduce memory peak."""
353 if True: # to force call of SeqByteReceiver's destructor
354 argsInPy = SeqByteReceiver( argsin )
355 data = argsInPy.data()
356 _,kws=pickle.loads(data)
358 # fetch real data if necessary
359 kws[elt] = UnProxyObject( kws[elt] )
360 self.context.update(kws)
362 exc_typ,exc_val,exc_fr=sys.exc_info()
363 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
364 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:First %s" % (self.nodeName),0))
366 def executeSecond(self,outargsname):
367 """ Same than second part of self.execute to reduce memory peak."""
369 exec(self.ccode, self.context)
371 for arg in outargsname:
372 if arg not in self.context:
373 raise KeyError("There is no variable %s in context" % arg)
374 argsout.append(self.context[arg])
377 # the proxy mecanism is catched here
378 argPickle = SpoolPickleObject( arg )
379 retArg = SenderByte_i( self.poa,argPickle )
380 id_o = self.poa.activate_object(retArg)
381 retObj = self.poa.id_to_reference(id_o)
382 ret.append( retObj._narrow( SALOME.SenderByte ) )
385 exc_typ,exc_val,exc_fr=sys.exc_info()
386 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
387 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:Second %s, outargsname: %s" % (self.nodeName,outargsname),0))
389 def listAllVarsInContext(self):
391 pat = re.compile("^__([a-z]+)__$")
392 return [elt for elt in self.context if not pat.match(elt)]
394 def removeAllVarsInContext(self):
395 for elt in self.listAllVarsInContext():
396 del self.context[elt]
398 def getValueOfVarInContext(self,varName):
400 return pickle.dumps(self.context[varName],-1)
402 exc_typ,exc_val,exc_fr=sys.exc_info()
403 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
404 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
407 def assignVarInContext(self, varName, value):
409 self.context[varName][0] = pickle.loads(value)
411 exc_typ,exc_val,exc_fr=sys.exc_info()
412 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
413 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
416 def callMethodOnVarInContext(self, varName, methodName, args):
418 return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
420 exc_typ,exc_val,exc_fr=sys.exc_info()
421 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
422 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))