]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
[EDF30062] : Forward of current directory mecanism
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 25 Jun 2024 08:04:48 +0000 (10:04 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 25 Jun 2024 08:04:48 +0000 (10:04 +0200)
idl/SALOME_Component.idl
src/Basics/KernelBasis.cxx
src/Basics/KernelBasis.hxx
src/Basics/KernelBasis.i
src/Container/Container_i.cxx
src/Container/SALOME_Container.py
src/Container/SALOME_ContainerManager.cxx
src/Container/SALOME_Container_i.hxx
src/Container/Test/testProxy.py

index e73f0b9bc29280ffdcd6abea5e987a979a396b90..3e550f8ba34f2069fc376413ab8892ce3df74029 100644 (file)
@@ -94,6 +94,8 @@ module Engines
     
     void set_number_of_retry(in long nbRetry);
 
+    void set_current_directory(in string cwd);
+
     void set_startup_code(in string codeAtStartUp);
 
     string get_startup_code();
index ed6837dc80a49f81387163fb96a0ea3fd07a43bf..9b548c55749f92a86f6ce6aeedc768bf6113cd5e 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "KernelBasis.hxx"
 
+#include <memory>
 #include <sstream>
 #include <stdexcept>
 
@@ -166,6 +167,8 @@ constexpr int DFT_SALOME_NB_RETRY = 1;
 
 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
 
+static bool SALOME_FW_CWD_STATUS = false;
+
 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
 {
   switch( protocol )
@@ -297,3 +300,35 @@ SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
     DefaultPyExecMode = isEnvVarSet();
   return DefaultPyExecMode;
 }
+
+bool SALOME::GetForwardCurrentDirectoryStatus()
+{
+  return SALOME_FW_CWD_STATUS;
+}
+
+void SALOME::SetForwardCurrentDirectoryStatus(bool newStatus)
+{
+  SALOME_FW_CWD_STATUS = newStatus;
+}
+
+extern "C"
+{
+#ifndef WIN32
+# include <unistd.h>
+#else
+# include <windows.h>
+#endif
+}
+
+#ifdef WIN32
+#define getcwd _getcwd
+#endif
+
+auto CStyleDeleter = [](char *ptr) { if(ptr) free(ptr); };
+using AutoCDeleter = std::unique_ptr<char, decltype(CStyleDeleter)>;
+
+std::string SALOME::GetCurrentWorkingDirectory()
+{
+  AutoCDeleter cwdPtr( getcwd( nullptr,10000 ), CStyleDeleter );
+  return std::string( cwdPtr.get() );
+}
index 121bf978cb966bde68e72d4814aa09b38f1626c7..de5c1e092422700d04b6443ac9b00c7ac7a54ff0 100644 (file)
@@ -65,4 +65,7 @@ namespace SALOME
   bool BASICS_EXPORT BigObjOnDiskDirectoryDefined();
   void BASICS_EXPORT SetNumberOfRetry(int nbRetry);
   int BASICS_EXPORT GetNumberOfRetry();
+  bool BASICS_EXPORT GetForwardCurrentDirectoryStatus();
+  void BASICS_EXPORT SetForwardCurrentDirectoryStatus(bool newStatus);
+  std::string BASICS_EXPORT GetCurrentWorkingDirectory();
 }
index 8b6ae421e206c4e3cad7f20ac46ab4b9945945aa..e24211dcaf06bec030eef999e8119bc53ef82c05 100644 (file)
@@ -62,6 +62,8 @@ using namespace SALOME;
 %rename (SetDirectoryForReplayFiles) SetDirectoryForReplayFilesSwig;
 %rename (SetNumberOfRetry) SetNumberOfRetrySwig;
 %rename (GetNumberOfRetry) GetNumberOfRetrySwig;
+%rename (GetForwardCurrentDirectoryStatus) GetForwardCurrentDirectoryStatusSwig;
+%rename (SetForwardCurrentDirectoryStatus) SetForwardCurrentDirectoryStatusSwig;
 
 bool getSSLMode();
 void setSSLMode(bool sslMode);
@@ -153,6 +155,16 @@ int GetNumberOfRetrySwig()
   return SALOME::GetNumberOfRetry( );
 }
 
+bool GetForwardCurrentDirectoryStatusSwig()
+{
+  return SALOME::GetForwardCurrentDirectoryStatus();
+}
+
+void SetForwardCurrentDirectoryStatusSwig(bool newStatus)
+{
+  SALOME::SetForwardCurrentDirectoryStatus(newStatus);
+}
+
 std::string GetDirectoryForReplayFilesSwig()
 {
   return SALOME::GetDirectoryForReplayFiles();
index 173633db4c6cd265f065e7fa412928d616be6ca2..cc07f9fdc656fe7c7b0c80e6ecf75f93be6fa357 100644 (file)
@@ -1185,6 +1185,18 @@ void Abstract_Engines_Container_i::set_number_of_retry(CORBA::Long nbRetry)
   SALOME::SetNumberOfRetry( nbRetry );
 }
 
