Salome HOME
venv directory is configured in config_appli.xml file
[modules/kernel.git] / src / KERNEL_PY / __init__.py
index 8577a7dd29391c5d7530fe345ba41fb97becdecb..2aa01975bdc5ba6dd057e731dc61fe403a3f316e 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -136,7 +136,7 @@ if not flags:
         # dl module can be unavailable
         import dl
         flags = dl.RTLD_NOW | dl.RTLD_GLOBAL
-    except:
+    except Exception:
         pass
     pass
 if not flags:
@@ -144,7 +144,7 @@ if not flags:
         # DLFCN module can be unavailable
         import DLFCN
         flags = DLFCN.RTLD_NOW | DLFCN.RTLD_GLOBAL
-    except:
+    except Exception:
         pass
     pass
 if not flags:
@@ -152,7 +152,7 @@ if not flags:
         # ctypes module can be unavailable
         import ctypes
         flags = ctypes.RTLD_GLOBAL
-    except:
+    except Exception:
         pass
     pass
 
@@ -171,58 +171,61 @@ __EMB_SERVANT_ENV_VAR_NAME = "SALOME_EMB_SERVANT"
 def standalone():
     import os
     os.environ[__EMB_SERVANT_ENV_VAR_NAME] = "1"
+    import KernelBasis
+    KernelBasis.setSSLMode(True)
 
 def salome_init(path=None, embedded=False):
     import os
+    import KernelBasis
     if __EMB_SERVANT_ENV_VAR_NAME in os.environ:
-        salome_init_without_session()
+        KernelBasis.setSSLMode(True)
+    #
+    if KernelBasis.getSSLMode():
+        if KernelBasis.getIOROfEmbeddedNS() == "":
+            salome_init_without_session(path, embedded)
+        else:
+            salome_init_without_session_attached(path, embedded)
     else:
         salome_init_with_session(path, embedded)
 
 class StandAloneLifecyle:
+    def __init__(self, containerManager, resourcesManager):
+        self._cm = containerManager
+        self._rm = resourcesManager
+
     def FindOrLoadComponent(self,contName,moduleName):
         global orb
-        if contName == "FactoryServer" and moduleName == "GEOM":
-            import GeomHelper
-            geom_ior = GeomHelper.BuildGEOMInstance()
-            import GEOM
-            import CORBA
-            orb=CORBA.ORB_init([''])
-            geom = orb.string_to_object(geom_ior)
-            return geom
-        if contName == "FactoryServer" and moduleName == "SMESH":
-            import SMeshHelper
-            smesh_ior = SMeshHelper.BuildSMESHInstance()
-            import SMESH
-            import CORBA
-            orb=CORBA.ORB_init([''])
-            smeshInst = orb.string_to_object(smesh_ior)
-            return smeshInst
-        if contName == "FactoryServer" and moduleName == "SHAPERSTUDY":
-            from SHAPERSTUDY import SHAPERSTUDY_No_Session
-            from SALOME_ContainerPy import SALOME_ContainerPy_Gen_i
-            import PortableServer
-            import KernelServices
-            obj = orb.resolve_initial_references("RootPOA")
-            poa = obj._narrow(PortableServer.POA)
-            pman = poa._get_the_POAManager()
-            #
-            cont = SALOME_ContainerPy_Gen_i(orb,poa,"FactoryServer")
-            conId = poa.activate_object(cont)
-            conObj = poa.id_to_reference(conId)
-            #
-            pman.activate()
-            #
-            compoName = "SHAPERSTUDY"
-            servant = SHAPERSTUDY_No_Session(orb,poa,conObj,"FactoryServer","SHAPERSTUDY_inst_1",compoName)
-            ret = servant.getCorbaRef()
-            KernelServices.RegisterCompo(compoName,ret)
-            return ret
-        raise RuntimeError("Undealed situation cont = {} module = {}".format(contName,moduleName))
-
-def salome_init_without_session():
-    global lcc,myStudy,orb,modulcat
-    lcc = StandAloneLifecyle()
+        import importlib
+        builder_name = moduleName + "_SalomeSessionless"
+        moduleObj = importlib.import_module(builder_name)
+        result, orb = moduleObj.buildInstance(orb)
+        return result
+        #raise RuntimeError("Undealed situation cont = {} module = {}".format(contName,moduleName))
+
+    def getContainerManager(self):
+      return self._cm
+
+    def getResourcesManager(self):
+      return self._rm
+
+def salome_init_without_session_common(path=None, embedded=False):
+    from ORBConfigFile import writeORBConfigFileSSL
+    OMNIORB_USER_PATH = "OMNIORB_USER_PATH"
+    def RemoveOmniorbConfigFile():
+        import os
+        if "OMNIORB_CONFIG" in os.environ:
+            fileToRemove = os.environ["OMNIORB_CONFIG"]
+            if os.path.exists(fileToRemove):
+                os.unlink(fileToRemove)
+
+    if OMNIORB_USER_PATH in os.environ:
+        import atexit
+        writeORBConfigFileSSL(os.environ[OMNIORB_USER_PATH],kwargs={"with_pid":True})
+        atexit.register(RemoveOmniorbConfigFile)
+
+    global lcc,naming_service,myStudy,orb,modulcat,sg
+    import KernelBasis
+    KernelBasis.setSSLMode(True)
     import KernelDS
     myStudy = KernelDS.myStudy()
     import CORBA
@@ -231,10 +234,63 @@ def salome_init_without_session():
     import SALOME_ModuleCatalog
     from salome_kernel import list_of_catalogs_regarding_environement
     modulcat = KernelModuleCatalog.myModuleCatalog( list_of_catalogs_regarding_environement() )
