X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FKERNEL_PY%2F__init__.py;h=2aa01975bdc5ba6dd057e731dc61fe403a3f316e;hb=a4874256219c549a9b1ff740d549391c4bf2f25f;hp=cfa376fa33040b1a7f6ea92537fc1312bbd94c36;hpb=9a965a48d4bc1a6cd1f73229a91e77b10bffa881;p=modules%2Fkernel.git diff --git a/src/KERNEL_PY/__init__.py b/src/KERNEL_PY/__init__.py old mode 100755 new mode 100644 index cfa376fa3..2aa01975b --- a/src/KERNEL_PY/__init__.py +++ b/src/KERNEL_PY/__init__.py @@ -1,5 +1,5 @@ # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2015 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 @@ -26,7 +26,7 @@ # Module : SALOME # """ -Module salome gives access to Salome ressources. +Module salome gives access to Salome resources. variables: @@ -45,9 +45,7 @@ variables: - salome.sg : salome object to communicate with the graphical user interface (if any) - methods: - - updateObjBrowser(bool): - - getActiveStudyId(): - - getActiveStudyName(): + - updateObjBrowser(): - SelectedCount(): returns number of selected objects - getSelected(i): returns entry of selected object number i @@ -65,20 +63,18 @@ variables: - IDToObject(Entry): returns CORBA reference from entry - salome.myStudyName : active Study Name - - salome.myStudyId : active Study Id - salome.myStudy : the active Study itself (CORBA ior) - methods : defined in SALOMEDS.idl """ ## @package salome -# Module salome gives access to Salome ressources. +# Module salome gives access to Salome resources. # # \param salome.orb : CORBA orb object # \param salome.naming_service : instance of naming Service class (SALOME_NamingServicePy::SALOME_NamingServicePy_i) # \param salome.lcc : instance of lifeCycleCORBA class (SALOME_LifeCycleCORBA) # \param salome.sg : Salome object to communicate with the graphical user interface, if running (see interface in salome_iapp::SalomeOutsideGUI) # \param salome.myStudyName : active Study Name -# \param salome.myStudyId : active Study Id # \param salome.myStudy : the active Study (interface SALOMEDS::Study) # @@ -110,13 +106,13 @@ MATCH_ENDING_PATTERN="site-packages" + os.path.sep + "salome" def extend_path(pname): for dir in sys.path: - if not isinstance(dir, basestring) or not os.path.isdir(dir) or not dir.endswith(MATCH_ENDING_PATTERN): + if not isinstance(dir, str) or not os.path.isdir(dir) or not dir.endswith(MATCH_ENDING_PATTERN): continue subdir = os.path.join(dir, pname) # XXX This may still add duplicate entries to path on # case-insensitive filesystems if os.path.isdir(subdir) and subdir not in __path__: - if verbose(): print "INFO - The directory %s is appended to sys.path" % subdir + if verbose(): print("INFO - The directory %s is appended to sys.path" % subdir) __path__.append(subdir) extend_path(ROOT_PYTHONPACKAGE_NAME) @@ -140,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: @@ -148,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: @@ -156,7 +152,7 @@ if not flags: # ctypes module can be unavailable import ctypes flags = ctypes.RTLD_GLOBAL - except: + except Exception: pass pass @@ -165,93 +161,265 @@ if not flags: # sys.setdlopenflags(flags) # pass -orb, lcc, naming_service, cm,sg=None,None,None,None,None -myStudyManager, myStudyId, myStudy, myStudyName=None,None,None,None +orb, lcc, naming_service, cm, sg, esm, dsm, modulcat = None,None,None,None,None,None,None,None +myStudy, myStudyName = None,None -def setCurrentStudy(theStudy): - """ - Change current study : an existing one given by a study object. +salome_initial=True - :param theStudy: the study CORBA object to set as current study - """ - global myStudyId, myStudy, myStudyName - myStudyId, myStudy, myStudyName =salome_study.setCurrentStudy(theStudy) +__EMB_SERVANT_ENV_VAR_NAME = "SALOME_EMB_SERVANT" -def setCurrentStudyId(theStudyId=0): - """ - Change current study : an existing or new one given by Id. +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: + 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 + 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)) - :param theStudyId: the study Id (optional argument) - 0 : create a new study (default). - n (>0) : try connection to study with Id = n, or create a new one - if study not found. + 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 + orb=CORBA.ORB_init(['']) + import KernelModuleCatalog + import SALOME_ModuleCatalog + from salome_kernel import list_of_catalogs_regarding_environement + modulcat = KernelModuleCatalog.myModuleCatalog( list_of_catalogs_regarding_environement() ) + # + 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): """ - global myStudyId, myStudy, myStudyName - myStudyId, myStudy, myStudyName =salome_study.setCurrentStudyId(theStudyId) + 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) -salome_initial=1 -def salome_init(theStudyId=0,embedded=0): +def salome_init_with_session(path=None, embedded=False): """ - Performs only once SALOME general purpose intialisation for scripts. - optional argument : theStudyId - When in embedded interpreter inside IAPP, theStudyId is not used - When used without GUI (external interpreter) - 0 : create a new study (default). - n (>0) : try connection to study with Id = n, or create a new one - if study not found. - If study creation, its Id may be different from theStudyId ! + Performs only once SALOME general purpose initialisation for scripts. Provides: orb reference to CORBA lcc a LifeCycleCorba instance naming_service a naming service instance cm reference to the container manager + esm reference to external server manager + dsm reference to shared dataserver manager + modulcat reference to modulecatalog instance sg access to SALOME GUI (when linked with IAPP GUI) - myStudyManager the study manager - myStudyId active study identifier myStudy active study itself (CORBA reference) myStudyName active study name """ global salome_initial - global orb, lcc, naming_service, cm + global orb, lcc, naming_service, cm, esm, dsm, modulcat global sg - global myStudyManager, myStudyId, myStudy, myStudyName - + global myStudy, myStudyName + import KernelBasis + KernelBasis.setSSLMode(False) try: if salome_initial: - salome_initial=0 + salome_initial=False sg = salome_iapp_init(embedded) - orb, lcc, naming_service, cm = salome_kernel_init() - myStudyManager, myStudyId, myStudy, myStudyName = salome_study_init(theStudyId) + orb, lcc, naming_service, cm, esm, dsm, modulcat = salome_kernel_init() + myStudy, myStudyName = salome_study_init(path) pass pass - except RuntimeError, inst: + except RuntimeError as inst: # wait a little to avoid trace mix import time time.sleep(0.2) x = inst - print "salome.salome_init():", x - print """ + print("salome.salome_init():", x) + print(""" ============================================ May be there is no running SALOME session - salome.salome_init() is intented to be used + salome.salome_init() is intended to be used within an already running session ============================================ - """ + """) raise def salome_close(): - global salome_initial, myStudy, myStudyId, myStudyName + global salome_initial, myStudy, myStudyName try: - # study can be closed either from GUI or directly with salome.myStudy.Close() - myStudy.Close() - except: + # study can be clear either from GUI or directly with salome.myStudy.Clear() + myStudy.Clear() + except Exception: pass - salome_initial=1 + salome_initial=True salome_iapp_close() salome_study_close() - myStudyId, myStudy, myStudyName=None,None,None + 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(): + import CORBA + import CosNaming + orb = CORBA.ORB_init() + ns0 = orb.resolve_initial_references("NameService") + return ns0._narrow(CosNaming.NamingContext) + +def salome_walk_on_containers(ns,root): + import CosNaming + it = ns.list(0)[1] + if not it: + return + cont = True + while cont: + cont,obj = it.next_one() + if cont: + if obj.binding_name[0].kind == "object": + import Engines + corbaObj = ns.resolve(obj.binding_name) + if isinstance(corbaObj,Engines._objref_Container): + yield corbaObj,(root,obj.binding_name[0].id) + else: + father = ns.resolve([obj.binding_name[0]]) + for elt,elt2 in salome_walk_on_containers(father,root+[obj.binding_name[0].id]): + yield elt,elt2 + pass + pass pass +def salome_shutdown_containers_with_session(): + salome_init() + ns=salome_NS() + li = [elt for elt in salome_walk_on_containers(ns,[""])] + print("Number of containers in NS : {}".format(len(li))) + for cont,(root,cont_name) in li: + try: + cont.Shutdown() + 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()