Salome HOME
Copyright update 2021
[modules/kernel.git] / src / Launcher / Launcher_Job_Command.cxx
index 345a5ff74d6c0b92acb57929efba03d971da7dad..eb86061b8cc58b0a151e574753f0f36c12d9ed30 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2009-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2009-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #ifdef WIN32
 #include <io.h>
 #define _chmod chmod
+#include <process.h>
 #endif
 
-Launcher::Job_Command::Job_Command() {_job_type = "command";}
+#include <sstream>
+
+const char Launcher::Job_Command::TYPE_NAME[] = "command";
+
+Launcher::Job_Command::Job_Command()
+{
+  _job_type = Launcher::Job_Command::TYPE_NAME;
+}
 
 Launcher::Job_Command::~Job_Command() {}
 
@@ -46,7 +54,7 @@ Launcher::Job_Command::update_job()
 }
 
 #ifdef WITH_LIBBATCH
-std::string 
+std::string
 Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string launch_date)
 {
   // parameters
@@ -54,21 +62,52 @@ Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string l
 
   // File name
   std::string launch_date_port_file = launch_date;
-  std::string launch_script = Kernel_Utils::GetTmpDir() + "runCommand_" + _job_file_name + "_" + launch_date + ".sh";
+  std::ostringstream str_pid;
+#ifdef WIN32
+  str_pid << _getpid();
+#else
+  str_pid << ::getpid();
+#endif
+  std::string launch_script = Kernel_Utils::GetTmpDir() + "runCommand_"
+                              + _job_file_name + "_" + launch_date + "-"
+                              + str_pid.str();
+#ifndef WIN32
+  launch_script += ".sh";
+#else
+  launch_script += ".bat";
+#endif
   std::ofstream launch_script_stream;
   launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
-   
+
   // Script
-  launch_script_stream << "#! /bin/bash -f" << std::endl;
+#ifndef WIN32
+  launch_script_stream << "#!/bin/sh -f" << std::endl;
   launch_script_stream << "cd " << work_directory << std::endl;
+  // remove the exit code from any previous execution
+  launch_script_stream << "rm -f logs/exit_code.log" << std::endl;
   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
   if (_env_file != "")
   {
     std::string::size_type last = _env_file.find_last_of("/");
-    launch_script_stream << "source ./" << _env_file.substr(last+1) << std::endl;
+    launch_script_stream << ". ./" << _env_file.substr(last+1) << std::endl;
+  }
+#else
+  launch_script_stream << "echo OFF" << std::endl;
+  launch_script_stream << "cd " << work_directory << std::endl;
+  launch_script_stream << "set PYTHONPATH=" << work_directory << ";%PYTHONPATH%" << std::endl;
+  launch_script_stream << "set PATH=" << work_directory << ";%PATH%" << std::endl;
+  if (_env_file != "")
+  {
+    std::string::size_type last = _env_file.find_last_of("\\");
+    launch_script_stream << "call " << _env_file.substr(last+1) << std::endl;
   }
-  launch_script_stream << "./" << _job_file_name_complete << " > " << work_directory <<"/logs/command_" << launch_date << ".log 2>&1" << std::endl;
+#endif
+  launch_script_stream << runCommandString() << std::endl;
+#ifndef WIN32
+  // log the exit code
+  launch_script_stream << "echo $? > logs/exit_code.log" << std::endl;
+#endif
 
   // Return
   launch_script_stream.flush();
@@ -77,4 +116,15 @@ Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string l
   chmod(_job_file.c_str(), 0x1ED);
   return launch_script;
 }
+
+std::string Launcher::Job_Command::runCommandString()
+{
+  std::ostringstream result;
+#ifndef WIN32
+  result << "./" << _job_file_name_complete;
+#else
+  result << "call " << _job_file_name_complete;
+#endif
+  return result.str();
+}
 #endif