]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_Container.py
Salome HOME
[EDF29576] : Move to python implementation of driver and integrate monitoring into...
[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 os
35 import sys
36 import traceback
37 import importlib
38 from omniORB import CORBA, PortableServer
39 import SALOMEDS
40 import Engines, Engines__POA
41 from SALOME_NamingServicePy import *
42 from SALOME_ComponentPy import *
43 import SALOME_PyNode
44 import logging
45
46 from SALOME_utilities import *
47 from Utils_Identity import getShortHostName
48 from salome_utils import verbose
49 from KernelBasis import VerbosityActivated,getSSLMode
50
51 #=============================================================================
52
53 #define an implementation of the container interface for embedding in Container implemented in C++
54
55 class SALOME_Container_i:
56     _orb = None
57     _poa = None
58     _containerName = ""
59     _naming_service = None
60
61     #-------------------------------------------------------------------------
62
63     def __init__(self ,containerName, containerIORStr, dftTimeIntervalInMs):
64         # Warning this part of code is called at the very first step of container launching
65         # so logging is not instanciate. So use verbose method to discrimine if a message should be printed or not
66         try:
67           argv = sys.argv
68         except AttributeError :
69           # for embedded python interpreter
70           # shouldn't be needed after python 3.8
71           # see https://bugs.python.org/issue32573
72           argv = ['']
73         logging.debug("Instanciation of {} PID = {}".format(containerName,os.getpid()))
74         self._orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
75         self._poa = self._orb.resolve_initial_references("RootPOA")
76         self._containerName = containerName
77         self._logFileName = None
78         self._timeIntervalInMs = dftTimeIntervalInMs
79         self._logm = None
80         self._log = None
81         self._container = self._orb.string_to_object(containerIORStr)
82
83     @property
84     def logm(self):
85         logging.debug("Logm PID = {}".format(os.getpid()))
86         import salome
87         if self._logm is None:
88            salome.salome_init()
89            self._logm = salome.logm
90         return self._logm
91
92     #-------------------------------------------------------------------------
93
94     def import_component(self, componentName):
95         ret=""
96         try:
97             logging.debug("try import ",componentName)
98             importlib.import_module(componentName)
99             logging.debug("import ",componentName," successful")
100         except ImportError:
101             #can't import python module componentName
102             #try to find it in python path
103             try:
104               _specs = importlib.util.find_spec(componentName)
105               _module = importlib.util.module_from_spec(_specs)
106               _specs.loader.exec_module(_module)
107               #module file found in path
108               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
109               ret=ret+traceback.format_exc(10)
110             except ImportError as ee:
111               ret="ImplementationNotFound"
112             except Exception:
113               print("error when calling find_module")
114               ret="ImplementationNotFound"
115         except Exception:
116             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
117             ret=ret+traceback.format_exc(10)
118             traceback.print_exc()
119             print("import ",componentName," not possible")
120         return ret
121
122     #-------------------------------------------------------------------------
123
124     def create_component_instance(self, componentName, instanceName):
125         comp_iors=""
126         ret=""
127         try:
128             component=importlib.import_module(componentName)
129             factory=getattr(component,componentName)
130             comp_i=factory(self._orb,
131                            self._poa,
132                            self._container,
133                            self._containerName,
134                            instanceName,
135                            componentName)
136
137             comp_o = comp_i._this()
138             comp_iors = self._orb.object_to_string(comp_o)
139         except Exception:
140             ret=traceback.format_exc(10)
141             traceback.print_exc()
142         return comp_iors, ret
143
144
145     def create_pynode(self,nodeName,code):
146         try:
147           node=SALOME_PyNode.PyNode_i(nodeName,code,self._poa,self)
148           id_o = self._poa.activate_object(node)
149           comp_o = self._poa.id_to_reference(id_o)
150           comp_iors = self._orb.object_to_string(comp_o)
151           return 0,comp_iors
152         except Exception:
153           exc_typ,exc_val,exc_fr=sys.exc_info()
154           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
155           return 1,"".join(l)
156
157     def create_pyscriptnode(self,nodeName,code):
158         logging.debug("create_pyscriptnode of {} PID = {}".format(nodeName,os.getpid()))
159         try:
160           logscript = None
161           if getSSLMode():
162             logscript = self._log.addScript(nodeName,code)
163           node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self, logscript)
164           id_o = self._poa.activate_object(node)
165           comp_o = self._poa.id_to_reference(id_o)
166           comp_iors = self._orb.object_to_string(comp_o)
167           return 0,comp_iors
168         except Exception:
169           exc_typ,exc_val,exc_fr=sys.exc_info()
170           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
171           print("".join(l)) ; sys.stdout.flush() # print error also in logs of remote container
172           return 1,"".join(l)
173         
174     def positionVerbosityOfLogger(self):
175         logging.debug("positionVerbosityOfLogger PID = {}".format(os.getpid()))
176         if VerbosityActivated():
177           import salome_utils
178           salome_utils.positionVerbosityOfLoggerRegardingState()
179     
180     def monitoringtimeresms(self):
181         return self._timeIntervalInMs
182     
183     def shutdownPy(self):
184         if getSSLMode():
185            if self._log:
186               #self._log.destroy()# TODO : choose to destroy perf report or not. For the moment we keep the report
187               pass
188            
189     def killMe(self):
190         import signal
191         os.kill( os.getpid() , signal.SIGTERM)
192            
193     def setLogFileName(self, logFileName):
194         logging.debug("setLogFileName {} PID = {}".format(logFileName,os.getpid()))
195         if getSSLMode():
196           self._log = self.logm.declareContainer( self._containerName, logFileName )
197
198     def SetMonitoringtimeresms(self , value):
199         self._timeIntervalInMs = value