1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # SALOME Container : implementation of container and engine for Kernel
25 # File : SALOME_Container.py
26 # Author : Paul RASCLE, EDF
30 ## @package SALOME_Container
31 # \brief python implementation of container interface for Kernel
39 from omniORB import CORBA, PortableServer
41 import Engines, Engines__POA
42 from SALOME_NamingServicePy import *
43 from SALOME_ComponentPy import *
47 from SALOME_utilities import *
48 from Utils_Identity import getShortHostName
49 from salome_utils import verbose
50 from KernelBasis import VerbosityActivated,getSSLMode
52 #=============================================================================
54 #define an implementation of the container interface for embedding in Container implemented in C++
56 class SALOME_Container_Abstract_i(abc.ABC):
60 _naming_service = None
62 #-------------------------------------------------------------------------
64 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
65 # Warning this part of code is called at the very first step of container launching
66 # so logging is not instanciate. So use verbose method to discrimine if a message should be printed or not
69 except AttributeError :
70 # for embedded python interpreter
71 # shouldn't be needed after python 3.8
72 # see https://bugs.python.org/issue32573
74 logging.debug("Instanciation of {} PID = {}".format(containerName,os.getpid()))
75 self._orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
76 self._poa = self._orb.resolve_initial_references("RootPOA")
77 self._containerName = containerName
78 self._logFileName = None
79 self._timeIntervalInMs = dftTimeIntervalInMs
82 self._container = self._orb.string_to_object(containerIORStr)
85 def getPyScriptCls(self):
86 raise RuntimeError("Must be overloaded")
90 logging.debug("Logm PID = {}".format(os.getpid()))
92 if self._logm is None:
94 self._logm = salome.logm
97 #-------------------------------------------------------------------------
99 def import_component(self, componentName):
102 logging.debug("try import ",componentName)
103 importlib.import_module(componentName)
104 logging.debug("import ",componentName," successful")
106 #can't import python module componentName
107 #try to find it in python path
109 _specs = importlib.util.find_spec(componentName)
110 _module = importlib.util.module_from_spec(_specs)
111 _specs.loader.exec_module(_module)
112 #module file found in path
113 ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
114 ret=ret+traceback.format_exc(10)
115 except ImportError as ee:
116 ret="ImplementationNotFound"
118 print("error when calling find_module")
119 ret="ImplementationNotFound"
121 ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
122 ret=ret+traceback.format_exc(10)
123 traceback.print_exc()
124 print("import ",componentName," not possible")
127 #-------------------------------------------------------------------------
129 def create_component_instance(self, componentName, instanceName):
133 component=importlib.import_module(componentName)
134 factory=getattr(component,componentName)
135 comp_i=factory(self._orb,
142 comp_o = comp_i._this()
143 comp_iors = self._orb.object_to_string(comp_o)
145 ret=traceback.format_exc(10)
146 traceback.print_exc()
147 return comp_iors, ret
150 def create_pynode(self,nodeName,code):
152 node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
153 id_o = self._poa.activate_object(node)
154 comp_o = self._poa.id_to_reference(id_o)
155 comp_iors = self._orb.object_to_string(comp_o)
158 exc_typ,exc_val,exc_fr=sys.exc_info()
159 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
162 def create_pyscriptnode(self,nodeName,code):
163 logging.debug("create_pyscriptnode of {} PID = {}".format(nodeName,os.getpid()))
167 logscript = self._log.addScript(nodeName,code)
168 cls = self.getPyScriptCls()
169 node = cls(nodeName,code,self._poa,self, logscript)
170 id_o = self._poa.activate_object(node)
171 comp_o = self._poa.id_to_reference(id_o)
172 comp_iors = self._orb.object_to_string(comp_o)
175 exc_typ,exc_val,exc_fr=sys.exc_info()
176 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
177 print("".join(l)) ; sys.stdout.flush() # print error also in logs of remote container
180 def positionVerbosityOfLogger(self):
182 salome_utils.positionVerbosityOfLoggerRegardingState()
183 logging.debug("positionVerbosityOfLogger PID = {}".format(os.getpid()))
185 def monitoringtimeresms(self):
186 return self._timeIntervalInMs
188 def shutdownPy(self):
191 #self._log.destroy()# TODO : choose to destroy perf report or not. For the moment we keep the report
196 os.kill( os.getpid() , signal.SIGKILL)
198 def setLogFileName(self, logFileName):
199 logging.debug("setLogFileName {} PID = {}".format(logFileName,os.getpid()))
201 self._log = self.logm.declareContainer( self._containerName, logFileName )
203 def SetMonitoringtimeresms(self , value):
204 self._timeIntervalInMs = value
206 class SALOME_Container_i(SALOME_Container_Abstract_i):
207 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
208 super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
210 def getPyScriptCls(self):
211 return SALOME_PyNode.PyScriptNode_i
213 class SALOME_Container_OutOfProcess_i(SALOME_Container_i):
214 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
215 super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
217 def getPyScriptCls(self):
218 return SALOME_PyNode.PyScriptNode_OutOfProcess_i
220 class SALOME_Container_OutOfProcess_Replay_i(SALOME_Container_i):
221 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
222 super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
224 def getPyScriptCls(self):
225 return SALOME_PyNode.PyScriptNode_OutOfProcess_Replay_i
227 class SALOME_Container_OutOfProcess_FT_i(SALOME_Container_i):
228 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
229 super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
231 def getPyScriptCls(self):
232 return SALOME_PyNode.PyScriptNode_OutOfProcess_FT_i
234 class SALOME_Container_OutOfProcess_Replay_FT_i(SALOME_Container_i):
235 def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
236 super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
238 def getPyScriptCls(self):
239 return SALOME_PyNode.PyScriptNode_OutOfProcess_Replay_FT_i