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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include "FWCore/PluginManager/interface/PluginManager.h"
#include "FWCore/PluginManager/interface/standard.h"
#include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
//local includes
#include "CondCore/Utilities/interface/Utilities.h"
#include <fstream>
#include <iostream>
#include "CondCore/CondDB/interface/Auth.h"
#include <termios.h>
#include <unistd.h>
#include <cstdio>
namespace cond {
int getch() {
int ch;
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return ch;
}
std::string getpass(const std::string& prompt, bool show_asterisk) {
const char BACKSPACE = 127;
const char RETURN = 10;
std::string password;
unsigned char ch = 0;
std::cout << prompt;
while ((ch = getch()) != RETURN) {
if (ch == BACKSPACE) {
if (!password.empty()) {
if (show_asterisk)
std::cout << "\b \b";
password.resize(password.length() - 1);
}
} else {
password += ch;
if (show_asterisk)
std::cout << '*';
}
}
std::cout << std::endl;
return password;
}
std::string getpassForUser(const std::string& userName) {
std::string prompt("Enter password for user ");
prompt += userName;
prompt += ": ";
return getpass(prompt, true);
}
} // namespace cond
cond::UtilitiesError::UtilitiesError(const std::string& message) : Exception(message) {}
cond::UtilitiesError::~UtilitiesError() throw() {}
cond::Utilities::Utilities(const std::string& commandName, std::string positionalParameter)
: m_name(commandName),
m_options(std::string("Usage: ") + m_name + std::string(" [options] ") + positionalParameter +
std::string(" \n")),
m_positionalOptions(),
m_values() {
m_options.add_options()("debug", "switch on debug mode")("help,h", "help message");
if (!positionalParameter.empty()) {
m_positionalOptions.add(positionalParameter.c_str(), -1);
addOption<std::string>(positionalParameter, "", positionalParameter);
}
}
cond::Utilities::~Utilities() {}
int cond::Utilities::execute() { return 0; }
int cond::Utilities::run(int argc, char** argv) {
edmplugin::PluginManager::Config config;
edmplugin::PluginManager::configure(edmplugin::standard::config());
std::vector<edm::ParameterSet> psets;
edm::ParameterSet pSet;
pSet.addParameter("@service_type", std::string("SiteLocalConfigService"));
psets.push_back(pSet);
edm::ServiceToken servToken(edm::ServiceRegistry::createSet(psets));
m_currentToken = &servToken;
edm::ServiceRegistry::Operate operate(servToken);
int ret = 0;
try {
parseCommand(argc, argv);
if (m_values.count("help")) {
std::cout << m_options << std::endl;
;
return 0;
}
ret = execute();
} catch (cond::Exception& err) {
std::cout << err.what() << std::endl;
ret = 1;
} catch (const std::exception& exc) {
std::cout << exc.what() << std::endl;
ret = 1;
}
m_currentToken = nullptr;
return ret;
}
void cond::Utilities::addConnectOption(const std::string& connectionOptionName,
const std::string& shortName,
const std::string& helpEntry) {
addOption<std::string>(connectionOptionName, shortName, helpEntry);
}
void cond::Utilities::addAuthenticationOptions() {
addOption<std::string>("authPath", "P", "path to the authentication key");
addOption<std::string>("user", "u", "user name");
addOption<std::string>("pass", "p", "password");
}
void cond::Utilities::addConfigFileOption() {
addOption<std::string>("configFile", "f", "configuration file(optional)");
}
void cond::Utilities::parseCommand(int argc, char** argv) {
boost::program_options::store(
boost::program_options::command_line_parser(argc, argv).options(m_options).positional(m_positionalOptions).run(),
m_values);
if (m_options.find_nothrow("configFile", false)) {
std::string configFileName = getValueIfExists("configFile");
if (!configFileName.empty()) {
std::fstream configFile;
configFile.open(configFileName.c_str(), std::fstream::in);
boost::program_options::store(boost::program_options::parse_config_file(configFile, m_options), m_values);
configFile.close();
}
}
boost::program_options::notify(m_values);
}
std::string cond::Utilities::getAuthenticationPathValue() { return getOptionValue<std::string>("authPath"); }
std::string cond::Utilities::getUserValue() { return getOptionValue<std::string>("user"); }
std::string cond::Utilities::getPasswordValue() { return getOptionValue<std::string>("pass"); }
std::string cond::Utilities::getConnectValue() { return getOptionValue<std::string>("connect"); }
std::string cond::Utilities::getLogDBValue() { return getOptionValue<std::string>("logDB"); }
std::string cond::Utilities::getDictionaryValue() { return getOptionValue<std::string>("dictionary"); }
std::string cond::Utilities::getConfigFileValue() { return getOptionValue<std::string>("configFile"); }
bool cond::Utilities::hasOptionValue(const std::string& fullName) {
const void* found = m_options.find_nothrow(fullName, false);
if (!found) {
std::stringstream message;
message << "Utilities::hasOptionValue: option \"" << fullName << "\" is not known by the command.";
sendException(message.str());
}
return m_values.count(fullName);
}
bool cond::Utilities::hasDebug() { return m_values.count("debug"); }
void cond::Utilities::initializePluginManager() {
// dummy, to avoid to adapt non-CondCore clients
}
std::string cond::Utilities::getValueIfExists(const std::string& fullName) {
std::string val("");
if (m_values.count(fullName)) {
val = m_values[fullName].as<std::string>();
}
return val;
}
void cond::Utilities::sendError(const std::string& message) { throw cond::UtilitiesError(message); }
void cond::Utilities::sendException(const std::string& message) { throw cond::Exception(message); }
|