Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:55

0001 #include "FWCore/PluginManager/interface/PluginManager.h"
0002 #include "FWCore/PluginManager/interface/standard.h"
0003 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 
0006 //local includes
0007 #include "CondCore/Utilities/interface/Utilities.h"
0008 #include <fstream>
0009 #include <iostream>
0010 
0011 #include "CondCore/CondDB/interface/Auth.h"
0012 
0013 #include <termios.h>
0014 #include <unistd.h>
0015 #include <cstdio>
0016 
0017 namespace cond {
0018 
0019   int getch() {
0020     int ch;
0021     struct termios t_old, t_new;
0022 
0023     tcgetattr(STDIN_FILENO, &t_old);
0024     t_new = t_old;
0025     t_new.c_lflag &= ~(ICANON | ECHO);
0026     tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
0027 
0028     ch = getchar();
0029 
0030     tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
0031     return ch;
0032   }
0033 
0034   std::string getpass(const std::string& prompt, bool show_asterisk) {
0035     const char BACKSPACE = 127;
0036     const char RETURN = 10;
0037 
0038     std::string password;
0039     unsigned char ch = 0;
0040 
0041     std::cout << prompt;
0042 
0043     while ((ch = getch()) != RETURN) {
0044       if (ch == BACKSPACE) {
0045         if (password.length() != 0) {
0046           if (show_asterisk)
0047             std::cout << "\b \b";
0048           password.resize(password.length() - 1);
0049         }
0050       } else {
0051         password += ch;
0052         if (show_asterisk)
0053           std::cout << '*';
0054       }
0055     }
0056     std::cout << std::endl;
0057     return password;
0058   }
0059 
0060   std::string getpassForUser(const std::string& userName) {
0061     std::string prompt("Enter password for user ");
0062     prompt += userName;
0063     prompt += ": ";
0064     return getpass(prompt, true);
0065   }
0066 
0067 }  // namespace cond
0068 
0069 cond::UtilitiesError::UtilitiesError(const std::string& message) : Exception(message) {}
0070 cond::UtilitiesError::~UtilitiesError() throw() {}
0071 
0072 cond::Utilities::Utilities(const std::string& commandName, std::string positionalParameter)
0073     : m_name(commandName),
0074       m_options(std::string("Usage: ") + m_name + std::string(" [options] ") + positionalParameter +
0075                 std::string(" \n")),
0076       m_positionalOptions(),
0077       m_values() {
0078   m_options.add_options()("debug", "switch on debug mode")("help,h", "help message");
0079   if (!positionalParameter.empty()) {
0080     m_positionalOptions.add(positionalParameter.c_str(), -1);
0081     addOption<std::string>(positionalParameter, "", positionalParameter);
0082   }
0083 }
0084 
0085 cond::Utilities::~Utilities() {}
0086 
0087 int cond::Utilities::execute() { return 0; }
0088 
0089 int cond::Utilities::run(int argc, char** argv) {
0090   edmplugin::PluginManager::Config config;
0091   edmplugin::PluginManager::configure(edmplugin::standard::config());
0092 
0093   std::vector<edm::ParameterSet> psets;
0094   edm::ParameterSet pSet;
0095   pSet.addParameter("@service_type", std::string("SiteLocalConfigService"));
0096   psets.push_back(pSet);
0097   edm::ServiceToken servToken(edm::ServiceRegistry::createSet(psets));
0098   m_currentToken = &servToken;
0099   edm::ServiceRegistry::Operate operate(servToken);
0100 
0101   int ret = 0;
0102   try {
0103     parseCommand(argc, argv);
0104     if (m_values.count("help")) {
0105       std::cout << m_options << std::endl;
0106       ;
0107       return 0;
0108     }
0109     ret = execute();
0110   } catch (cond::Exception& err) {
0111     std::cout << err.what() << std::endl;
0112     ret = 1;
0113   } catch (const std::exception& exc) {
0114     std::cout << exc.what() << std::endl;
0115     ret = 1;
0116   }
0117   m_currentToken = nullptr;
0118   return ret;
0119 }
0120 
0121 void cond::Utilities::addConnectOption(const std::string& connectionOptionName,
0122                                        const std::string& shortName,
0123                                        const std::string& helpEntry) {
0124   addOption<std::string>(connectionOptionName, shortName, helpEntry);
0125 }
0126 
0127 void cond::Utilities::addAuthenticationOptions() {
0128   addOption<std::string>("authPath", "P", "path to the authentication key");
0129   addOption<std::string>("user", "u", "user name");
0130   addOption<std::string>("pass", "p", "password");
0131 }
0132 
0133 void cond::Utilities::addConfigFileOption() {
0134   addOption<std::string>("configFile", "f", "configuration file(optional)");
0135 }
0136 
0137 void cond::Utilities::parseCommand(int argc, char** argv) {
0138   boost::program_options::store(
0139       boost::program_options::command_line_parser(argc, argv).options(m_options).positional(m_positionalOptions).run(),
0140       m_values);
0141   if (m_options.find_nothrow("configFile", false)) {
0142     std::string configFileName = getValueIfExists("configFile");
0143     if (!configFileName.empty()) {
0144       std::fstream configFile;
0145       configFile.open(configFileName.c_str(), std::fstream::in);
0146       boost::program_options::store(boost::program_options::parse_config_file(configFile, m_options), m_values);
0147       configFile.close();
0148     }
0149   }
0150   boost::program_options::notify(m_values);
0151 }
0152 
0153 std::string cond::Utilities::getAuthenticationPathValue() { return getOptionValue<std::string>("authPath"); }
0154 
0155 std::string cond::Utilities::getUserValue() { return getOptionValue<std::string>("user"); }
0156 
0157 std::string cond::Utilities::getPasswordValue() { return getOptionValue<std::string>("pass"); }
0158 
0159 std::string cond::Utilities::getConnectValue() { return getOptionValue<std::string>("connect"); }
0160 
0161 std::string cond::Utilities::getLogDBValue() { return getOptionValue<std::string>("logDB"); }
0162 
0163 std::string cond::Utilities::getDictionaryValue() { return getOptionValue<std::string>("dictionary"); }
0164 
0165 std::string cond::Utilities::getConfigFileValue() { return getOptionValue<std::string>("configFile"); }
0166 
0167 bool cond::Utilities::hasOptionValue(const std::string& fullName) {
0168   const void* found = m_options.find_nothrow(fullName, false);
0169   if (!found) {
0170     std::stringstream message;
0171     message << "Utilities::hasOptionValue: option \"" << fullName << "\" is not known by the command.";
0172     sendException(message.str());
0173   }
0174   return m_values.count(fullName);
0175 }
0176 
0177 bool cond::Utilities::hasDebug() { return m_values.count("debug"); }
0178 
0179 void cond::Utilities::initializePluginManager() {
0180   // dummy, to avoid to adapt non-CondCore clients
0181 }
0182 
0183 std::string cond::Utilities::getValueIfExists(const std::string& fullName) {
0184   std::string val("");
0185   if (m_values.count(fullName)) {
0186     val = m_values[fullName].as<std::string>();
0187   }
0188   return val;
0189 }
0190 
0191 void cond::Utilities::sendError(const std::string& message) { throw cond::UtilitiesError(message); }
0192 
0193 void cond::Utilities::sendException(const std::string& message) { throw cond::Exception(message); }