DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_dump.cxx File Reference
#include <stdlib.h>
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include "conffwk/Configuration.hpp"
#include "conffwk/ConfigObject.hpp"
#include "conffwk/Schema.hpp"
Include dependency graph for config_dump.cxx:

Go to the source code of this file.

Classes

struct  SortByName
 

Functions

 ERS_DECLARE_ISSUE (config_dump, BadCommandLine, "bad command line: "<< reason,((const char *) reason)) ERS_DECLARE_ISSUE(config_dump
 
static void print_referenced_by (const ConfigObject &obj, const char *prefix)
 
static void print_versions (const std::vector< dunedaq::conffwk::Version > &versions)
 
int main (int argc, char *argv[])
 

Variables

 ConfigException
 
caught dunedaq::conffwk::Exception exception
 

Function Documentation

◆ ERS_DECLARE_ISSUE()

ERS_DECLARE_ISSUE ( config_dump ,
BadCommandLine ,
"bad command line: "<< reason,
((const char *) reason)  )

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 76 of file config_dump.cxx.

77{
78 const std::string any("*");
79 std::string db_name, class_name, object_id, since, until;
80
81 bool changes = false;
82
83 bool skip_irrelevant = true;
85
86 bool direct_info = false;
87 bool objects_details = false;
88 bool contained_in = false;
89 bool referenced_by = false;
90
91 boost::program_options::options_description desc(
92 "Dumps class and objects descriptions using abstract conffwk API.\n"
93 "Without -c or -o options, the utility lists all classes.\n"
94 "\n"
95 "Options/Arguments");
96
97 try
98 {
99 std::vector<std::string> vesrions_str;
100 std::string class_name2;
101
102 desc.add_options()
103 (
104 "database,d",
105 boost::program_options::value<std::string>(&db_name)->required(),
106 "database specification in format plugin-name:parameters"
107 )
108 (
109 "changes,a",
110 "print details of new repository versions or modified files"
111 )
112 (
113 "versions,v",
114 boost::program_options::value<std::vector<std::string>>(&vesrions_str)->multitoken()->zero_tokens(),
115 "print details of versions from archive providing 4 parameters \"all|skip\" \"date|id|tag\" \"since\" \"until\""
116 )
117 (
118 "class-direct-info,c",
119 boost::program_options::value<std::string>(&class_name)->default_value("")->implicit_value(any),
120 "print direct properties of all classes, or given class if name is provided"
121 )
122 (
123 "class-all-info,C",
124 boost::program_options::value<std::string>(&class_name2)->default_value("")->implicit_value(any),
125 "similar to -c, but prints all properties of class (all attributes, all superclasses, etc.)"
126 )
127 (
128 "list-objects,o",
129 "list objects of class"
130 )
131 (
132 "print-referenced-by,r",
133 "print objects referencing given object (only with -o option)"
134 )
135 (
136 "dump-objects,O",
137 boost::program_options::value<std::string>(&object_id)->default_value("")->implicit_value(any),
138 "dump all objects of class or details of given object, if id is provided (-c is required)"
139 )
140 (
141 "show-contained-in,n",
142 "when dump an object, print out the database file it belongs to"
143 )
144 (
145 "help,h",
146 "print help message"
147 );
148
149 boost::program_options::variables_map vm;
150 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
151
152 if (vm.count("help"))
153 {
154 std::cout << desc << std::endl;
155 return EXIT_SUCCESS;
156 }
157
158 boost::program_options::notify(vm);
159
160 if (!class_name.empty())
161 direct_info = true;
162
163 if (!class_name2.empty())
164 {
165 if (!class_name.empty())
166 throw std::runtime_error("cannot use -c and -C options simultaneously");
167 else
168 class_name = std::move(class_name2);
169 }
170
171 if (vm.count("changes"))
172 changes = true;
173
174 if (!vesrions_str.empty())
175 {
176 if (vesrions_str.size() != 4)
177 {
178 throw std::runtime_error("-v option must have 4 parameters, see help");
179 }
180 else
181 {
182 if (vesrions_str[0] == "all")
183 skip_irrelevant = false;
184 else if (vesrions_str[0] != "skip")
185 throw std::runtime_error("first parameter of -v has to be \"all\" or \"skip\"");
186
187 if (vesrions_str[1] == "date")
189 else if (vesrions_str[1] == "id")
191 else if (vesrions_str[1] != "tag")
192 throw std::runtime_error("second versions parameter must be \"date\", \"id\" or \"tag\"");
193
194 since = vesrions_str[2];
195 until = vesrions_str[3];
196 }
197 }
198
199 if (!object_id.empty())
200 objects_details = true;
201
202 if (vm.count("list-objects"))
203 {
204 objects_details = false;
205 object_id = any;
206 }
207
208 if (vm.count("print-referenced-by"))
209 referenced_by = true;
210
211 if (vm.count("show-contained-in"))
212 contained_in = true;
213
214 if (class_name.empty() && !object_id.empty() && object_id != any)
215 throw std::runtime_error("object id is set, but no class name given (use -c option)");
216 }
217 catch (std::exception &ex)
218 {
219 ers::fatal(config_dump::BadCommandLine(ERS_HERE, ex.what()));
220 return EXIT_FAILURE;
221 }
222
223
224 try
225 {
226 Configuration conf(db_name);
227
228 // get versions if any
229 if (changes)
230 {
231 std::cout << "Changes:\n";
232 print_versions(conf.get_changes());
233 return EXIT_SUCCESS;
234 }
235
236 if (!since.empty())
237 {
238 std::cout << "Versions:\n";
239 print_versions(conf.get_versions(since, until, query_type, skip_irrelevant));
240 return EXIT_SUCCESS;
241 }
242
243 std::set<std::string> classes;
244
245 if (!class_name.empty() && class_name != any)
246 classes.insert(class_name);
247 else
248 for (const auto &i : conf.superclasses())
249 classes.insert(*i.first);
250
251 // if there are no options, just list classes
252 if (class_name.empty() && object_id.empty())
253 {
254 std::cout << "The database schema has " << classes.size() << " class(es):\n";
255 for (const auto &i : classes)
256 std::cout << " - \'" << i << "\'\n";
257 return EXIT_SUCCESS;
258 }
259
260 // only print details of classes
261 if (!class_name.empty() && object_id.empty())
262 {
263 if (class_name == any)
264 std::cout << "The database schema has " << classes.size() << " class(es):\n";
265
266 for (const auto &i : classes)
267 conf.get_class_info(i, direct_info).print(std::cout, " ");
268
269 return EXIT_SUCCESS;
270 }
271
272 const char *prefix = "";
273 const char *prefix2 = " ";
274 const char *prefix3 = " ";
275
276 // list or print all objects of class(es)
277 if (object_id == any)
278 {
279 if (class_name.empty() || class_name == any)
280 {
281 std::cout << "The database schema has " << classes.size() << " class(es):\n";
282 prefix = " ";
283 prefix2 = " ";
284 prefix3 = " ";
285 }
286
287 for (std::set<std::string>::const_iterator i = classes.begin(); i != classes.end(); ++i)
288 {
289 std::vector<ConfigObject> objects;
290 conf.get(*i, objects);
291 if (objects.empty())
292 {
293 std::cout << prefix << "The class \'" << *i << "\' has no objects\n";
294 }
295 else
296 {
297 std::cout << prefix << "The class \'" << *i << "\' has " << objects.size() << " object(s) including sub-classes:\n";
298
299 // sort by ID for consistent output
300 std::set<const ConfigObject*, SortByName> sorted_by_id;
301
302 for (const auto &j : objects)
303 sorted_by_id.insert(&j);
304
305 for (const auto &j : sorted_by_id)
306 if (j->class_name() == *i)
307 {
308 if (objects_details)
309 j->print_ref(std::cout, conf, prefix2, contained_in);
310 else
311 std::cout << prefix << " - \'" << j->UID() << "\'\n";
312
313 if (referenced_by)
314 print_referenced_by(*j, prefix3);
315 }
316 else
317 {
318 std::cout << prefix << " - \'" << j->UID() << "\' (database class name = \'" << j->class_name() << "\')\n";
319 }
320 }
321 }
322 }
323 else
324 {
326 conf.get(class_name, object_id, obj);
327 obj.print_ref(std::cout, conf, "", contained_in);
328 if (referenced_by)
329 print_referenced_by(obj, prefix2);
330 }
331 }
332 catch (dunedaq::conffwk::Exception &ex)
333 {
334 ers::fatal(config_dump::ConfigException(ERS_HERE, ex));
335 return EXIT_FAILURE;
336 }
337
338 return EXIT_SUCCESS;
339}
#define ERS_HERE
Represents database objects.
Defines base class for cache of template objects.
static void print_versions(const std::vector< dunedaq::conffwk::Version > &versions)
static void print_referenced_by(const ConfigObject &obj, const char *prefix)
void fatal(const Issue &issue)
Definition ers.hpp:88

◆ print_referenced_by()

static void print_referenced_by ( const ConfigObject & obj,
const char * prefix )
static

Definition at line 37 of file config_dump.cxx.

38{
39 std::vector<ConfigObject> values;
40 obj.referenced_by(values, "*", false, 0, 0);
41 if (values.size() == 0)
42 {
43 std::cout << prefix << "is not referenced by others objects\n";
44 }
45 else
46 {
47 std::cout << prefix << "is referenced by " << values.size() << " object" << (values.size() == 1 ? "" : "s") << ":\n";
48 for (const auto &iobj : values)
49 std::cout << prefix << " * " << iobj << std::endl;
50
51 }
52}

◆ print_versions()

static void print_versions ( const std::vector< dunedaq::conffwk::Version > & versions)
static

Definition at line 55 of file config_dump.cxx.

56{
57 const auto len = versions.size();
58 unsigned int idx = 1;
59 for (const auto& x : versions)
60 {
61 char buf[50];
62 std::time_t t(x.get_timestamp());
63 std::strftime(buf, 50, "%F %T %Z", std::localtime(&t));
64 std::cout << " * version [" << idx++ << '/' << len << "]\n"
65 " id: " << x.get_id() << "\n"
66 " user: " << x.get_user() << "\n"
67 " date: " << buf << "\n"
68 " comment: " << x.get_comment() << "\n"
69 " files:\n";
70
71 for (const auto& f : x.get_files())
72 std::cout << " - \"" << f << "\"\n";
73 }
74}

Variable Documentation

◆ ConfigException

ConfigException

Definition at line 23 of file config_dump.cxx.

◆ exception

caught dunedaq::conffwk::Exception exception

Definition at line 24 of file config_dump.cxx.