]> SALOME platform Git repositories - modules/kernel.git/blob - src/Basics/KernelBasis.cxx
Salome HOME
[EDF30062] : Forward of current directory mecanism
[modules/kernel.git] / src / Basics / KernelBasis.cxx
1 // Copyright (C) 2021-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "KernelBasis.hxx"
21
22 #include <memory>
23 #include <sstream>
24 #include <stdexcept>
25
26 static bool DEFAULT_SSL_MODE = true;
27 static bool GUI_MODE = false;
28
29 // IOR of SALOME_Embedded_NamingService servant
30 static std::string IOR_OF_EMBEDDED_NS;
31
32 bool getSSLMode()
33 {
34   return DEFAULT_SSL_MODE;
35 }
36
37 void setSSLMode(bool sslMode)
38 {
39   DEFAULT_SSL_MODE = sslMode;
40 }
41
42 bool getGUIMode()
43 {
44   return GUI_MODE;
45 }
46
47 void setGUIMode(bool guiMode)
48 {
49   GUI_MODE = guiMode;
50 }
51
52 std::string getIOROfEmbeddedNS()
53 {
54   return IOR_OF_EMBEDDED_NS;
55 }
56
57 void setIOROfEmbeddedNS(const std::string& ior)
58 {
59   IOR_OF_EMBEDDED_NS = ior;
60 }
61
62 #include <iostream>
63
64 /*!
65  * Callable from Python in case if sys.stdout is not connected to tty
66  */
67 void WriteInStdout(const std::string& msg)
68 {
69   std::cout << msg << std::endl << std::flush;
70 }
71
72 /*!
73  * Callable from Python in case if sys.stdout is not connected to tty
74  */
75 void WriteInStderr(const std::string& msg)
76 {
77   std::cerr << msg << std::endl << std::flush;
78 }
79
80 namespace SALOME
81 {
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";
92
93   static PyExecutionMode FromIntToPyExecutionMode(char value)
94   {
95     switch(value)
96     {
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;
107     }
108     throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
109   }
110   
111   static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
112   {
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 !");
124   }
125
126   static std::string FromExecutionModeToStr(PyExecutionMode execMode)
127   {
128     switch(execMode)
129     {
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;
140       default:
141         throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
142     }
143   }
144 }
145
146 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
147
148 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
149
150 std::string SALOME_REPLAY_FILES_DIR;
151
152 int SALOME::GetBigObjOnDiskThreshold()
153 {
154   return SALOME_BIG_OBJ_ON_DISK_THRES;
155 }
156
157 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
158 {
159   SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
160 }
161
162 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
163
164 static std::string SALOME_FILE_BIG_OBJ_DIR;
165
166 constexpr int DFT_SALOME_NB_RETRY = 1;
167
168 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
169
170 static bool SALOME_FW_CWD_STATUS = false;
171
172 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
173 {
174   switch( protocol )
175   {
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;
180     default:
181       throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
182   }
183 }
184
185 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
186 {
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 !");
192 }
193
194 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
195 {
196   switch( protocol )
197   {
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;
202     default:
203       throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
204   }
205 }
206
207 /*!
208  * This method returns the protocol of proxy transfert and the directory
209  */
210 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
211 {
212   if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
213   {
214     directory = SALOME_FILE_BIG_OBJ_DIR;
215     return SALOME::BigObjTransferProtocol::SharedFileSystem;
216   }
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)
220   {
221     directory = SALOME_FILE_BIG_OBJ_DIR;
222     return SALOME::BigObjTransferProtocol::SharedFileSystem;
223   }
224   std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
225   short iproxyprot = 0;
226   iss >> iproxyprot;
227   return FromIntToBigObjOnDiskProtocol( iproxyprot );
228 }
229
230 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
231 {
232   return SALOME_FILE_BIG_OBJ_DIR;
233 }
234
235 std::string SALOME::GetDirectoryForReplayFiles()
236 {
237   return SALOME_REPLAY_FILES_DIR;
238 }
239
240 void SALOME::SetDirectoryForReplayFiles(const std::string& directory)
241 {
242   SALOME_REPLAY_FILES_DIR = directory;
243 }
244
245 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
246 {
247   SALOME_FILE_BIG_OBJ_DIR = directory;
248 }
249
250 bool SALOME::BigObjOnDiskDirectoryDefined()
251 {
252   return ! SALOME_FILE_BIG_OBJ_DIR.empty();
253 }
254
255 void SALOME::SetNumberOfRetry(int nbRetry)
256 {
257   SALOME_NB_RETRY = nbRetry;
258 }
259
260 int SALOME::GetNumberOfRetry()
261 {
262   return SALOME_NB_RETRY;
263 }
264
265 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
266
267 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
268 {
269   DefaultPyExecMode = mode;
270 }
271
272 void SALOME::SetPyExecutionModeStr(const std::string& mode)
273 {
274   SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
275 }
276
277 std::vector<std::string> SALOME::GetAllPyExecutionModes()
278 {
279   return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
280 }
281
282 std::string SALOME::GetPyExecutionModeStr()
283 {
284   return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
285 }
286
287 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
288 {
289   auto isEnvVarSet = []() -> SALOME::PyExecutionMode
290   {
291     const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
292     if (envVar && (envVar[0] != '\0'))
293     {
294       const int numValue = std::stoi(envVar);
295       return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
296     }
297     return SALOME::PyExecutionMode::InProcess;
298   };
299   if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
300     DefaultPyExecMode = isEnvVarSet();
301   return DefaultPyExecMode;
302 }
303
304 bool SALOME::GetForwardCurrentDirectoryStatus()
305 {
306   return SALOME_FW_CWD_STATUS;
307 }
308
309 void SALOME::SetForwardCurrentDirectoryStatus(bool newStatus)
310 {
311   SALOME_FW_CWD_STATUS = newStatus;
312 }
313
314 extern "C"
315 {
316 #ifndef WIN32
317 # include <unistd.h>
318 #else
319 # include <windows.h>
320 #endif
321 }
322
323 #ifdef WIN32
324 #define getcwd _getcwd
325 #endif
326
327 auto CStyleDeleter = [](char *ptr) { if(ptr) free(ptr); };
328 using AutoCDeleter = std::unique_ptr<char, decltype(CStyleDeleter)>;
329
330 std::string SALOME::GetCurrentWorkingDirectory()
331 {
332   AutoCDeleter cwdPtr( getcwd( nullptr,10000 ), CStyleDeleter );
333   return std::string( cwdPtr.get() );
334 }