DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_dump.cxx
Go to the documentation of this file.
1//
2// DUNE DAQ modification notice:
3// This file has been modified from the original ATLAS config source for the DUNE DAQ project.
4// Fork baseline commit: 67a24e731 (2022-10-27).
5// Renamed since fork: yes (from bin/config_dump.cpp to apps/config_dump.cxx).
6//
7
8#include <stdlib.h>
9
10#include <iostream>
11#include <string>
12
13#include <boost/program_options.hpp>
14
17#include "conffwk/Schema.hpp"
18
19using namespace dunedaq::conffwk;
20
22 config_dump,
23 BadCommandLine,
24 "bad command line: " << reason,
25 ((const char*)reason)
26)
27
29 config_dump,
31 "caught dunedaq::conffwk::Exception exception",
32)
33
35{
36 bool
37 operator()(const ConfigObject *o1, const ConfigObject *o2) const
38 {
39 return o1->UID() < o2->UID();
40 }
41};
42
43static void
44print_referenced_by(const ConfigObject &obj, const char *prefix)
45{
46 std::vector<ConfigObject> values;
47 obj.referenced_by(values, "*", false, 0, 0);
48 if (values.size() == 0)
49 {
50 std::cout << prefix << "is not referenced by others objects\n";
51 }
52 else
53 {
54 std::cout << prefix << "is referenced by " << values.size() << " object" << (values.size() == 1 ? "" : "s") << ":\n";
55 for (const auto &iobj : values)
56 std::cout << prefix << " * " << iobj << std::endl;
57
58 }
59}
60
61static void
62print_versions(const std::vector<dunedaq::conffwk::Version>& versions)
63{
64 const auto len = versions.size();
65 unsigned int idx = 1;
66 for (const auto& x : versions)
67 {
68 char buf[50];
69 std::time_t t(x.get_timestamp());
70 std::strftime(buf, 50, "%F %T %Z", std::localtime(&t));
71 std::cout << " * version [" << idx++ << '/' << len << "]\n"
72 " id: " << x.get_id() << "\n"
73 " user: " << x.get_user() << "\n"
74 " date: " << buf << "\n"
75 " comment: " << x.get_comment() << "\n"
76 " files:\n";
77
78 for (const auto& f : x.get_files())
79 std::cout << " - \"" << f << "\"\n";
80 }
81}
82
83int main(int argc, char *argv[])
84{
85 const std::string any("*");
86 std::string db_name, class_name, object_id, since, until;
87
88 bool changes = false;
89
90 bool skip_irrelevant = true;
92
93 bool direct_info = false;
94 bool objects_details = false;
95 bool contained_in = false;
96 bool referenced_by = false;
97
98 boost::program_options::options_description desc(
99 "Dumps class and objects descriptions using abstract conffwk API.\n"
100 "Without -c or -o options, the utility lists all classes.\n"
101 "\n"
102 "Options/Arguments");
103
104 try
105 {
106 std::vector<std::string> vesrions_str;
107 std::string class_name2;
108
109 desc.add_options()
110 (
111 "database,d",
112 boost::program_options::value<std::string>(&db_name)->required(),
113 "database specification in format plugin-name:parameters"
114 )
115 (
116 "changes,a",
117 "print details of new repository versions or modified files"
118 )
119 (
120 "versions,v",
121 boost::program_options::value<std::vector<std::string>>(&vesrions_str)->multitoken()->zero_tokens(),
122 "print details of versions from archive providing 4 parameters \"all|skip\" \"date|id|tag\" \"since\" \"until\""
123 )
124 (
125 "class-direct-info,c",
126 boost::program_options::value<std::string>(&class_name)->default_value("")->implicit_value(any),
127 "print direct properties of all classes, or given class if name is provided"
128 )
129 (
130 "class-all-info,C",
131 boost::program_options::value<std::string>(&class_name2)->default_value("")->implicit_value(any),
132 "similar to -c, but prints all properties of class (all attributes, all superclasses, etc.)"
133 )
134 (
135 "list-objects,o",
136 "list objects of class"
137 )
138 (
139 "print-referenced-by,r",
140 "print objects referencing given object (only with -o option)"
141 )
142 (
143 "dump-objects,O",
144 boost::program_options::value<std::string>(&object_id)->default_value("")->implicit_value(any),
145 "dump all objects of class or details of given object, if id is provided (-c is required)"
146 )
147 (
148 "show-contained-in,n",
149 "when dump an object, print out the database file it belongs to"
150 )
151 (
152 "help,h",
153 "print help message"
154 );
155
156 boost::program_options::variables_map vm;
157 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
158
159 if (vm.count("help"))
160 {
161 std::cout << desc << std::endl;
162 return EXIT_SUCCESS;
163 }
164
165 boost::program_options::notify(vm);
166
167 if (!class_name.empty())
168 direct_info = true;
169
170 if (!class_name2.empty())
171 {
172 if (!class_name.empty())
173 throw std::runtime_error("cannot use -c and -C options simultaneously");
174 else
175 class_name = std::move(class_name2);
176 }
177
178 if (vm.count("changes"))
179 changes = true;
180
181 if (!vesrions_str.empty())
182 {
183 if (vesrions_str.size() != 4)
184 {
185 throw std::runtime_error("-v option must have 4 parameters, see help");
186 }
187 else
188 {
189 if (vesrions_str[0] == "all")
190 skip_irrelevant = false;
191 else if (vesrions_str[0] != "skip")
192 throw std::runtime_error("first parameter of -v has to be \"all\" or \"skip\"");
193
194 if (vesrions_str[1] == "date")
196 else if (vesrions_str[1] == "id")
198 else if (vesrions_str[1] != "tag")
199 throw std::runtime_error("second versions parameter must be \"date\", \"id\" or \"tag\"");
200
201 since = vesrions_str[2];
202 until = vesrions_str[3];
203 }
204 }
205
206 if (!object_id.empty())
207 objects_details = true;
208
209 if (vm.count("list-objects"))
210 {
211 objects_details = false;
212 object_id = any;
213 }
214
215 if (vm.count("print-referenced-by"))
216 referenced_by = true;
217
218 if (vm.count("show-contained-in"))
219 contained_in = true;
220
221 if (class_name.empty() && !object_id.empty() && object_id != any)
222 throw std::runtime_error("object id is set, but no class name given (use -c option)");
223 }
224 catch (std::exception &ex)
225 {
226 ers::fatal(config_dump::BadCommandLine(ERS_HERE, ex.what()));
227 return EXIT_FAILURE;
228 }
229
230
231 try
232 {
233 Configuration conf(db_name);
234
235 // get versions if any
236 if (changes)
237 {
238 std::cout << "Changes:\n";
240 return EXIT_SUCCESS;
241 }
242
243 if (!since.empty())
244 {
245 std::cout << "Versions:\n";
246 print_versions(conf.get_versions(since, until, query_type, skip_irrelevant));
247 return EXIT_SUCCESS;
248 }
249
250 std::set<std::string> classes;
251
252 if (!class_name.empty() && class_name != any)
253 classes.insert(class_name);
254 else
255 for (const auto &i : conf.superclasses())
256 classes.insert(*i.first);
257
258 // if there are no options, just list classes
259 if (class_name.empty() && object_id.empty())
260 {
261 std::cout << "The database schema has " << classes.size() << " class(es):\n";
262 for (const auto &i : classes)
263 std::cout << " - \'" << i << "\'\n";
264 return EXIT_SUCCESS;
265 }
266
267 // only print details of classes
268 if (!class_name.empty() && object_id.empty())
269 {
270 if (class_name == any)
271 std::cout << "The database schema has " << classes.size() << " class(es):\n";
272
273 for (const auto &i : classes)
274 conf.get_class_info(i, direct_info).print(std::cout, " ");
275
276 return EXIT_SUCCESS;
277 }
278
279 const char *prefix = "";
280 const char *prefix2 = " ";
281 const char *prefix3 = " ";
282
283 // list or print all objects of class(es)
284 if (object_id == any)
285 {
286 if (class_name.empty() || class_name == any)
287 {
288 std::cout << "The database schema has " << classes.size() << " class(es):\n";
289 prefix = " ";
290 prefix2 = " ";
291 prefix3 = " ";
292 }
293
294 for (std::set<std::string>::const_iterator i = classes.begin(); i != classes.end(); ++i)
295 {
296 std::vector<ConfigObject> objects;
297 conf.get(*i, objects);
298 if (objects.empty())
299 {
300 std::cout << prefix << "The class \'" << *i << "\' has no objects\n";
301 }
302 else
303 {
304 std::cout << prefix << "The class \'" << *i << "\' has " << objects.size() << " object(s) including sub-classes:\n";
305
306 // sort by ID for consistent output
307 std::set<const ConfigObject*, SortByName> sorted_by_id;
308
309 for (const auto &j : objects)
310 sorted_by_id.insert(&j);
311
312 for (const auto &j : sorted_by_id)
313 if (j->class_name() == *i)
314 {
315 if (objects_details)
316 j->print_ref(std::cout, conf, prefix2, contained_in);
317 else
318 std::cout << prefix << " - \'" << j->UID() << "\'\n";
319
320 if (referenced_by)
321 print_referenced_by(*j, prefix3);
322 }
323 else
324 {
325 std::cout << prefix << " - \'" << j->UID() << "\' (database class name = \'" << j->class_name() << "\')\n";
326 }
327 }
328 }
329 }
330 else
331 {
333 conf.get(class_name, object_id, obj);
334 obj.print_ref(std::cout, conf, "", contained_in);
335 if (referenced_by)
336 print_referenced_by(obj, prefix2);
337 }
338 }
339 catch (dunedaq::conffwk::Exception &ex)
340 {
341 ers::fatal(config_dump::ConfigException(ERS_HERE, ex));
342 return EXIT_FAILURE;
343 }
344
345 return EXIT_SUCCESS;
346}
#define ERS_HERE
Represents database objects.
const std::string & UID() const noexcept
Return object identity.
Defines base class for cache of template objects.
const conffwk::fmap< conffwk::fset > & superclasses() const noexcept
void get(const std::string &class_name, const std::string &id, ConfigObject &object, unsigned long rlevel=0, const std::vector< std::string > *rclasses=0)
Get object by class name and object id (multi-thread safe).
const dunedaq::conffwk::class_t & get_class_info(const std::string &class_name, bool direct_only=false)
The method provides access to description of class.
std::vector< dunedaq::conffwk::Version > get_changes()
Get new conffwk versions.
std::vector< dunedaq::conffwk::Version > get_versions(const std::string &since, const std::string &until, dunedaq::conffwk::Version::QueryType type=dunedaq::conffwk::Version::query_by_date, bool skip_irrelevant=true)
Get repository versions in interval.
conffwk entry point
int main(int argc, char *argv[])
static void print_versions(const std::vector< dunedaq::conffwk::Version > &versions)
static void print_referenced_by(const ConfigObject &obj, const char *prefix)
ConfigException
#define ERS_DECLARE_ISSUE( namespace_name, class_name, message_, attributes)
Definition macro.hpp:65
msgpack::object obj
void fatal(const Issue &issue)
Definition ers.hpp:99
bool operator()(const ConfigObject *o1, const ConfigObject *o2) const
void print(std::ostream &out, const std::string &prefix="") const