Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/kernel.git] / src / Launcher / Launcher_Job_Command.cxx
1 // Copyright (C) 2009-2011  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.
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
24 #ifdef WITH_LIBBATCH
25 #include <Batch/Batch_Constants.hxx>
26 #endif
27
28 #ifdef WNT
29 #include <io.h>
30 #define _chmod chmod
31 #endif
32
33 Launcher::Job_Command::Job_Command() {_job_type = "command";}
34
35 Launcher::Job_Command::~Job_Command() {}
36
37 void
38 Launcher::Job_Command::update_job()
39 {
40 #ifdef WITH_LIBBATCH
41   Batch::Parametre params = common_job_params();
42   params[Batch::EXECUTABLE] = buildCommandScript(params, _launch_date);
43   _batch_job->setParametre(params);
44 #endif
45 }
46
47 #ifdef WITH_LIBBATCH
48 std::string 
49 Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string launch_date)
50 {
51   // parameters
52   std::string work_directory = params[Batch::WORKDIR].str();
53
54   // File name
55   std::string launch_date_port_file = launch_date;
56   std::string launch_script = "/tmp/runCommand_" + _job_file_name + "_" + launch_date + ".sh";
57   std::ofstream launch_script_stream;
58   launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
59    
60   // Script
61   launch_script_stream << "#! /bin/bash -f" << std::endl;
62   launch_script_stream << "cd " << work_directory << std::endl;
63   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
64   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
65   if (_env_file != "")
66   {
67     std::string::size_type last = _env_file.find_last_of("/");
68     launch_script_stream << "source ./" << _env_file.substr(last+1) << std::endl;
69   }
70   launch_script_stream << "./" << _job_file_name_complete << " > " << work_directory <<"/logs/command_" << launch_date << ".log 2>&1" << std::endl;
71
72   // Return
73   launch_script_stream.flush();
74   launch_script_stream.close();
75   chmod(launch_script.c_str(), 0x1ED);
76   chmod(_job_file.c_str(), 0x1ED);
77   return launch_script;
78 }
79 #endif