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