-    # activate poaManager to accept co-localized CORBA calls.
+    #
     poa = orb.resolve_initial_references("RootPOA")
     poaManager = poa._get_the_POAManager()
     poaManager.activate()
+    #
+    sg = salome_iapp_init(embedded)
+    salome_study_init_without_session(path)
+    #
+    from NamingService import NamingService
+    naming_service = NamingService()
+
+def salome_init_without_session(path=None, embedded=False):
+    salome_init_without_session_common(path,embedded)
+    global lcc,cm,dsm,esm
+    import KernelLauncher
+    cm = KernelLauncher.myContainerManager()
+    lcc = StandAloneLifecyle(cm, KernelLauncher.myResourcesManager())
+    # activate poaManager to accept co-localized CORBA calls.
+    from KernelSDS import GetDSMInstance
+    import sys
+    if hasattr(sys, 'argv'):
+      argv = sys.argv
+    else:
+      argv = ['']
+    dsm = GetDSMInstance(argv)
+    # esm inherits from SALOME_CPythonHelper singleton already initialized by GetDSMInstance
+    # esm inherits also from SALOME_ResourcesManager creation/initialization (concerning SingleThreadPOA POA) when KernelLauncher.GetContainerManager() has been called
+    esm = KernelLauncher.GetExternalServer()
+    
+def salome_init_without_session_attached(path=None, embedded=False):
+    """
+    Configuration SSL inside a python interpretor launched in the SALOME_Container_No_NS_Serv.
+    In this configuration, 
+    """
+    salome_init_without_session_common(path,embedded)
+    global lcc,cm,dsm,esm
+    import CORBA
+    orb=CORBA.ORB_init([''])
+    import Engines
+    import KernelBasis
+    nsAbroad = orb.string_to_object( KernelBasis.getIOROfEmbeddedNS() )
+    import SALOME
+    CM_NAME_IN_NS = "/ContainerManager"
+    cm = orb.string_to_object( nsAbroad.Resolve(CM_NAME_IN_NS).decode() )
+    naming_service.Register(cm,CM_NAME_IN_NS)
+    RM_NAME_IN_NS = "/ResourcesManager"
+    rm = orb.string_to_object( nsAbroad.Resolve(RM_NAME_IN_NS).decode() )
+    naming_service.Register(rm,RM_NAME_IN_NS)
+    #
+    DSM_NAME_IN_NS = "/DataServerManager"
+    lcc = StandAloneLifecyle(cm,rm)
+    dsm = orb.string_to_object( nsAbroad.Resolve(DSM_NAME_IN_NS).decode() )
+    naming_service.Register(dsm,DSM_NAME_IN_NS)
+    #
+    ESM_NAME_IN_NS = "/ExternalServers"
+    esm = orb.string_to_object( nsAbroad.Resolve(ESM_NAME_IN_NS).decode() )
+    naming_service.Register(esm,ESM_NAME_IN_NS)
 
 def salome_init_with_session(path=None, embedded=False):
     """
@@ -255,7 +311,8 @@ def salome_init_with_session(path=None, embedded=False):
     global orb, lcc, naming_service, cm, esm, dsm, modulcat
     global sg
     global myStudy, myStudyName
-
+    import KernelBasis
+    KernelBasis.setSSLMode(False)
     try:
         if salome_initial:
             salome_initial=False
@@ -284,12 +341,18 @@ def salome_close():
     try:
         # study can be clear either from GUI or directly with salome.myStudy.Clear()
         myStudy.Clear()
-    except:
+    except Exception:
         pass
     salome_initial=True
     salome_iapp_close()
     salome_study_close()
     myStudy, myStudyName = None, None
+    import KernelBasis
+    if KernelBasis.getSSLMode() and not KernelBasis.getGUIMode():
+        import KernelDS
+        KernelDS.KillGlobalSessionInstance()
+        import KernelSDS
+        KernelSDS.KillCPythonHelper()
     pass
 
 def salome_NS():
@@ -321,7 +384,7 @@ def salome_walk_on_containers(ns,root):
         pass
     pass
 
-def salome_shutdown_containers():
+def salome_shutdown_containers_with_session():
     salome_init()
     ns=salome_NS()
     li = [elt for elt in salome_walk_on_containers(ns,[""])]
@@ -329,12 +392,34 @@ def salome_shutdown_containers():
     for cont,(root,cont_name) in li:
         try:
             cont.Shutdown()
-        except:
+        except Exception:
             pass
         ref_in_ns = "/".join(root+[cont_name])
         naming_service.Destroy_Name(ref_in_ns)
     print("Number of containers in NS after clean : {}".format( len( list(salome_walk_on_containers(ns,[""])) )))
+    
+def salome_shutdown_containers_without_session():
+    containersEntries = [elt for elt in naming_service.repr() if "/Containers/" == elt[:12]]
+    for containerEntry in containersEntries:
+        cont = naming_service.Resolve(containerEntry)
+        try:
+            cont.Shutdown()
+        except:
+            pass
+
+def salome_shutdown_containers():
+    import KernelBasis
+    if KernelBasis.getSSLMode():
+        salome_shutdown_containers_without_session()
+    else:
+        salome_shutdown_containers_with_session()
 
+class SessionContextManager:
+    def __enter__(self):
+        standalone()
+        salome_init()
+    def __exit__(self, type, value, traceback):
+        salome_close()
 
 #to expose all objects to pydoc
 __all__=dir()