CMS 3D CMS Logo

Utilities.cc
Go to the documentation of this file.
5 
6 //local includes
11 #include <boost/foreach.hpp>
12 #include <fstream>
13 #include <iostream>
14 
16 
17 #include <termios.h>
18 #include <unistd.h>
19 #include <cstdio>
20 
21 namespace cond {
22 
23  int getch() {
24  int ch;
25  struct termios t_old, t_new;
26 
27  tcgetattr(STDIN_FILENO, &t_old);
28  t_new = t_old;
29  t_new.c_lflag &= ~(ICANON | ECHO);
30  tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
31 
32  ch = getchar();
33 
34  tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
35  return ch;
36  }
37 
38  std::string getpass(const std::string& prompt, bool show_asterisk){
39  const char BACKSPACE=127;
40  const char RETURN=10;
41 
42  std::string password;
43  unsigned char ch=0;
44 
45  std::cout <<prompt;
46 
47  while((ch=getch())!=RETURN){
48  if(ch==BACKSPACE){
49  if(password.length()!=0){
50  if(show_asterisk) std::cout <<"\b \b";
51  password.resize(password.length()-1);
52  }
53  } else {
54  password+=ch;
55  if(show_asterisk) std::cout <<'*';
56  }
57  }
58  std::cout <<std::endl;
59  return password;
60  }
61 
63  std::string prompt("Enter password for user ");
64  prompt += userName;
65  prompt += ": ";
66  return getpass(prompt,true );
67  }
68 
69 }
70 
72 }
74 
76  std::string positionalParameter):m_name(commandName),
77  m_options(std::string("Usage: ")+m_name+
78  std::string(" [options] ")+positionalParameter
79  +std::string(" \n")),
80  m_positionalOptions(),
81  m_values(){
82  m_options.add_options()
83  ("debug","switch on debug mode")
84  ("help,h", "help message")
85  ;
86  if(!positionalParameter.empty()) {
87  m_positionalOptions.add( positionalParameter.c_str(), -1);
88  addOption<std::string>(positionalParameter,"",positionalParameter);
89  }
90 }
91 
92 
94 }
95 
97  return 0;
98 }
99 
100 int cond::Utilities::run( int argc, char** argv ){
103 
104  std::vector<edm::ParameterSet> psets;
105  edm::ParameterSet pSet;
106  pSet.addParameter("@service_type",std::string("SiteLocalConfigService"));
107  psets.push_back(pSet);
109  m_currentToken = &servToken;
111 
112  int ret = 0;
113  try{
114  parseCommand( argc, argv );
115  if (m_values.count("help")) {
116  std::cout << m_options <<std::endl;;
117  return 0;
118  }
119  ret = execute();
120  }catch( cond::Exception& err ){
121  std::cout << err.what() << std::endl;
122  ret = 1;
123  }catch( const std::exception& exc ){
124  std::cout << exc.what() << std::endl;
125  ret = 1;
126  }
127  m_currentToken = nullptr;
128  return ret;
129 }
130 
131 void
132 cond::Utilities::addConnectOption(const std::string& connectionOptionName,
133  const std::string& shortName,
134  const std::string& helpEntry ){
135  addOption<std::string>(connectionOptionName,shortName,helpEntry);
136 }
137 
138 void
140  addOption<std::string>("authPath","P","path to the authentication key");
141  addOption<std::string>("user","u","user name");
142  addOption<std::string>("pass","p","password");
143 }
144 
145 void
147  addOption<std::string>("configFile","f","configuration file(optional)");
148 }
149 
151  boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(m_options).positional(m_positionalOptions).run(), m_values);
152  if(m_options.find_nothrow("configFile",false)){
153  std::string configFileName = getValueIfExists("configFile");
154  if (! configFileName.empty()){
155  std::fstream configFile;
156  configFile.open(configFileName.c_str(), std::fstream::in);
157  boost::program_options::store(boost::program_options::parse_config_file(configFile,m_options), m_values);
158  configFile.close();
159  }
160  }
161  boost::program_options::notify(m_values);
162 }
163 
165  return getOptionValue<std::string>("authPath");
166 }
167 
169  return getOptionValue<std::string>("user");
170 }
171 
173  return getOptionValue<std::string>("pass");
174 }
175 
177  return getOptionValue<std::string>("connect");
178 }
179 
181  return getOptionValue<std::string>("logDB");
182 }
183 
185  return getOptionValue<std::string>("dictionary");
186 }
187 
189  return getOptionValue<std::string>("configFile");
190 }
191 
192 
194  const void* found = m_options.find_nothrow(fullName, false);
195  if(!found){
196  std::stringstream message;
197  message << "Utilities::hasOptionValue: option \"" << fullName << "\" is not known by the command.";
198  sendException(message.str());
199  }
200  return m_values.count(fullName);
201 }
202 
204  return m_values.count("debug");
205 }
206 
208  // dummy, to avoid to adapt non-CondCore clients
209 }
210 
212  std::string val("");
213  if(m_values.count(fullName)){
214  val = m_values[fullName].as<std::string>();
215  }
216  return val;
217 }
218 
220  throw cond::UtilitiesError(message);
221 }
222 
224  throw cond::Exception(message);
225 }
226 
persistency::Exception Exception
Definition: Exception.h:25
std::string getUserValue()
Definition: Utilities.cc:168
void parseCommand(int argc, char **argv)
Definition: Utilities.cc:150
int run(int argc, char **argv)
Definition: Utilities.cc:100
Base exception class for the object to relational access.
Definition: Exception.h:11
int getch()
Definition: Utilities.cc:23
static PluginManager & configure(const Config &)
std::string getValueIfExists(const std::string &fullName)
Definition: Utilities.cc:211
UtilitiesError(const std::string &message)
Definition: Utilities.cc:71
char const * what() const override
Definition: Exception.cc:103
virtual ~Utilities()
Definition: Utilities.cc:93
std::string getPasswordValue()
Definition: Utilities.cc:172
edm::ServiceToken * m_currentToken
Definition: Utilities.h:69
std::string getDictionaryValue()
Definition: Utilities.cc:184
std::string getConfigFileValue()
Definition: Utilities.cc:188
void addConnectOption(std::string const &fullName, std::string const &shortName, std::string const &helpEntry)
Definition: Utilities.cc:132
config
Definition: looper.py:291
std::string getAuthenticationPathValue()
Definition: Utilities.cc:164
PluginManager::Config config()
Definition: standard.cc:21
std::string getpassForUser(const std::string &userName)
Definition: Utilities.cc:62
void addAuthenticationOptions()
Definition: Utilities.cc:139
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:125
void addConfigFileOption()
Definition: Utilities.cc:146
static ServiceToken createSet(std::vector< ParameterSet > &)
std::string getConnectValue()
Definition: Utilities.cc:176
void initializePluginManager()
Definition: Utilities.cc:207
Utilities(const std::string &commandName, std::string positionalParameter=std::string(""))
Definition: Utilities.cc:75
std::string getLogDBValue()
Definition: Utilities.cc:180
boost::program_options::variables_map m_values
Definition: Utilities.h:77
bool hasOptionValue(const std::string &fullName)
Definition: Utilities.cc:193
Definition: plugin.cc:24
void sendError(const std::string &message)
Definition: Utilities.cc:219
~UtilitiesError() override
Definition: Utilities.cc:73
void sendException(const std::string &message)
Definition: Utilities.cc:223
std::string getpass(const std::string &prompt, bool show_asterisk=true)
Definition: Utilities.cc:38
boost::program_options::options_description m_options
Definition: Utilities.h:75
virtual int execute()
Definition: Utilities.cc:96
boost::program_options::positional_options_description m_positionalOptions
Definition: Utilities.h:76
def operate(timelog, memlog, json_f, num)