Salome HOME
[EDF30356] : Extend management of maximum_time attribute format from pylauncher to...
[modules/kernel.git] / src / Container / SALOME_Container.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  SALOME Container : implementation of container and engine for Kernel
25 #  File   : SALOME_Container.py
26 #  Author : Paul RASCLE, EDF
27 #  Module : SALOME
28 #  $Header$
29
30 ## @package SALOME_Container
31 # \brief python implementation of container interface for Kernel
32 #
33
34 import abc
35 import os
36 import sys
37 import traceback
38 import importlib
39 from omniORB import CORBA, PortableServer
40 import SALOMEDS
41 import Engines, Engines__POA
42 from SALOME_NamingServicePy import *
43 from SALOME_ComponentPy import *
44 import SALOME_PyNode
45 import logging
46
47 from SALOME_utilities import *
48 from Utils_Identity import getShortHostName
49 from salome_utils import verbose
50 from KernelBasis import VerbosityActivated,getSSLMode
51
52 #=============================================================================
53
54 #define an implementation of the container interface for embedding in Container implemented in C++
55
56 class SALOME_Container_Abstract_i(abc.ABC):
57     _orb = None
58     _poa = None
59     _containerName = ""
60     _naming_service = None
61
62     #-------------------------------------------------------------------------
63
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
67         try:
68           argv = sys.argv
69         except AttributeError :
70           # for embedded python interpreter
71           # shouldn't be needed after python 3.8
72           # see https://bugs.python.org/issue32573
73           argv = ['']
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
80         self._logm = None
81         self._log = None
82         self._container = self._orb.string_to_object(containerIORStr)
83
84     @abc.abstractmethod
85     def getPyScriptCls(self):
86         raise RuntimeError("Must be overloaded")
87
88     @property
89     def logm(self):
90         logging.debug("Logm PID = {}".format(os.getpid()))
91         import salome
92         if self._logm is None:
93            salome.salome_init()
94            self._logm = salome.logm
95         return self._logm
96
97     #-------------------------------------------------------------------------
98
99     def import_component(self, componentName):
100         ret=""
101         try:
102             logging.debug("try import ",componentName)
103             importlib.import_module(componentName)
104             logging.debug("import ",componentName," successful")
105         except ImportError:
106             #can't import python module componentName
107             #try to find it in python path
108             try:
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"
117             except Exception:
118               print("error when calling find_module")
119               ret="ImplementationNotFound"
120         except Exception:
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")
125         return ret
126
127     #-------------------------------------------------------------------------
128
129     def create_component_instance(self, componentName, instanceName):
130         comp_iors=""
131         ret=""
132         try:
133             component=importlib.import_module(componentName)
134             factory=getattr(component,componentName)
135             comp_i=factory(self._orb,
136                            self._poa,
137                            self._container,
138                            self._containerName,
139                            instanceName,
140                            componentName)
141
142             comp_o = comp_i._this()
143             comp_iors = self._orb.object_to_string(comp_o)
144         except Exception:
145             ret=traceback.format_exc(10)
146             traceback.print_exc()
147         return comp_iors, ret
148
149
150     def create_pynode(self,nodeName,code):
151         try:
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)
156           return 0,comp_iors
157         except Exception:
158           exc_typ,exc_val,exc_fr=sys.exc_info()
159           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
160           return 1,"".join(l)
161
162     def create_pyscriptnode(self,nodeName,code):
163         logging.debug("create_pyscriptnode of {} PID = {}".format(nodeName,os.getpid()))
164         try:
165           logscript = None
166           if getSSLMode():
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)
173           return 0,comp_iors
174         except Exception:
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
178           return 1,"".join(l)
179         
180     def positionVerbosityOfLogger(self):
181         import salome_utils
182         salome_utils.positionVerbosityOfLoggerRegardingState()
183         logging.debug("positionVerbosityOfLogger PID = {}".format(os.getpid()))
184     
185     def monitoringtimeresms(self):
186         return self._timeIntervalInMs
187     
188     def shutdownPy(self):
189         if getSSLMode():
190            if self._log:
191               #self._log.destroy()# TODO : choose to destroy perf report or not. For the moment we keep the report
192               pass
193            
194     def killMe(self):
195         import signal
196         os.kill( os.getpid() , signal.SIGKILL)
197            
198     def setLogFileName(self, logFileName):
199         logging.debug("setLogFileName {} PID = {}".format(logFileName,os.getpid()))
200         if getSSLMode():
201           self._log = self.logm.declareContainer( self._containerName, logFileName )
202
203     def SetMonitoringtimeresms(self , value):
204         self._timeIntervalInMs = value
205
206 class SALOME_Container_i(SALOME_Container_Abstract_i):
207     def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
208       super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
209
210     def getPyScriptCls(self):
211       return SALOME_PyNode.PyScriptNode_i
212
213 class SALOME_Container_OutOfProcess_i(SALOME_Container_i):
214     def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
215       super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
216       
217     def getPyScriptCls(self):
218       return SALOME_PyNode.PyScriptNode_OutOfProcess_i
219
220 class SALOME_Container_OutOfProcess_Replay_i(SALOME_Container_i):
221     def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
222       super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
223
224     def getPyScriptCls(self):
225       return SALOME_PyNode.PyScriptNode_OutOfProcess_Replay_i
226
227 class SALOME_Container_OutOfProcess_FT_i(SALOME_Container_i):
228     def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
229       super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
230       
231     def getPyScriptCls(self):
232       return SALOME_PyNode.PyScriptNode_OutOfProcess_FT_i
233
234 class SALOME_Container_OutOfProcess_Replay_FT_i(SALOME_Container_i):
235     def __init__(self, containerName, containerIORStr, dftTimeIntervalInMs):
236       super().__init__(containerName, containerIORStr, dftTimeIntervalInMs)
237
238     def getPyScriptCls(self):
239       return SALOME_PyNode.PyScriptNode_OutOfProcess_Replay_FT_i