Salome HOME
Compilation under Windows: add missing header
[modules/kernel.git] / src / Launcher / Launcher_Job_Command.cxx
1 // Copyright (C) 2009-2021  CEA/DEN, EDF R&D, OPEN CASCADE
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 // Author: AndrĂ© RIBES - EDF R&D
21 //
22 #include "Launcher_Job_Command.hxx"
23 #include "Basics_DirUtils.hxx"
24
25 #ifdef WITH_LIBBATCH
26 #include <libbatch/Constants.hxx>
27 #endif
28
29 #ifdef WIN32
30 #include <io.h>
31 #define _chmod chmod
32 #include <process.h>
33 #endif
34
35 #include <sstream>
36
37 const char Launcher::Job_Command::TYPE_NAME[] = "command";
38
39 Launcher::Job_Command::Job_Command()
40 {
41   _job_type = Launcher::Job_Command::TYPE_NAME;
42 }
43
44 Launcher::Job_Command::~Job_Command() {}
45
46 void
47 Launcher::Job_Command::update_job()
48 {
49 #ifdef WITH_LIBBATCH
50   Batch::Parametre params = common_job_params();
51   params[Batch::EXECUTABLE] = buildCommandScript(params, _launch_date);
52   _batch_job->setParametre(params);
53 #endif
54 }
55
56 #ifdef WITH_LIBBATCH
57 std::string
58 Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string launch_date)
59 {
60   // parameters
61   std::string work_directory = params[Batch::WORKDIR].str();
62
63   // File name
64   std::string launch_date_port_file = launch_date;
65   std::ostringstream str_pid;
66 #ifdef WIN32
67   str_pid << _getpid();
68 #else
69   str_pid << ::getpid();
70 #endif
71   std::string launch_script = Kernel_Utils::GetTmpDir() + "runCommand_"
72                               + _job_file_name + "_" + launch_date + "-"
73                               + str_pid.str();
74 #ifndef WIN32
75   launch_script += ".sh";
76 #else
77   launch_script += ".bat";
78 #endif
79   std::ofstream launch_script_stream;
80   launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
81
82   // Script
83 #ifndef WIN32
84   launch_script_stream << "#!/bin/sh -f" << std::endl;
85   launch_script_stream << "cd " << work_directory << std::endl;
86   // remove the exit code from any previous execution
87   launch_script_stream << "rm -f logs/exit_code.log" << std::endl;
88   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
89   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
90   if (_env_file != "")
91   {
92     std::string::size_type last = _env_file.find_last_of("/");
93     launch_script_stream << ". ./" << _env_file.substr(last+1) << std::endl;
94   }
95 #else
96   launch_script_stream << "echo OFF" << std::endl;
97   launch_script_stream << "cd " << work_directory << std::endl;
98   launch_script_stream << "set PYTHONPATH=" << work_directory << ";%PYTHONPATH%" << std::endl;
99   launch_script_stream << "set PATH=" << work_directory << ";%PATH%" << std::endl;
100   if (_env_file != "")
101   {
102     std::string::size_type last = _env_file.find_last_of("\\");
103     launch_script_stream << "call " << _env_file.substr(last+1) << std::endl;
104   }
105 #endif
106   launch_script_stream << runCommandString() << std::endl;
107 #ifndef WIN32
108   // log the exit code
109   launch_script_stream << "echo $? > logs/exit_code.log" << std::endl;
110 #endif
111
112   // Return
113   launch_script_stream.flush();
114   launch_script_stream.close();
115   chmod(launch_script.c_str(), 0x1ED);
116   chmod(_job_file.c_str(), 0x1ED);
117   return launch_script;
118 }
119
120 std::string Launcher::Job_Command::runCommandString()
121 {
122   std::ostringstream result;
123 #ifndef WIN32
124   result << "./" << _job_file_name_complete;
125 #else
126   result << "call " << _job_file_name_complete;
127 #endif
128   return result.str();
129 }
130 #endif