Salome HOME
updated copyright message
[modules/kernel.git] / src / Container / SALOME_ContainerPy.py
index 9d0339d199a1253f86c41507d8808f033cce27c8..815616a6f5fb698937eb8444c7bb81c6e33248c2 100755 (executable)
@@ -1,6 +1,6 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #
 import os
 import sys
-import string
+import importlib
 
 from omniORB import CORBA, PortableServer
-import SALOMEDS 
+import SALOMEDS
 import Engines, Engines__POA
+import salome_psutil
 from SALOME_NamingServicePy import *
+from SALOME_Embedded_NamingService import SALOME_Embedded_NamingService
 from SALOME_ComponentPy import *
 
 from SALOME_utilities import *
@@ -46,11 +48,15 @@ from launchConfigureParser import verbose
 
 #define an implementation of the container interface for the container implemented in Python
 
-class SALOME_ContainerPy_i (Engines__POA.Container):
+class SALOME_ContainerPy_Gen_i(Engines__POA.Container):
+    """
+    Implementation without naming_service server
+    """
     _orb = None
     _poa = None
     _numInstance = 0
     _listInstances_map = {}
+    _script = ""
 
     #-------------------------------------------------------------------------
 
@@ -58,89 +64,20 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
         MESSAGE( "SALOME_ContainerPy_i::__init__" )
         self._orb = orb
         self._poa = poa
+        self._load_script = None
         myMachine=getShortHostName()
         Container_path = "/Containers/" + myMachine + "/" + containerName
         self._containerName = Container_path
-        if verbose(): print "container name ",self._containerName
-
-        naming_service = SALOME_NamingServicePy_i(self._orb)
-        self._naming_service = naming_service
-        MESSAGE( str(Container_path) )
-        naming_service.Register(self._this(), Container_path)
-            
-    #-------------------------------------------------------------------------
-
-    def start_impl(self, ContainerName):
-        MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
-        myMachine=getShortHostName()
-        theContainer = "/Containers/" + myMachine + "/" + ContainerName
-        try:
-            obj = self._naming_service.Resolve(theContainer)
-        except :
-            obj = None
-            MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
-        if obj is None:
-            container = None
-        else:
-            container = obj._narrow(Engines.Container)
-            if container is None:
-                MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
-            else :
-                MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
-            return container
-        #shstr = os.getenv( "PWD" ) + "/"
-        #shstr += "runSession ./SALOME_ContainerPy.py "
-        shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
-        #shstr = "runSession SALOME_ContainerPy.py "
-        shstr += ContainerName
-
-        # mpv: fix for SAL4731 - allways create new file to write log of server
-        num = 1
-        fileName = ""
-        while 1:
-            fileName = "/tmp/"+ContainerName+"_%i.log"%num
-            if not os.path.exists(fileName):
-                break
-            num += 1
-            pass
-        
-        shstr += " > "
-        shstr += fileName
-        shstr += " 2>&1 &"
-        
-        #shstr += " > /tmp/"
-        #shstr += ContainerName
-        #shstr += ".log 2>&1 &"
-        
-        MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
-        os.system( shstr )
-        count = 21
-        while container is None :
-            time.sleep(1)
-            count = count - 1
-            MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
-            try :
-                obj = self._naming_service.Resolve(theContainer)
-            except :
-                obj = None
-            if obj is None:
-                container = None
-            else:
-                container = obj._narrow(Engines.Container)
-                if container is None:
-                    MESSAGE(  str(containerName) + ".object exists but is not a Container" )
-                return container
-            if count == 0 :
-                return container
+        if verbose(): print("container name ",self._containerName)
 
     #-------------------------------------------------------------------------
 
     def instance(self, nameToRegister, componentName):
         MESSAGE(  "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
         self._numInstance = self._numInstance +1
-        instanceName = nameToRegister + "_inst_" + `self._numInstance`
+        instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
 
-        component=__import__(componentName)
+        component=importlib.import_module(componentName)
         factory=getattr(component,componentName)
         comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
                        instanceName, nameToRegister)
@@ -154,35 +91,35 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
     def load_impl(self, nameToRegister, componentName):
         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
         self._numInstance = self._numInstance +1
-        instanceName = nameToRegister + "_inst_" + `self._numInstance`
+        instanceName = nameToRegister + "_inst_" + repr(self._numInstance)
         interfaceName = nameToRegister
         the_command = "import " + nameToRegister + "\n"
         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
-        exec the_command
+        exec(the_command)
         comp_o = comp_i._this()
         return comp_o
-    
+
     #-------------------------------------------------------------------------
-    
+
     def import_component(self, componentName):
         MESSAGE( "SALOME_Container_i::import_component" )
         reason = ""
         try:
