Salome HOME
[bos #37406][CEA] Fixed segfault in YACS_YacsRuntimeTest on UB22,FD36 and FD37 by...
[modules/yacs.git] / src / runtime / RuntimeSALOME.cxx
index 94b205986f021f812f3d97c16e1760d70b4bf427..55401af07229a27a718aefaaae0b12b289f80cd1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -59,6 +59,7 @@
 #include "SalomeContainer.hxx"
 #include "CppContainer.hxx"
 #include "SalomeHPContainer.hxx"
+#include "PythonCppUtils.hxx"
 
 //Nodes
 #include "PythonNode.hxx"
 #include "CalStreamPort.hxx"
 
 #ifdef SALOME_KERNEL
-#include "SALOME_NamingService.hxx"
+#include "SALOME_NamingService_Wrapper.hxx"
 #include "SALOME_LifeCycleCORBA.hxx"
-#include "SALOME_NamingService.hxx"
 #include "SALOME_ResourcesManager.hxx"
 #include "SALOME_ContainerManager.hxx"
 #include "SALOMEconfig.h"
+#include "SALOME_Embedded_NamingService.hxx"
 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
 
 #endif
 #include <omniORB4/CORBA.h>
 #include <iostream>
 #include <sstream>
+#include <string>
 #include <cassert>
+#include <memory>
 
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
 using namespace std;
 using namespace YACS::ENGINE;
 
+std::unique_ptr<SALOME_NamingService_Container_Abstract> RuntimeSALOME::getNS()
+{
+  std::unique_ptr<SALOME_NamingService_Container_Abstract> ret(new SALOME_NamingService_Wrapper);
+  return ret;
+}
+
 void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
 {
   if (! Runtime::_singleton)
@@ -241,6 +250,16 @@ RuntimeSALOME::~RuntimeSALOME()
     {
       delete (*pt).second;
     }
+  _connectionManager.ShutdownWithExit();
+}
+
+void RuntimeSALOME::loadModulCatalog()
+{
+  AutoGIL agil;
+  const char * SCRIPT = "from salome_kernel import list_of_catalogs_regarding_environement\n"
+"import KernelModuleCatalog\n"
+"KernelModuleCatalog.myModuleCatalog( list_of_catalogs_regarding_environement() )\n";
+  PyRun_SimpleString(SCRIPT);
 }
 
 //! CORBA and Python initialization
@@ -314,7 +333,9 @@ void RuntimeSALOME::init(long flags, int argc, char* argv[])
               }
               PySys_SetArgv(pyArgc, changed_pyArgv);
             }
+#if PY_VERSION_HEX < 0x03070000
           PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
+#endif
           PyEval_SaveThread(); /* Release the thread state */
           //here we do not have the Global Interpreter Lock
         }
@@ -446,41 +467,93 @@ void RuntimeSALOME::fini()
     }
 }
 
+PyObject *RuntimeSALOME::launchSubProcess(const std::vector<std::string>& cmds)
+{
+  std::ostringstream oss; oss << "from subprocess import Popen" << std::endl;
+  oss << "p = Popen([";
+  for(auto i = 0 ; i < cmds.size() ; ++i)
+  {
+    oss << " " << "\"" << cmds[i] << "\"";
+    if(i < cmds.size()-1)
+      oss << ", ";
+    else
+      oss << " ";
+  }
+  oss << "])";
+  AutoGIL agil;
+  AutoPyRef context = PyDict_New();
+  PyDict_SetItemString( context, "__builtins__", getBuiltins() );
+  std::string errorDetails;
+  try
+  {
+    PythonNode::ExecuteLocalInternal(oss.str().c_str(),context,errorDetails);
+  }
+  catch(const YACS::Exception& e)
+  {
+    std::cerr << e.what() << std::endl << errorDetails << std::endl;
+    throw e;
+  }
+  PyObject *ret = PyDict_GetItemString(context,"p");
+  Py_XINCREF(ret);
+  Py_XINCREF(ret);
+  return ret;
+}
+
 std::vector< std::pair<std::string,int> > RuntimeSALOME::getCatalogOfComputeNodes() const
 {
   CORBA::ORB_ptr orb(getOrb());
-  SALOME_NamingService namingService;
+  SALOME_NamingService_Wrapper namingService;
   try
   {
     namingService.init_orb(orb);
   }
   catch(SALOME_Exception& e)
   {
-    throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Unable to contact the SALOME Naming Service");
+    throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to contact the SALOME Naming Service");
   }
   CORBA::Object_var obj(namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS));
   if(CORBA::is_nil(obj))
