1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
/***********************************************/
/**
* @file runCommand.cpp
*
* @brief Execute system commands.
*
* @author Matthias Ellmer
* @author Torsten Mayer-Guerr
* @date 2016-07-13
*/
/***********************************************/
// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
Execute system \config{command}s. If \config{executeParallel} is set and
multiple \config{command}s are given they are executed in parallel at
distributed nodes, otherwise they are executed consecutively at master node only.
)";
/***********************************************/
#include "programs/program.h"
#include "inputOutput/system.h"
/***** CLASS ***********************************/
/** @brief Execute system commands.
* @ingroup programsGroup */
class RunCommand
{
public:
void run(Config &config, Parallel::CommunicatorPtr comm);
};
GROOPS_REGISTER_PROGRAM(RunCommand, PARALLEL, "Execute system commands", System)
/***********************************************/
void RunCommand::run(Config &config, Parallel::CommunicatorPtr comm)
{
try
{
std::vector<FileName> command;
Bool silently;
Bool continueAfterError;
Bool executeParallel;
readConfig(config, "command", command, Config::MUSTSET, "", "");
readConfig(config, "silently", silently, Config::DEFAULT, "0", "without showing the output.");
readConfig(config, "continueAfterError", continueAfterError, Config::DEFAULT, "0", "continue with next command after error, otherwise throw exception");
readConfig(config, "executeParallel", executeParallel, Config::DEFAULT, "0", "execute several commands in parallel");
if(isCreateSchema(config)) return;
// lambda function
// ---------------
auto run = [&](UInt i)
{
logStatus<<"Run command: \""<<command.at(i)<<"\""<<Log::endl;
std::vector<std::string> outputs;
if(!System::exec(command.at(i), outputs))
{
if(continueAfterError)
logWarning<<"Command \""<<command.at(i)<<"\" exited with error"<<Log::endl;
else
throw(Exception("Command \""+command.at(i).str()+"\" exited with error"));
}
if(!silently)
for(const auto &output : outputs)
logInfo<<output<<Log::endl;
};
// ---------------
if(executeParallel)
{
Log::GroupPtr groupPtr = Log::group(TRUE, FALSE); // group is freed in the destructor
Parallel::forEach(command.size(), run, comm, FALSE);
}
else if(Parallel::isMaster(comm))
for(UInt i=0; i<command.size(); i++)
run(i);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
|