-            if verbose(): print "try import %s" % componentName
+            if verbose(): print("try import %s" % componentName)
             # try import component
-            module=__import__(componentName)
-            if verbose(): print "import %s is done successfully" % componentName
+            module=importlib.import_module(componentName)
+            if verbose(): print("import %s is done successfully" % componentName)
             # if import successfully, check that component is loadable
             if not hasattr(module, componentName):
                 reason = "module %s is not loadable" % componentName
-                print reason
+                print(reason)
                 pass
             pass
-        except:
+        except Exception:
             import traceback
-            print "cannot import %s" % componentName
+            print("cannot import %s" % componentName)
             traceback.print_exc()
             reason = "cannot import %s" % componentName
         return reason
@@ -192,60 +129,56 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
     def load_component_Library(self, componentName):
         MESSAGE(  "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
         ret = 0
-        instanceName = componentName + "_inst_" + `self._numInstance`
+        instanceName = componentName + "_inst_" + repr(self._numInstance)
         interfaceName = componentName
         reason = self.import_component(componentName)
         return reason == "", reason
-    
+
     #-------------------------------------------------------------------------
 
-    def create_component_instance_env(self, componentName, studyId, env):
-      return self.create_component_instance(componentName, studyId), ""
+    def create_component_instance_env(self, componentName, env):
+      return self.create_component_instance(componentName), ""
 
-    def create_component_instance(self, componentName, studyId):
-        MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
-        if studyId < 0:
-            MESSAGE( "Study ID is lower than 0!" )
-            return None
-        else:
-            self._numInstance = self._numInstance +1
-            instanceName = componentName + "_inst_" + `self._numInstance`
-            comp_iors=""
-            try:
-                component=__import__(componentName)
-                factory=getattr(component,componentName)
-                comp_i=factory(self._orb,
-                               self._poa,
-                               self._this(),
-                               self._containerName,
-                               instanceName,
-                               componentName)
-                
-                MESSAGE( "SALOME_Container_i::create_component_instance : OK")
-                comp_o = comp_i._this()
-                self._listInstances_map[instanceName] = comp_i
-            except:
-                import traceback
-                traceback.print_exc()
-                MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
-            return comp_o
+    def create_component_instance(self, componentName):
+        MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) )
+        self._numInstance = self._numInstance +1
+        instanceName = componentName + "_inst_" + repr(self._numInstance)
+        comp_iors=""
+        try:
+            component=importlib.import_module(componentName)
+            factory=getattr(component,componentName)
+            comp_i=factory(self._orb,
+                           self._poa,
+                           self._this(),
+                           self._containerName,
+                           instanceName,
+                           componentName)
+
+            MESSAGE( "SALOME_Container_i::create_component_instance : OK")
+            comp_o = comp_i._this()
+            self._listInstances_map[instanceName] = comp_i
+        except Exception:
+            import traceback
+            traceback.print_exc()
+            MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
+        return comp_o
 
     #-------------------------------------------------------------------------
 
-    def find_component_instance(self, registeredName, studyId):
+    def find_component_instance(self, registeredName):
         anEngine = None
-        keysList = self._listInstances_map.keys()
-        i = 0
-        while i < len(keysList):
-            instance = keysList[i]
+        for instance in self._listInstances_map:
             if find(instance,registeredName) == 0:
                 anEngine = self._listInstances_map[instance]
-                if studyId == anEngine.getStudyId():
-                    return anEngine._this()
-            i = i + 1
-        return anEngine._this()
-        
-        
+                return anEngine._this()
+        return anEngine
+
+
+    #-------------------------------------------------------------------------
+
+    def create_python_service_instance(self, CompName):
+        return self.create_component_instance(CompName)
+
     #-------------------------------------------------------------------------
 
     def remove_impl(self, component):
@@ -275,6 +208,41 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
 
     #-------------------------------------------------------------------------
 
+    def getNumberOfCPUCores(self):
+        return salome_psutil.getNumberOfCPUCores()
+
+    #-------------------------------------------------------------------------
+
+    def loadOfCPUCores(self):
+        return salome_psutil.loadOfCPUCores(self._load_script)
+
+    #-------------------------------------------------------------------------
+
+    def setPyScriptForCPULoad(self, script):
+        self._load_script = script
+
+    #-------------------------------------------------------------------------
+
+    def resetScriptForCPULoad(self):
+        self._load_script = None
+
+    #-------------------------------------------------------------------------
+
+    def getTotalPhysicalMemory(self):
+        return salome_psutil.getTotalPhysicalMemory()
+
+    #-------------------------------------------------------------------------
+
+    def getTotalPhysicalMemoryInUse(self):
+        return salome_psutil.getTotalPhysicalMemoryInUse()
+
+    #-------------------------------------------------------------------------
+
+    def getTotalPhysicalMemoryInUseByMe(self):
+        return salome_psutil.getTotalPhysicalMemoryInUseByMe()
+
+    #-------------------------------------------------------------------------
+
     def _get_name(self):
         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
         return self._containerName
