From: Anthony Geay Date: Tue, 25 Jun 2024 08:04:48 +0000 (+0200) Subject: [EDF30062] : Forward of current directory mecanism X-Git-Tag: V9_13_0b1~7 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=15a97ac169f8acd9cfcb6c4f9ef236d79c9d6a32;p=modules%2Fkernel.git [EDF30062] : Forward of current directory mecanism --- diff --git a/idl/SALOME_Component.idl b/idl/SALOME_Component.idl index e73f0b9bc..3e550f8ba 100644 --- a/idl/SALOME_Component.idl +++ b/idl/SALOME_Component.idl @@ -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(); diff --git a/src/Basics/KernelBasis.cxx b/src/Basics/KernelBasis.cxx index ed6837dc8..9b548c557 100644 --- a/src/Basics/KernelBasis.cxx +++ b/src/Basics/KernelBasis.cxx @@ -19,6 +19,7 @@ #include "KernelBasis.hxx" +#include #include #include @@ -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 +#else +# include +#endif +} + +#ifdef WIN32 +#define getcwd _getcwd +#endif + +auto CStyleDeleter = [](char *ptr) { if(ptr) free(ptr); }; +using AutoCDeleter = std::unique_ptr; + +std::string SALOME::GetCurrentWorkingDirectory() +{ + AutoCDeleter cwdPtr( getcwd( nullptr,10000 ), CStyleDeleter ); + return std::string( cwdPtr.get() ); +} diff --git a/src/Basics/KernelBasis.hxx b/src/Basics/KernelBasis.hxx index 121bf978c..de5c1e092 100644 --- a/src/Basics/KernelBasis.hxx +++ b/src/Basics/KernelBasis.hxx @@ -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(); } diff --git a/src/Basics/KernelBasis.i b/src/Basics/KernelBasis.i index 8b6ae421e..e24211dca 100644 --- a/src/Basics/KernelBasis.i +++ b/src/Basics/KernelBasis.i @@ -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(); diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 173633db4..cc07f9fdc 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -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 ); diff --git a/src/Container/SALOME_Container.py b/src/Container/SALOME_Container.py index a7d2f83c5..8af9050a3 100644 --- a/src/Container/SALOME_Container.py +++ b/src/Container/SALOME_Container.py @@ -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 diff --git a/src/Container/SALOME_ContainerManager.cxx b/src/Container/SALOME_ContainerManager.cxx index 688358c1e..8566a0de8 100644 --- a/src/Container/SALOME_ContainerManager.cxx +++ b/src/Container/SALOME_ContainerManager.cxx @@ -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); diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index 4acef01d2..758082ffa 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -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; diff --git a/src/Container/Test/testProxy.py b/src/Container/Test/testProxy.py index 962e2ab07..1be8344fe 100644 --- a/src/Container/Test/testProxy.py +++ b/src/Container/Test/testProxy.py @@ -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()