Salome HOME
Porting SALOME KERNEL to CMake
[modules/kernel.git] / src / Launcher / Launcher_Job_SALOME.cxx
index d11057b5105f3d3f79e77afecf71503033b92541..b0c70f5b575aa7193047a980d0130976cfe44390 100644 (file)
@@ -1,26 +1,35 @@
-//  Copyright (C) 2009-2010  CEA/DEN, EDF R&D
+// Copyright (C) 2009-2012  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.
+// 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.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 // Author: AndrĂ© RIBES - EDF R&D
 //
 #include "Launcher_Job_SALOME.hxx"
 
+#ifdef WITH_LIBBATCH
+#include <Batch/Batch_Constants.hxx>
+#endif
+
+#ifdef WNT
+#include <io.h>
+#define _chmod chmod
+#endif
+
 Launcher::Job_SALOME::Job_SALOME() {}
 
 Launcher::Job_SALOME::~Job_SALOME() {}
@@ -43,6 +52,7 @@ Launcher::Job_SALOME::update_job()
 #ifdef WITH_LIBBATCH
   Batch::Parametre params = common_job_params();
   params[Batch::EXECUTABLE] = buildSalomeScript(params);
+  params[Batch::EXCLUSIVE] = true;
   _batch_job->setParametre(params);
 #endif
 }
@@ -54,28 +64,29 @@ Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
   // parameters
   std::string work_directory = params[Batch::WORKDIR].str();
 
-  std::string launch_date_port_file = _launch_date;
   std::string launch_script = "/tmp/runSalome_" + _job_file_name + "_" + _launch_date + ".sh";
   std::ofstream launch_script_stream;
-  launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
+  launch_script_stream.open(launch_script.c_str(), 
+                            std::ofstream::out
+#ifdef WIN32           
+ | std::ofstream::binary   //rnv: to avoid CL+RF end of line on windows
+#endif
+                           );
    
   // Begin of script
-  launch_script_stream << "#! /bin/bash -f" << std::endl;
+  launch_script_stream << "#!/bin/sh -f" << std::endl;
   launch_script_stream << "cd " << work_directory << 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;
   }
   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
 
   // -- Generates Catalog Resources
-  std::string resource_protocol = "ssh";
-  if (_resource_definition.ClusterInternalProtocol == rsh)
-    resource_protocol = "rsh";
-  
+  std::string resource_protocol = ParserResourcesType::protocolToString(_resource_definition.ClusterInternalProtocol);
   launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
   launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
   launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
@@ -95,20 +106,26 @@ Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
   launch_script_stream << "echo '</resources>' >> $CATALOG_FILE" << std::endl;
   launch_script_stream << "fi" << std::endl;
 
+  // Create file for ns-port-log
+  launch_script_stream << "NS_PORT_FILE_PATH=`mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX` &&\n";
+  launch_script_stream << "NS_PORT_FILE_NAME=`basename $NS_PORT_FILE_PATH` &&\n";
+
   // Launch SALOME with an appli
-  launch_script_stream << _resource_definition.AppliPath << "/runAppli --terminal  --ns-port-log=" << launch_date_port_file <<  " > logs/salome_" << _launch_date << ".log 2>&1" << std::endl;
-  launch_script_stream << "current=0\n"
-                      << "stop=20\n" 
-                      << "while ! test -f " << _resource_definition.AppliPath << "/" << launch_date_port_file << "\n"
-                      << "do\n"
-                      << "  sleep 2\n"
-                      << "  let current=current+1\n"
-                      << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
-                      << "    echo Error Naming Service failed ! >&2\n"
-                      << "    exit\n"
-                      << "  fi\n"
-                      << "done\n"
-                      << "appli_port=`cat " << _resource_definition.AppliPath << "/" << launch_date_port_file << "`\n";
+  launch_script_stream << _resource_definition.AppliPath << "/runAppli --terminal --ns-port-log=$NS_PORT_FILE_NAME --server-launch-mode=fork ";
+  launch_script_stream << "> logs/salome_" << _launch_date << ".log 2>&1 &&" << std::endl;
+  launch_script_stream << "current=0 &&\n"
+                       << "stop=20 &&\n"
+                       << "while ! test -s $NS_PORT_FILE_PATH\n"
+                       << "do\n"
+                       << "  sleep 2\n"
+                       << "  current=$((current+1))\n"
+                       << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
+                       << "    echo Error Naming Service failed ! >&2\n"
+                       << "    exit\n"
+                       << "  fi\n"
+                       << "done &&\n"
+                       << "appli_port=`cat $NS_PORT_FILE_PATH` &&\n"
+                       << "rm $NS_PORT_FILE_PATH &&\n";
 
   // Call real job type
   addJobTypeSpecificScript(launch_script_stream);