Salome HOME
Copyright update 2021
[modules/kernel.git] / src / Launcher / Launcher_Job_Command.cxx
index bdb0f2ec4e1303b156c0da7c7895b5443d6ef84d..eb86061b8cc58b0a151e574753f0f36c12d9ed30 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017  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
 #ifdef WIN32
 #include <io.h>
 #define _chmod chmod
+#include <process.h>
 #endif
 
 #include <sstream>
 
-Launcher::Job_Command::Job_Command() {_job_type = "command";}
+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() {}
 
@@ -57,14 +63,28 @@ Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string l
   // File name
   std::string launch_date_port_file = launch_date;
   std::ostringstream str_pid;
+#ifdef WIN32
+  str_pid << _getpid();
+#else
   str_pid << ::getpid();
-  std::string launch_script = Kernel_Utils::GetTmpDir() + "runCommand_" + _job_file_name + "_" + launch_date + "-" + str_pid.str() + ".sh";
+#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
+#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 != "")
@@ -72,7 +92,22 @@ Launcher::Job_Command::buildCommandScript(Batch::Parametre params, std::string l
     std::string::size_type last = _env_file.find_last_of("/");
     launch_script_stream << ". ./" << _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;
+#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;
+  }
+#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();
@@ -81,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