From 6c51e975da1bb85596a6d3c0b8aa08f43362dc4f Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Wed, 24 Jan 2018 17:26:08 +0100 Subject: [PATCH] Add command_salome job type. --- idl/SALOME_Launcher.idl | 3 + src/Launcher/CMakeLists.txt | 2 + src/Launcher/Launcher_Job_Command.cxx | 16 ++- src/Launcher/Launcher_Job_Command.hxx | 3 + src/Launcher/Launcher_Job_CommandSALOME.cxx | 50 +++++++++ src/Launcher/Launcher_Job_CommandSALOME.hxx | 48 +++++++++ src/Launcher/Launcher_Job_PythonSALOME.cxx | 6 +- src/Launcher/Launcher_Job_PythonSALOME.hxx | 1 + src/Launcher/Launcher_Job_YACSFile.cxx | 3 +- src/Launcher/Launcher_Job_YACSFile.hxx | 2 + src/Launcher/Launcher_XML_Persistence.cxx | 9 +- src/Launcher/SALOME_Launcher.cxx | 22 ++-- src/Launcher/Test/test_launcher.py | 111 ++++++++++++++++++++ 13 files changed, 259 insertions(+), 17 deletions(-) create mode 100644 src/Launcher/Launcher_Job_CommandSALOME.cxx create mode 100644 src/Launcher/Launcher_Job_CommandSALOME.hxx diff --git a/idl/SALOME_Launcher.idl b/idl/SALOME_Launcher.idl index 2506fa52e..51c5af298 100644 --- a/idl/SALOME_Launcher.idl +++ b/idl/SALOME_Launcher.idl @@ -52,6 +52,9 @@ struct JobParameters //! Type of the job. /*! There are three supported types: - "command" : execute #job_file script without %SALOME environment + - "command_salome" : execute #job_file script within %SALOME environment + (salome shell) but the %SALOME application is not + launched - "python_salome" : execute #job_file python script by %SALOME - "yacs_file" : execute #job_file by YACS module as a xml YACS schema */ diff --git a/src/Launcher/CMakeLists.txt b/src/Launcher/CMakeLists.txt index 4c8560db3..4ace6169c 100755 --- a/src/Launcher/CMakeLists.txt +++ b/src/Launcher/CMakeLists.txt @@ -69,6 +69,7 @@ SET(Launcher_SOURCES SALOME_Launcher_Handler.cxx Launcher_Job.cxx Launcher_Job_Command.cxx + Launcher_Job_CommandSALOME.cxx Launcher_Job_SALOME.cxx Launcher_Job_PythonSALOME.cxx Launcher_Job_YACSFile.cxx @@ -119,6 +120,7 @@ SET(COMMON_HEADERS_HXX Launcher.hxx Launcher_Job.hxx Launcher_Job_Command.hxx + Launcher_Job_CommandSALOME.hxx Launcher_Job_PythonSALOME.hxx Launcher_Job_SALOME.hxx Launcher_Job_YACSFile.hxx diff --git a/src/Launcher/Launcher_Job_Command.cxx b/src/Launcher/Launcher_Job_Command.cxx index 9a4fdf0f8..ed4542bd1 100644 --- a/src/Launcher/Launcher_Job_Command.cxx +++ b/src/Launcher/Launcher_Job_Command.cxx @@ -33,7 +33,12 @@ #include -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() {} @@ -72,7 +77,7 @@ 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 << std::endl; + launch_script_stream << runCommandString() << std::endl; // Return launch_script_stream.flush(); @@ -81,4 +86,11 @@ 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; + result << "./" << _job_file_name_complete; + return result.str(); +} #endif diff --git a/src/Launcher/Launcher_Job_Command.hxx b/src/Launcher/Launcher_Job_Command.hxx index 20dc95f7b..d95b01b5d 100644 --- a/src/Launcher/Launcher_Job_Command.hxx +++ b/src/Launcher/Launcher_Job_Command.hxx @@ -39,9 +39,12 @@ namespace Launcher virtual void update_job(); + static const char TYPE_NAME[]; + #ifdef WITH_LIBBATCH protected: std::string buildCommandScript(Batch::Parametre params, std::string launch_date); + virtual std::string runCommandString(); #endif }; } diff --git a/src/Launcher/Launcher_Job_CommandSALOME.cxx b/src/Launcher/Launcher_Job_CommandSALOME.cxx new file mode 100644 index 000000000..9bf021803 --- /dev/null +++ b/src/Launcher/Launcher_Job_CommandSALOME.cxx @@ -0,0 +1,50 @@ +// Copyright (C) 2009-2017 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, 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 +// 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 +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +// Author: André RIBES - EDF R&D +// +#include "Launcher_Job_CommandSALOME.hxx" + +#ifdef WITH_LIBBATCH +#include +#endif + +#include + +const char Launcher::Job_CommandSALOME::TYPE_NAME[] = "command_salome"; + +Launcher::Job_CommandSALOME::Job_CommandSALOME() +{ + _job_type = Launcher::Job_CommandSALOME::TYPE_NAME; +} + +Launcher::Job_CommandSALOME::~Job_CommandSALOME() {} + + +#ifdef WITH_LIBBATCH + +std::string Launcher::Job_CommandSALOME::runCommandString() +{ + std::ostringstream result; + result << _resource_definition.AppliPath + << "/salome shell ./" + << _job_file_name_complete; + return result.str(); +} +#endif diff --git a/src/Launcher/Launcher_Job_CommandSALOME.hxx b/src/Launcher/Launcher_Job_CommandSALOME.hxx new file mode 100644 index 000000000..dd3884bcf --- /dev/null +++ b/src/Launcher/Launcher_Job_CommandSALOME.hxx @@ -0,0 +1,48 @@ +// Copyright (C) 2009-2016 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, 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 +// 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 +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +// Author: André RIBES - EDF R&D +// +#ifndef _LAUNCHER_JOB_COMMAND_SALOME_HXX_ +#define _LAUNCHER_JOB_COMMAND_SALOME_HXX_ + +#include "Launcher_Job_Command.hxx" +#include "Launcher.hxx" + +#ifdef WITH_LIBBATCH +#include +#endif + +namespace Launcher +{ + class LAUNCHER_EXPORT Job_CommandSALOME : virtual public Launcher::Job_Command + { + public: + Job_CommandSALOME(); + virtual ~Job_CommandSALOME(); + static const char TYPE_NAME[]; + +#ifdef WITH_LIBBATCH + protected: + virtual std::string runCommandString(); +#endif + }; +} + +#endif diff --git a/src/Launcher/Launcher_Job_PythonSALOME.cxx b/src/Launcher/Launcher_Job_PythonSALOME.cxx index 1ebc0dca8..658e6c4c9 100644 --- a/src/Launcher/Launcher_Job_PythonSALOME.cxx +++ b/src/Launcher/Launcher_Job_PythonSALOME.cxx @@ -21,8 +21,12 @@ // #include "Launcher_Job_PythonSALOME.hxx" +const char Launcher::Job_PythonSALOME::TYPE_NAME[] = "python_salome"; -Launcher::Job_PythonSALOME::Job_PythonSALOME() {_job_type = "python_salome";} +Launcher::Job_PythonSALOME::Job_PythonSALOME() +{ + _job_type = Launcher::Job_PythonSALOME::TYPE_NAME; +} Launcher::Job_PythonSALOME::~Job_PythonSALOME() {} diff --git a/src/Launcher/Launcher_Job_PythonSALOME.hxx b/src/Launcher/Launcher_Job_PythonSALOME.hxx index 4a357765f..876f775db 100644 --- a/src/Launcher/Launcher_Job_PythonSALOME.hxx +++ b/src/Launcher/Launcher_Job_PythonSALOME.hxx @@ -34,6 +34,7 @@ namespace Launcher virtual void setJobFile(const std::string & job_file); virtual void addJobTypeSpecificScript(std::ofstream & launch_script_stream); + static const char TYPE_NAME[]; }; } diff --git a/src/Launcher/Launcher_Job_YACSFile.cxx b/src/Launcher/Launcher_Job_YACSFile.cxx index 388090351..79e5b9a3f 100644 --- a/src/Launcher/Launcher_Job_YACSFile.cxx +++ b/src/Launcher/Launcher_Job_YACSFile.cxx @@ -22,10 +22,11 @@ #include "Launcher_Job_YACSFile.hxx" #include +const char Launcher::Job_YACSFile::TYPE_NAME[] = "yacs_file"; Launcher::Job_YACSFile::Job_YACSFile() { - _job_type = "yacs_file"; + _job_type = Launcher::Job_YACSFile::TYPE_NAME; _dumpState = -1; _yacsDriverOptions = ""; } diff --git a/src/Launcher/Launcher_Job_YACSFile.hxx b/src/Launcher/Launcher_Job_YACSFile.hxx index 5cd2f8a1c..657231244 100644 --- a/src/Launcher/Launcher_Job_YACSFile.hxx +++ b/src/Launcher/Launcher_Job_YACSFile.hxx @@ -36,6 +36,8 @@ namespace Launcher virtual void addJobTypeSpecificScript(std::ofstream & launch_script_stream); virtual void checkSpecificParameters(); + static const char TYPE_NAME[]; + protected: int _dumpState; std::string _yacsDriverOptions; diff --git a/src/Launcher/Launcher_XML_Persistence.cxx b/src/Launcher/Launcher_XML_Persistence.cxx index 8da040423..f7db94e58 100644 --- a/src/Launcher/Launcher_XML_Persistence.cxx +++ b/src/Launcher/Launcher_XML_Persistence.cxx @@ -24,6 +24,7 @@ #include "Launcher_XML_Persistence.hxx" #include "Launcher_Job_Command.hxx" +#include "Launcher_Job_CommandSALOME.hxx" #include "Launcher_Job_YACSFile.hxx" #include "Launcher_Job_PythonSALOME.hxx" @@ -241,11 +242,13 @@ XML_Persistence::createJobFromXmlNode(xmlNodePtr job_node) string job_type = getAttrValue(job_node, "type"); if (job_type.empty()) throw LauncherException(string("Invalid job \"") + job_name + "\": type is not defined"); - if (job_type == "command") + if (job_type == Launcher::Job_Command::TYPE_NAME) new_job = new Launcher::Job_Command(); - else if (job_type == "yacs_file") + else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME) + new_job = new Launcher::Job_CommandSALOME(); + else if (job_type == Launcher::Job_YACSFile::TYPE_NAME) new_job = new Launcher::Job_YACSFile(); - else if (job_type == "python_salome") + else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME) new_job = new Launcher::Job_PythonSALOME(); else { diff --git a/src/Launcher/SALOME_Launcher.cxx b/src/Launcher/SALOME_Launcher.cxx index 8849b855e..3acc98d86 100644 --- a/src/Launcher/SALOME_Launcher.cxx +++ b/src/Launcher/SALOME_Launcher.cxx @@ -32,6 +32,7 @@ #include "Launcher_Job_Command.hxx" #include "Launcher_Job_YACSFile.hxx" #include "Launcher_Job_PythonSALOME.hxx" +#include "Launcher_Job_CommandSALOME.hxx" #include "utilities.h" @@ -95,21 +96,22 @@ SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters) { std::string job_type = job_parameters.job_type.in(); - if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome") - { - std::string message("SALOME_Launcher::createJob: bad job type: "); - message += job_type; - THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR); - } - Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it - if (job_type == "command") + if (job_type == Launcher::Job_Command::TYPE_NAME) new_job = new Launcher::Job_Command(); - else if (job_type == "yacs_file") + else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME) + new_job = new Launcher::Job_CommandSALOME(); + else if (job_type == Launcher::Job_YACSFile::TYPE_NAME) new_job = new Launcher::Job_YACSFile(); - else if (job_type == "python_salome") + else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME) new_job = new Launcher::Job_PythonSALOME(); + else + { + std::string message("SALOME_Launcher::createJob: bad job type: "); + message += job_type; + THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR); + } // Name new_job->setJobName(job_parameters.job_name.in()); diff --git a/src/Launcher/Test/test_launcher.py b/src/Launcher/Test/test_launcher.py index 991c05599..7c11e64e0 100755 --- a/src/Launcher/Test/test_launcher.py +++ b/src/Launcher/Test/test_launcher.py @@ -536,6 +536,117 @@ f.close() self.verifyFile(os.path.join(job_params.result_directory, "result.txt"), "it works!\n") + ################################# + # test of command salome job type + ################################# + def test_command_salome(self): + case_test_dir = os.path.join(TestCompo.test_dir, "command_salome") + mkdir_p(case_test_dir) + + # job script + data_file = "in.txt" + script_file = "myEnvScript.py" + script_text = """#! /usr/bin/env python +# -*- coding: utf-8 -*- + +import os,sys +# verify import salome +import salome + +text_result = os.getenv("ENV_TEST_VAR","") + +f = open('result.txt', 'w') +f.write(text_result) +f.close() + +in_f = open("in.txt", "r") +in_text = in_f.read() +in_f.close() + +os.mkdir("copie") +f = open(os.path.join("copie",'copie.txt'), 'w') +f.write(in_text) +f.close() +""" + abs_script_file = os.path.join(case_test_dir, script_file) + f = open(abs_script_file, "w") + f.write(script_text) + f.close() + os.chmod(abs_script_file, 0o755) + + #environement script + env_file = "myEnv.sh" + env_text = """export ENV_TEST_VAR="expected" +""" + f = open(os.path.join(case_test_dir, env_file), "w") + f.write(env_text) + f.close() + + # write data file + f = open(os.path.join(case_test_dir, data_file), "w") + f.write("to be copied") + f.close() + + # job params + local_result_dir = os.path.join(case_test_dir, "result_comsalome_job-") + job_params = self.create_JobParameters() + job_params.job_type = "command_salome" + job_params.job_file = script_file + job_params.env_file = env_file + job_params.in_files = [data_file] + job_params.out_files = ["result.txt", "copie"] + job_params.local_directory = case_test_dir + + # create and launch the job + launcher = salome.naming_service.Resolve('/SalomeLauncher') + resManager= salome.lcc.getResourcesManager() + + for resource in self.ressources: + print "Testing command salome job on ", resource + job_params.result_directory = local_result_dir + resource + job_params.job_name = "CommandSalomeJob_" + resource + job_params.resource_required.name = resource + + # use the working directory of the resource + resParams = resManager.GetResourceDefinition(resource) + wd = os.path.join(resParams.working_directory, + "CommandSalomeJob" + self.suffix) + job_params.work_directory = wd + + job_id = launcher.createJob(job_params) + launcher.launchJob(job_id) + # wait for the end of the job + jobState = launcher.getJobState(job_id) + print "Job %d state: %s" % (job_id,jobState) + while jobState != "FINISHED" and jobState != "FAILED" : + time.sleep(3) + jobState = launcher.getJobState(job_id) + print "Job %d state: %s" % (job_id,jobState) + pass + + # verify the results + self.assertEqual(jobState, "FINISHED") + launcher.getJobResults(job_id, "") + self.verifyFile(os.path.join(job_params.result_directory, "result.txt"), + "expected") + self.verifyFile(os.path.join(job_params.result_directory, + "copie",'copie.txt'), + "to be copied") + + # verify getJobWorkFile + mydir = os.path.join(case_test_dir, "work_dir" + resource) + success = launcher.getJobWorkFile(job_id, "result.txt", mydir) + self.assertEqual(success, True) + self.verifyFile(os.path.join(mydir, "result.txt"), "expected") + + success = launcher.getJobWorkFile(job_id, "copie", mydir) + self.assertEqual(success, True) + self.verifyFile(os.path.join(mydir, "copie", "copie.txt"), + "to be copied") + pass + pass + pass + if __name__ == '__main__': # creat study import salome -- 2.39.2