+void Abstract_Engines_Container_i::set_current_directory(const char *cwd)
+{
+  AutoGIL gstate;
+  AutoPyRef result = PyObject_CallMethod(_pyCont,(char*)"setCurrentDirectory","s",cwd,nullptr);
+  if (PyErr_Occurred())
+  {
+    std::string error("can not set logfilename");
+    PyErr_Print();
+    THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
+  }
+}
+
 void Abstract_Engines_Container_i::set_directory_for_replay_files(const char *directory)
 {
   SALOME::SetDirectoryForReplayFiles( directory );
index a7d2f83c58e483c98aa9e41399f4b1da022cfa54..8af9050a326de43a51c1492065cb7f61265d2e07 100644 (file)
@@ -181,6 +181,9 @@ class SALOME_Container_Abstract_i(abc.ABC):
         import salome_utils
         salome_utils.positionVerbosityOfLoggerRegardingState()
         logging.debug("positionVerbosityOfLogger PID = {}".format(os.getpid()))
+
+    def setCurrentDirectory(self, newCwd):
+        os.chdir( newCwd )
     
     def monitoringtimeresms(self):
         return self._timeIntervalInMs
index 688358c1e023efd9e5697d974cdcea9da507b642..8566a0de80ad5a8e089444676c0391cff055a603 100644 (file)
@@ -569,6 +569,8 @@ Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::Con
           }
         }
         cont->override_environment_python( envCorba );
+        if( SALOME::GetForwardCurrentDirectoryStatus() )
+          cont->set_current_directory( SALOME::GetCurrentWorkingDirectory().c_str() );
         if( !_code_to_exe_on_startup.empty() )
         {
           INFOS("[GiveContainer] container " << containerNameInNS << " python code executed " << _code_to_exe_on_startup);
index 4acef01d28c9083a7abc202f8b03fbaf0ae090a7..758082ffa1325122e6c0899caafd31d86ab0f036 100644 (file)
@@ -88,6 +88,8 @@ public:
 
   void set_number_of_retry(CORBA::Long nbRetry) override;
 
+  void set_current_directory(const char *cwd) override;
+
   void set_directory_for_replay_files(const char *directory) override;
   
   void set_startup_code(const char *codeAtStartUp) override;
index 962e2ab0721c0b4552bc9ad1fda2bf52c27270f5..1be8344fefa996202909149da99f41183898a649 100644 (file)
@@ -25,6 +25,7 @@ import Engines
 import pickle
 import tempfile
 import pylauncher
+import KernelBasis
 
 class TestProxy(unittest.TestCase):
     def testProxy(self):
@@ -104,6 +105,65 @@ with open("{}","w") as f:
                 self.assertTrue(f.read()=="coucou")
             os.unlink( tmpFileName.name ) # context manager do not clean file
 
+    def testWorkingDirectoryForward(self):
+        """
+        [EDF30062] test of forward of current directory overriding entry in Catalog
+        """
+        KernelBasis.SetForwardCurrentDirectoryStatus( True ) # key point
+        hostname = "localhost"
+        cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
+
+        with tempfile.TemporaryDirectory() as tmpdirname:
+            os.chdir( str( tmpdirname ) )
+            cp = pylauncher.GetRequestForGiveContainer("localhost","gg")
+            cont = salome.cm.GiveContainer(cp)
+
+            pyscript2 = cont.createPyScriptNode("testScript","""import os
+ret = os.getcwd()
+""")
+            #
+            import SALOME_PyNode
+            poa = salome.orb.resolve_initial_references("RootPOA")
+            obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( ([],{}) ))
+            id_o = poa.activate_object(obj)
+            refPtr = poa.id_to_reference(id_o)
+            #
+            pyscript2.executeFirst(refPtr)
+            ret2 = pyscript2.executeSecond(["ret"])
+            #
+            ret2 = ret2[0]
+            ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
+            self.assertEqual(ret3,str(tmpdirname)) # key point
+            #
+            cont.Shutdown()
+        ################
+        KernelBasis.SetForwardCurrentDirectoryStatus( False ) # key point
+        cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
+
+        with tempfile.TemporaryDirectory() as tmpdirname:
+            os.chdir( str( tmpdirname ) )
+            cp = pylauncher.GetRequestForGiveContainer("localhost","gg")
+            cont = salome.cm.GiveContainer(cp)
+
+            pyscript2 = cont.createPyScriptNode("testScript","""import os
+ret = os.getcwd()
+""")
+            #
+            import SALOME_PyNode
+            poa = salome.orb.resolve_initial_references("RootPOA")
+            obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( ([],{}) ))
+            id_o = poa.activate_object(obj)
+            refPtr = poa.id_to_reference(id_o)
+            #
+            pyscript2.executeFirst(refPtr)
+            ret2 = pyscript2.executeSecond(["ret"])
+            #
+            ret2 = ret2[0]
+            ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
+            self.assertNotEqual(ret3,str(tmpdirname)) # key point
+            #
+            cont.Shutdown()
+
 if __name__ == '__main__':
     salome.standalone()
     salome.salome_init()