From: Margarita KARPUNINA Date: Thu, 22 Dec 2022 12:22:38 +0000 (+0300) Subject: [bos #32518][EDF] (2022-T3) X-Git-Tag: V2_5_0~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Femc2p%2FBR_V1_4_0;p=tools%2Flibbatch.git [bos #32518][EDF] (2022-T3) Parametrize commands in LIBBATCH Apply remarks according to code review --- diff --git a/src/Core/CommandsOverloader.cxx b/src/Core/CommandsOverloader.cxx index 06f7057..56e00dd 100644 --- a/src/Core/CommandsOverloader.cxx +++ b/src/Core/CommandsOverloader.cxx @@ -41,7 +41,8 @@ using namespace std; -namespace Batch { +namespace Batch +{ static set supportedCmds({"RM", "SH", "CP", "MKDIR", "RSH", "RCP", "SSH", "SCP", "RSYNC"}); @@ -50,9 +51,8 @@ namespace Batch { */ CommandsOverloader::CommandsOverloader() { - bool isFileDefined; - isCmdFileDefined(isFileDefined); - if (!isFileDefined && isCheckCmdsDefined()) { + if (!isCmdFileDefined() && isCheckCmdsDefined()) + { cout << "================================================================" << endl; cout << "Commands found on the system at compile time are used." << endl; cout << "================================================================" << endl; @@ -69,7 +69,8 @@ namespace Batch { /*! * Return the CommandsOverloader singleton. */ - CommandsOverloader& CommandsOverloader::getInstance () { + CommandsOverloader& CommandsOverloader::getInstance () + { static CommandsOverloader instance; return instance; } @@ -77,172 +78,190 @@ namespace Batch { /*! * Return an overridden definition of RM command. */ - string CommandsOverloader::RM_Command() { + string CommandsOverloader::RM_Command() + { string cmd = CMD_Command("RM"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef RM_COMMAND - return RM_COMMAND; + return RM_COMMAND; #else - throw RunTimeException("Can't use SH protocol (RM command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SH protocol (RM command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of SH command. */ - string CommandsOverloader::SH_Command() { + string CommandsOverloader::SH_Command() + { string cmd = CMD_Command("SH"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef SH_COMMAND - return SH_COMMAND; + return SH_COMMAND; #else - throw RunTimeException("Can't use SH protocol (SH command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SH protocol (SH command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of CP command. */ - string CommandsOverloader::CP_Command() { + string CommandsOverloader::CP_Command() + { string cmd = CMD_Command("CP"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef CP_COMMAND - return CP_COMMAND; + return CP_COMMAND; #else - throw RunTimeException("Can't use SH protocol (CP command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SH protocol (CP command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of MKDIR command. */ - string CommandsOverloader::MKDIR_Command() { + string CommandsOverloader::MKDIR_Command() + { string cmd = CMD_Command("MKDIR"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef MKDIR_COMMAND - return MKDIR_COMMAND; + return MKDIR_COMMAND; #else - throw RunTimeException("Can't use SH protocol (MKDIR command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SH protocol (MKDIR command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of RSH command. */ - string CommandsOverloader::RSH_Command() { + string CommandsOverloader::RSH_Command() + { string cmd = CMD_Command("RSH"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef RSH_COMMAND - return RSH_COMMAND; + return RSH_COMMAND; #else - throw RunTimeException("Can't use RSH protocol (RSH command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use RSH protocol (RSH command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of RCP command. */ - string CommandsOverloader::RCP_Command() { + string CommandsOverloader::RCP_Command() + { string cmd = CMD_Command("RCP"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef RCP_COMMAND - return RCP_COMMAND; + return RCP_COMMAND; #else - throw RunTimeException("Can't use RSH protocol (RCP command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use RSH protocol (RCP command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of SSH command. */ - string CommandsOverloader::SSH_Command() { + string CommandsOverloader::SSH_Command() + { string cmd = CMD_Command("SSH"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef SSH_COMMAND - return SSH_COMMAND; + return SSH_COMMAND; #else - throw RunTimeException("Can't use SSH protocol (SSH command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SSH protocol (SSH command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of SCP command. */ - string CommandsOverloader::SCP_Command() { + string CommandsOverloader::SCP_Command() + { string cmd = CMD_Command("SCP"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef SCP_COMMAND - return SCP_COMMAND; + return SCP_COMMAND; #else - throw RunTimeException("Can't use SSH protocol (SCP command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use SSH protocol (SCP command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! * Return an overridden definition of RSYNC command. */ - string CommandsOverloader::RSYNC_Command() { + string CommandsOverloader::RSYNC_Command() + { string cmd = CMD_Command("RSYNC"); - if (cmd.empty()) { + if (!cmd.empty()) + { + return cmd; + } + #ifdef RSYNC_COMMAND - return RSYNC_COMMAND; + return RSYNC_COMMAND; #else - throw RunTimeException("Can't use RSYNC protocol (RSYNC command was " - "not found neither in the file pointed by " - "LIBBATCH_OVERRIDE_CMDS environment variable " - "nor on the system at compile time)."); + throw RunTimeException("Can't use RSYNC protocol (RSYNC command was " + "not found neither in the file pointed by " + "LIBBATCH_OVERRIDE_CMDS environment variable " + "nor on the system at compile time)."); #endif - } else { - return cmd; - } } /*! @@ -250,9 +269,11 @@ namespace Batch { * - . * \param theFilename the name of text file to be parsed */ - void CommandsOverloader::parse(const std::string & theFilename) { + void CommandsOverloader::parse(const std::string & theFilename) + { ifstream fileStream(theFilename.c_str()); - if (!fileStream) { + if (!fileStream) + { stringstream errMsg; errMsg << "Can't open file with overridden commands definitions " << theFilename; throw RunTimeException(errMsg.str()); @@ -260,25 +281,30 @@ namespace Batch { string line; bool empty = true; int lineNumber = 1; - while (getline(fileStream, line)) { + while (getline(fileStream, line)) + { string str = line; - if (!str.empty()) { + if (!str.empty()) + { empty = false; // Find ' ' symbol and split the line size_t pos = str.find_first_of(' '); - if (pos == string::npos) { + if (pos == string::npos) + { stringstream errMsg; errMsg << "Wrong format of " << theFilename << " file on line " << lineNumber << ": Syntax error (missing ' ' character between key and value): " << line; throw RunTimeException(errMsg.str()); - } else { + } else + { string key = str.substr(0, pos); string value = str.substr(pos+1); // Check non-completeness of the file. string trimmedKey = trim(key); string trimmedValue = trim(value); - if (trimmedKey.empty() || trimmedValue.empty()) { + if (trimmedKey.empty() || trimmedValue.empty()) + { stringstream errMsg; errMsg << "Wrong format of " << theFilename << " file on line " << lineNumber << ": The non-completeness of the file: " << line; @@ -286,17 +312,20 @@ namespace Batch { } // Check the presence of an unsupported command. - if (supportedCmds.find(trimmedKey) == supportedCmds.end()) { + if (supportedCmds.find(trimmedKey) == supportedCmds.end()) + { stringstream errMsg; errMsg << "Wrong format of " << theFilename << " file on line " << lineNumber << ": The presence of an unsupported command: " << trimmedKey; throw RunTimeException(errMsg.str()); } - if (!hasKey(trimmedKey)) { + if (!hasKey(trimmedKey)) + { _cmdmap[trimmedKey] = trimmedValue; } - else { + else + { // Redifinition of already overloaded command is found. stringstream errMsg; errMsg << "Wrong format of " << theFilename << " file on line " << lineNumber @@ -307,7 +336,8 @@ namespace Batch { } ++lineNumber; } - if (empty) { + if (empty) + { stringstream errMsg; errMsg << "Wrong format of " << theFilename << " file: Empty file."; throw RunTimeException(errMsg.str()); @@ -320,7 +350,7 @@ namespace Batch { * \param theStr string to be stripped * \return stripped string */ - std::string CommandsOverloader::trim(const std::string & theStr) const noexcept + string CommandsOverloader::trim(const std::string & theStr) const noexcept { size_t beg = theStr.find_first_not_of(" \t"); if (beg == string::npos) beg = 0; @@ -329,16 +359,35 @@ namespace Batch { } /*! - * Check, if file with overloaded commands is defined by + * Return name of file with overloaded commands defined by * LIBBATCH_OVERRIDE_CMDS environment variable. - * \param theIsDefined in/out parameter; true, if file is defined; false - otherwise * \return the name of file with overloaded commands */ - const char * CommandsOverloader::isCmdFileDefined(bool & theIsDefined) const + string CommandsOverloader::getCmdFileName() const { - const char * filename = getenv("LIBBATCH_OVERRIDE_CMDS"); - theIsDefined = (filename != NULL && !string(filename).empty()); - return filename; + auto parseCmdFileName = []() -> std::string + { + const char* cmdFileName = getenv("LIBBATCH_OVERRIDE_CMDS"); + if (cmdFileName != NULL && !string(cmdFileName).empty()) + { + return cmdFileName; + } + return {}; + }; + + static const string cmdFileName = parseCmdFileName(); + return cmdFileName; + } + + /*! + * Check, if file with overloaded commands is defined by + * LIBBATCH_OVERRIDE_CMDS environment variable. + * \return true, if file is defined; false - otherwise + */ + bool CommandsOverloader::isCmdFileDefined() const + { + static const bool isFileDefined = !getCmdFileName().empty(); + return isFileDefined; } /*! @@ -360,29 +409,35 @@ namespace Batch { * \param theKey command name * \return overridden definition of the given command */ - string CommandsOverloader::CMD_Command(const std::string & theKey) { - if (_cmdmap.empty()) { - bool isFileDefined; - const char * filename = isCmdFileDefined(isFileDefined); - if (isFileDefined) { + string CommandsOverloader::CMD_Command(const std::string & theKey) + { + if (_cmdmap.empty()) + { + if (isCmdFileDefined()) + { // Environment variable LIBBATCH_OVERRIDE_CMDS is defined. // Parse text file pointed by LIBBATCH_OVERRIDE_CMDS environment variable. - parse(filename); + parse(getCmdFileName()); // Note: If environment variable LIBBATCH_OVERRIDE_CMDS is not defined, // use command configuration established at compile time. // Showing information about overloaded or not commands are used is disabled by default. // It can be switched ON via the environment variable: LIBBATCH_CHECK_CMDS=1 - if (isCheckCmdsDefined()) { + if (isCheckCmdsDefined()) + { cout << "================================================================" << endl; - if (_cmdmap.empty()) { + if (_cmdmap.empty()) + { cout << "Commands found on the system at compile time are used." << endl; - } else if (_cmdmap.size() == supportedCmds.size()) { + } else if (_cmdmap.size() == supportedCmds.size()) + { cout << "Overloaded commands are used." << endl; - } else { + } else + { cout << "Command(s)" << endl; map::const_iterator iter; - for (iter = _cmdmap.begin() ; iter != _cmdmap.end() ; ++iter) { + for (iter = _cmdmap.begin() ; iter != _cmdmap.end() ; ++iter) + { cout << "\t" << iter->first << endl; } cout << "is(are) overloaded." << endl; @@ -393,7 +448,7 @@ namespace Batch { } } - return (hasKey(theKey) ? _cmdmap[theKey] : string()); + return hasKey(theKey) ? _cmdmap[theKey] : string(); } /*! diff --git a/src/Core/CommandsOverloader.hxx b/src/Core/CommandsOverloader.hxx index 4a34c83..9f5daca 100644 --- a/src/Core/CommandsOverloader.hxx +++ b/src/Core/CommandsOverloader.hxx @@ -35,7 +35,8 @@ #include #include -namespace Batch { +namespace Batch +{ /*! * This class is used to parametrize commands in LIBBATCH. @@ -63,7 +64,8 @@ namespace Batch { void parse(const std::string & theFilename); std::string trim(const std::string & theStr) const noexcept; - const char * isCmdFileDefined(bool & theIsDefined) const; + std::string getCmdFileName() const; + bool isCmdFileDefined() const; bool isCheckCmdsDefined() const; std::string CMD_Command(const std::string & theKey); @@ -74,8 +76,10 @@ namespace Batch { private: // Forbid the use of copy constructor and assignment operator - CommandsOverloader(const CommandsOverloader &) {} - void operator=(const CommandsOverloader &) {} + CommandsOverloader(const CommandsOverloader &) = delete; + CommandsOverloader(const CommandsOverloader &&) = delete; + void operator=(const CommandsOverloader &) = delete; + void operator=(const CommandsOverloader &&) = delete; }; }