-    throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Unable to access to the resource manager !");
+    throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Unable to access to the resource manager !");
   Engines::ResourcesManager_var resManager(Engines::ResourcesManager::_narrow(obj));
   if(CORBA::is_nil(resManager))
-    throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Internal error ! The entry attached to the res manager in NS does not have right type !");
+    throw Exception("RuntimeSALOME::getCatalogOfComputeNodes : Internal error ! The entry attached to the res manager in NS does not have right type !");
   std::vector< std::pair<std::string,int> > ret;
+  Engines::ResourceParameters params;
+  params.name = "";
+  params.hostname = "";
+  params.OS = "";
+  params.nb_proc = 0;
+  params.mem_mb = 0;
+  params.cpu_clock = 0;
+  params.nb_node = 0;
+  params.nb_proc_per_node = 0;
+  params.policy = "";
+  params.can_launch_batch_jobs = false;
+  params.can_run_containers = true;
+  params.componentList.length(0);
+  try
+  {
+    Engines::ResourceList_var resourceList;
+    resourceList = resManager->GetFittingResources(params);
+    ret.reserve(resourceList->length());
+    for(int i = 0; i<resourceList->length(); i++)
+    {
+      const char* resource_name = resourceList[i];
+      std::string std_resource_name = resource_name;
+      Engines::ResourceDefinition_var resource_definition
+                              = resManager->GetResourceDefinition(resource_name);
+      int nb_cores = resource_definition->nb_node *
+                     resource_definition->nb_proc_per_node;
+      ret.push_back(std::pair<std::string,int>(resource_name, nb_cores));
+    }
+  }
+  catch(SALOME::SALOME_Exception& e)
   {
-    Engines::ResourceList *rl(0);
-    Engines::IntegerList *il(0);
-    resManager->ListAllAvailableResources(rl,il);
-    int sz(rl->length());
-    if(il->length()!=sz)
-      throw Exception("SalomeContainerToolsSpreadOverTheResDecorator::getParameters : Internal error ! Invalid size !");
-    ret.resize(sz);
-    for(int i=0;i<sz;i++)
-      {
-        std::string s((*rl)[i]);
-        ret[i]=std::pair<std::string,int>(s,(*il)[i]);
-      }
-    delete rl;
-    delete il;
+    std::string message;
+    message=e.details.text.in();
+    throw Exception(message);
   }
+
   return ret;
 }
 
@@ -1837,6 +1910,36 @@ CORBA::ORB_ptr RuntimeSALOME::getOrb() const
   return _orb;
 }
 
+/*!
+ * Retrieve from custom NS the entry. Custom NS is supposed to be hosted in current process.
+ * This method try to emulate CORBA ns convention : "corbaname:rir:#test.my_context/Echo.Object" is converted into "Echo"
+ * 
+ * See Engines::EmbeddedNamingService
+ */
+CORBA::Object_var RuntimeSALOME::getFromNS(const char *entry) const
+{
+  CORBA::Object_var ret;
+  std::string entryCpp(entry);
+  if(entryCpp.substr(0,4) == "IOR:")
+  {
+    ret = _orb->string_to_object( entry );
+  }
+  else
+  {
+    auto pos = entryCpp.find_last_of('/');
+    std::string entry1( entryCpp.substr(pos+1,std::string::npos) );
+    pos = entry1.find_last_of('.');
+    std::string entry2( entry1.substr(0,pos) );
+    Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService();
+    std::unique_ptr<Engines::IORType> iorRet( ns->Resolve( entry2.c_str() ) );
+    auto len = iorRet->length();
+    std::unique_ptr<char[]> iorTrans(new char[len+1]); iorTrans[len] = '\0';
+    for(auto i = 0 ; i < len ; ++i) iorTrans[i] = (*iorRet)[i];
+    ret = _orb->string_to_object(iorTrans.get());
+  }
+  return ret;
+}
+
 PyObject * RuntimeSALOME::getPyOrb() const
 {
   return _pyorb;