1 // Copyright (C) 2021-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "KernelBasis.hxx"
26 static bool DEFAULT_SSL_MODE = true;
27 static bool GUI_MODE = false;
29 // IOR of SALOME_Embedded_NamingService servant
30 static std::string IOR_OF_EMBEDDED_NS;
34 return DEFAULT_SSL_MODE;
37 void setSSLMode(bool sslMode)
39 DEFAULT_SSL_MODE = sslMode;
47 void setGUIMode(bool guiMode)
52 std::string getIOROfEmbeddedNS()
54 return IOR_OF_EMBEDDED_NS;
57 void setIOROfEmbeddedNS(const std::string& ior)
59 IOR_OF_EMBEDDED_NS = ior;
65 * Callable from Python in case if sys.stdout is not connected to tty
67 void WriteInStdout(const std::string& msg)
69 std::cout << msg << std::endl << std::flush;
73 * Callable from Python in case if sys.stdout is not connected to tty
75 void WriteInStderr(const std::string& msg)
77 std::cerr << msg << std::endl << std::flush;
82 static constexpr char IN_PROCESS_VALUE = 0;
83 static constexpr char IN_PROCESS_VALUE_STR[] = "InProcess";
84 static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE = 1;
85 static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE_STR[] = "OutOfProcessNoReplay";
86 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE = 2;
87 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR[] = "OutOfProcessWithReplay";
88 static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE = 3;
89 static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR[] = "OutOfProcessNoReplayFT";
90 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE = 4;
91 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR[] = "OutOfProcessWithReplayFT";
93 static PyExecutionMode FromIntToPyExecutionMode(char value)
97 case IN_PROCESS_VALUE:
98 return PyExecutionMode::InProcess;
99 case OUT_OF_PROCESS_NO_REPLAY_VALUE:
100 return PyExecutionMode::OutOfProcessNoReplay;
101 case OUT_OF_PROCESS_WITH_REPLAY_VALUE:
102 return PyExecutionMode::OutOfProcessWithReplay;
103 case OUT_OF_PROCESS_NO_REPLAY_FT_VALUE:
104 return PyExecutionMode::OutOfProcessNoReplayFT;
105 case OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE:
106 return PyExecutionMode::OutOfProcessWithReplayFT;
108 throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
111 static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
113 if(value == IN_PROCESS_VALUE_STR)
114 return PyExecutionMode::InProcess;
115 if(value == OUT_OF_PROCESS_NO_REPLAY_VALUE_STR)
116 return PyExecutionMode::OutOfProcessNoReplay;
117 if(value == OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR)
118 return PyExecutionMode::OutOfProcessWithReplay;
119 if(value == OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR)
120 return PyExecutionMode::OutOfProcessNoReplayFT;
121 if(value == OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR)
122 return PyExecutionMode::OutOfProcessWithReplayFT;
123 throw std::range_error("FromStrToPyExecutionMode : Invalid str value for py execution mode !");
126 static std::string FromExecutionModeToStr(PyExecutionMode execMode)
130 case PyExecutionMode::InProcess:
131 return IN_PROCESS_VALUE_STR;
132 case PyExecutionMode::OutOfProcessNoReplay:
133 return OUT_OF_PROCESS_NO_REPLAY_VALUE_STR;
134 case PyExecutionMode::OutOfProcessWithReplay:
135 return OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR;
136 case PyExecutionMode::OutOfProcessNoReplayFT:
137 return OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR;
138 case PyExecutionMode::OutOfProcessWithReplayFT:
139 return OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR;
141 throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
146 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
148 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
150 std::string SALOME_REPLAY_FILES_DIR;
152 int SALOME::GetBigObjOnDiskThreshold()
154 return SALOME_BIG_OBJ_ON_DISK_THRES;
157 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
159 SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
162 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
164 static std::string SALOME_FILE_BIG_OBJ_DIR;
166 constexpr int DFT_SALOME_NB_RETRY = 1;
168 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
170 static bool SALOME_FW_CWD_STATUS = false;
172 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
176 case SALOME::SHARED_FILE_SYSTEM_PROTOCOL:
177 return SALOME::BigObjTransferProtocol::SharedFileSystem;
178 case SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL:
179 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
181 throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
185 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
187 if( protocol == SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR )
188 return SALOME::BigObjTransferProtocol::SharedFileSystem;
189 if( protocol == SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR )
190 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
191 throw std::runtime_error("BigObjOnDiskProtocolFromStr unrecognized protocol !");
194 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
198 case SALOME::BigObjTransferProtocol::SharedFileSystem:
199 return SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR;
200 case SALOME::BigObjTransferProtocol::SSDCopyFileSystem:
201 return SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR;
203 throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
208 * This method returns the protocol of proxy transfert and the directory
210 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
212 if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
214 directory = SALOME_FILE_BIG_OBJ_DIR;
215 return SALOME::BigObjTransferProtocol::SharedFileSystem;
217 std::string protocol = SALOME_FILE_BIG_OBJ_DIR.substr(0,3);
218 directory = SALOME_FILE_BIG_OBJ_DIR.substr(3);
219 if( protocol[0]!=SALOME_FILE_BIG_OBJ_DIR_SEP || protocol[2]!=SALOME_FILE_BIG_OBJ_DIR_SEP)
221 directory = SALOME_FILE_BIG_OBJ_DIR;
222 return SALOME::BigObjTransferProtocol::SharedFileSystem;
224 std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
225 short iproxyprot = 0;
227 return FromIntToBigObjOnDiskProtocol( iproxyprot );
230 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
232 return SALOME_FILE_BIG_OBJ_DIR;
235 std::string SALOME::GetDirectoryForReplayFiles()
237 return SALOME_REPLAY_FILES_DIR;
240 void SALOME::SetDirectoryForReplayFiles(const std::string& directory)
242 SALOME_REPLAY_FILES_DIR = directory;
245 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
247 SALOME_FILE_BIG_OBJ_DIR = directory;
250 bool SALOME::BigObjOnDiskDirectoryDefined()
252 return ! SALOME_FILE_BIG_OBJ_DIR.empty();
255 void SALOME::SetNumberOfRetry(int nbRetry)
257 SALOME_NB_RETRY = nbRetry;
260 int SALOME::GetNumberOfRetry()
262 return SALOME_NB_RETRY;
265 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
267 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
269 DefaultPyExecMode = mode;
272 void SALOME::SetPyExecutionModeStr(const std::string& mode)
274 SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
277 std::vector<std::string> SALOME::GetAllPyExecutionModes()
279 return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
282 std::string SALOME::GetPyExecutionModeStr()
284 return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
287 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
289 auto isEnvVarSet = []() -> SALOME::PyExecutionMode
291 const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
292 if (envVar && (envVar[0] != '\0'))
294 const int numValue = std::stoi(envVar);
295 return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
297 return SALOME::PyExecutionMode::InProcess;
299 if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
300 DefaultPyExecMode = isEnvVarSet();
301 return DefaultPyExecMode;
304 bool SALOME::GetForwardCurrentDirectoryStatus()
306 return SALOME_FW_CWD_STATUS;
309 void SALOME::SetForwardCurrentDirectoryStatus(bool newStatus)
311 SALOME_FW_CWD_STATUS = newStatus;
319 # include <windows.h>
324 #define getcwd _getcwd
327 auto CStyleDeleter = [](char *ptr) { if(ptr) free(ptr); };
328 using AutoCDeleter = std::unique_ptr<char, decltype(CStyleDeleter)>;
330 std::string SALOME::GetCurrentWorkingDirectory()
332 AutoCDeleter cwdPtr( getcwd( nullptr,10000 ), CStyleDeleter );
333 return std::string( cwdPtr.get() );