@@ -287,7 +255,7 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
         return self._machineName
 
     #-------------------------------------------------------------------------
-    
+
     def _get_machineName(self):
         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
         self._machineName = "localhost"
@@ -310,23 +278,113 @@ class SALOME_ContainerPy_i (Engines__POA.Container):
 
 #=============================================================================
 
+class SALOME_ContainerPy_i(SALOME_ContainerPy_Gen_i):
+    """
+    Implementation with naming_service server
+    """
+    def __init__(self, orb, poa, containerName):
+        SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName)
+        naming_service = SALOME_NamingServicePy_i(self._orb)
+        self._naming_service = naming_service
+        MESSAGE( str(Container_path) )
+        naming_service.Register(self._this(), Container_path)
+
+    #-------------------------------------------------------------------------
+
+    def start_impl(self, ContainerName):
+        MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
+        myMachine=getShortHostName()
+        theContainer = "/Containers/" + myMachine + "/" + ContainerName
+        try:
+            obj = self._naming_service.Resolve(theContainer)
+        except :
+            obj = None
+            MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
+        if obj is None:
+            container = None
+        else:
+            container = obj._narrow(Engines.Container)
+            if container is None:
+                MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
+            else :
+                MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
+            return container
+        #shstr = os.getenv( "PWD" ) + "/"
+        #shstr += "runSession ./SALOME_ContainerPy.py "
+        shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
+        #shstr = "runSession SALOME_ContainerPy.py "
+        shstr += ContainerName
+
+        # mpv: fix for SAL4731 - always create new file to write log of server
+        num = 1
+        fileName = ""
+        while 1:
+            fileName = "/tmp/"+ContainerName+"_%i.log"%num
+            if not os.path.exists(fileName):
+                break
+            num += 1
+            pass
+
+        shstr += " > "
+        shstr += fileName
+        shstr += " 2>&1 &"
+
+        #shstr += " > /tmp/"
+        #shstr += ContainerName
+        #shstr += ".log 2>&1 &"
+
+        MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
+        os.system( shstr )
+        count = 21
+        while container is None :
+            time.sleep(1)
+            count = count - 1
+            MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
+            try :
+                obj = self._naming_service.Resolve(theContainer)
+            except :
+                obj = None
+            if obj is None:
+                container = None
+            else:
+                container = obj._narrow(Engines.Container)
+                if container is None:
+                    MESSAGE(  str(containerName) + ".object exists but is not a Container" )
+                return container
+            if count == 0 :
+                return container
+
+    pass
+
+class SALOME_ContainerPy_SSL_i(SALOME_ContainerPy_Gen_i):
+    """
+    Implementation with naming_service server
+    """
+    def __init__(self, orb, poa, containerName):
+        SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName)
+        naming_service = SALOME_Embedded_NamingService()
+        self._naming_service = naming_service._this()
+
+    def get_embedded_NS_if_ssl(self):
+        return self._naming_service
+
 if __name__ == "__main__":
   # change the stdout buffering to line buffering (same as C++ cout buffering)
   sys.stdout=os.fdopen(1,"w",1)
   #initialise the ORB and find the root POA
-  if verbose():print "Starting ",sys.argv[1]
+  if verbose():print("Starting ",sys.argv[1])
   orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
   poa = orb.resolve_initial_references("RootPOA")
-  if verbose():print "ORB and POA initialized"
+  if verbose():print("ORB and POA initialized")
 
   #create an instance of SALOME_ContainerPy_i and a Container reference
   #containerName = "FactoryServerPy"
   MESSAGE( str(sys.argv) )
   containerName = sys.argv[1]
   cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
-  if verbose():print "SALOME_ContainerPy_i instance created ",cpy_i 
+  if verbose():print("SALOME_ContainerPy_i instance created ",cpy_i)
   cpy_o = cpy_i._this()
-  if verbose():print "SALOME_ContainerPy_i instance activated ",cpy_o
+  if verbose():print("SALOME_ContainerPy_i instance activated ",cpy_o)
   sys.stdout.flush()
   sys.stderr.flush()
 
@@ -336,4 +394,4 @@ if __name__ == "__main__":
 
   #Block for ever
   orb.run()
-  if verbose():print "SALOME_ContainerPy_i shutdown"
+  if verbose():print("SALOME_ContainerPy_i shutdown")