The Gaudi Framework  master (adcf1ca6)
Loading...
Searching...
No Matches
genconf.cpp File Reference
#include <Gaudi/Algorithm.h>
#include <Gaudi/Auditor.h>
#include <Gaudi/IAuditor.h>
#include <Gaudi/PluginService.h>
#include <GaudiKernel/AlgTool.h>
#include <GaudiKernel/Bootstrap.h>
#include <GaudiKernel/DataHandle.h>
#include <GaudiKernel/DataHandleProperty.h>
#include <GaudiKernel/GaudiHandle.h>
#include <GaudiKernel/HashMap.h>
#include <GaudiKernel/IAlgTool.h>
#include <GaudiKernel/IAlgorithm.h>
#include <GaudiKernel/IAppMgrUI.h>
#include <GaudiKernel/IProperty.h>
#include <GaudiKernel/ISvcLocator.h>
#include <GaudiKernel/Service.h>
#include <GaudiKernel/SmartIF.h>
#include <GaudiKernel/System.h>
#include <GaudiKernel/Time.h>
#include <algorithm>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/program_options.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
#include <exception>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <type_traits>
#include <vector>
Include dependency graph for genconf.cpp:

Go to the source code of this file.

Classes

class  configGenerator
 

Macros

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )
 
#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )
 
#define LOG_INFO   BOOST_LOG_TRIVIAL( info )
 

Typedefs

typedef std::vector< std::string > Strings_t
 
typedef std::vector< fs::path > LibPathNames_t
 

Functions

int createAppMgr ()
 
void init_logging (boost::log::trivial::severity_level level)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ LOG_ERROR

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )

Definition at line 65 of file genconf.cpp.

◆ LOG_INFO

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 67 of file genconf.cpp.

◆ LOG_WARNING

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 66 of file genconf.cpp.

Typedef Documentation

◆ LibPathNames_t

typedef std::vector<fs::path> LibPathNames_t

Definition at line 76 of file genconf.cpp.

◆ Strings_t

typedef std::vector<std::string> Strings_t

Definition at line 75 of file genconf.cpp.

Function Documentation

◆ createAppMgr()

int createAppMgr ( )

Definition at line 868 of file genconf.cpp.

870{
872 SmartIF<IAppMgrUI> appUI( iface );
873 auto propMgr = appUI.as<IProperty>();
874 if ( !propMgr || !appUI ) return EXIT_FAILURE;
875
876 propMgr->setProperty( "JobOptionsType", "NONE" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No job
877 // options
878 propMgr->setProperty( "AppName", "" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No initial printout
879 // message
880 propMgr->setProperty( "OutputLevel", 7 ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No other printout
881 // messages
882 appUI->configure().ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
883 auto msgSvc = SmartIF<IMessageSvc>{ iface }.as<IProperty>();
884 msgSvc->setPropertyRepr( "setWarning", "['DefaultName', 'PropertyHolder']" )
885 .ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
886 msgSvc->setProperty( "Format", "%T %0W%M" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
887 return EXIT_SUCCESS;
888}
Definition of the basic interface.
Definition IInterface.h:225
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition IProperty.h:32
StatusCode setProperty(const Gaudi::Details::PropertyBase &p)
Set the property from a property.
Definition IProperty.h:38
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition SmartIF.h:110
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
GAUDI_API IAppMgrUI * createApplicationMgr()

◆ init_logging()

void init_logging ( boost::log::trivial::severity_level level)

Definition at line 255 of file genconf.cpp.

255 {
256 namespace logging = boost::log;
257 namespace keywords = boost::log::keywords;
258 namespace expr = boost::log::expressions;
259
260 logging::add_console_log( std::cout, keywords::format =
261 ( expr::stream << "[" << std::setw( 7 ) << std::left
262 << logging::trivial::severity << "] " << expr::smessage ) );
263
264 logging::core::get()->set_filter( logging::trivial::severity >= level );
265}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 268 of file genconf.cpp.

270{
271 init_logging( ( System::isEnvSet( "VERBOSE" ) && !System::getEnv( "VERBOSE" ).empty() )
272 ? boost::log::trivial::info
273 : boost::log::trivial::warning );
274
275 fs::path pwd = fs::initial_path();
276 fs::path out;
277 Strings_t libs;
278 std::string pkgName;
279 std::string userModule;
280
281 // declare a group of options that will be allowed only on command line
282 po::options_description generic( "Generic options" );
283 generic.add_options()( "help,h", "produce this help message" )(
284 "package-name,p", po::value<string>(), "name of the package for which we create the configurables file" )(
285 "input-libraries,i", po::value<string>(), "libraries to extract the component configurables from" )(
286 "input-cfg,c", po::value<string>(),
287 "path to the cfg file holding the description of the Configurable base "
288 "classes, the python module holding the Configurable definitions, etc..." )(
289 "output-dir,o", po::value<string>()->default_value( "../genConfDir" ),
290 "output directory for genconf files." )( "debug-level,d", po::value<int>()->default_value( 0 ), "debug level" )(
291 "load-library,l", po::value<Strings_t>()->composing(), "preloading library" )(
292 "user-module,m", po::value<string>(), "user-defined module to be imported by the genConf-generated one" )(
293 "no-init", "do not generate the (empty) __init__.py [deprecated]" )(
294 "type", po::value<string>()->default_value( "conf,conf2" ), "comma-separate types of configurables to generate" );
295
296 // declare a group of options that will be allowed both on command line
297 // _and_ in configuration file
298 po::options_description config( "Configuration" );
299 config.add_options()( "configurable-module", po::value<string>()->default_value( "AthenaCommon" ),
300 "Name of the module holding the configurable classes" )(
301 "configurable-default-name", po::value<string>()->default_value( "Configurable.DefaultName" ),
302 "Default name for the configurable instance" )( "configurable-algorithm",
303 po::value<string>()->default_value( "ConfigurableAlgorithm" ),
304 "Name of the configurable base class for Algorithm components" )(
305 "configurable-algtool", po::value<string>()->default_value( "ConfigurableAlgTool" ),
306 "Name of the configurable base class for AlgTool components" )(
307 "configurable-auditor", po::value<string>()->default_value( "ConfigurableAuditor" ),
308 "Name of the configurable base class for Auditor components" )(
309 "configurable-service", po::value<string>()->default_value( "ConfigurableService" ),
310 "Name of the configurable base class for Service components" );
311
312 po::options_description cmdline_options;
313 cmdline_options.add( generic ).add( config );
314
315 po::options_description config_file_options;
316 config_file_options.add( config );
317
318 po::options_description visible( "Allowed options" );
319 visible.add( generic ).add( config );
320
321 po::variables_map vm;
322
323 try {
324 po::store( po::command_line_parser( argc, argv ).options( cmdline_options ).run(), vm );
325
326 po::notify( vm );
327
328 // try to read configuration from the optionally given configuration file
329 if ( vm.contains( "input-cfg" ) ) {
330 string cfgFileName = vm["input-cfg"].as<string>();
331 cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
332 std::ifstream ifs( cfgFileName );
333 po::store( parse_config_file( ifs, config_file_options ), vm );
334 }
335
336 po::notify( vm );
337 } catch ( po::error& err ) {
338 LOG_ERROR << "error detected while parsing command options: " << err.what();
339 return EXIT_FAILURE;
340 }
341
342 //--- Process command options -----------------------------------------------
343 if ( vm.contains( "help" ) ) {
344 cout << visible << endl;
345 return EXIT_FAILURE;
346 }
347
348 if ( vm.contains( "no-init" ) ) { cout << "WARNING: option --no-init is deprecated as it is not needed anymore.\n"; }
349
350 if ( vm.contains( "package-name" ) ) {
351 pkgName = vm["package-name"].as<string>();
352 } else {
353 LOG_ERROR << "'package-name' required";
354 cout << visible << endl;
355 return EXIT_FAILURE;
356 }
357
358 if ( vm.contains( "user-module" ) ) {
359 userModule = vm["user-module"].as<string>();
360 LOG_INFO << "INFO: will import user module " << userModule;
361 }
362
363 if ( vm.contains( "input-libraries" ) ) {
364 // re-shape the input arguments:
365 // - removes spurious spaces,
366 // - split into tokens.
367 std::string tmp = vm["input-libraries"].as<std::string>();
368 boost::trim( tmp );
369 boost::split( libs, tmp, boost::is_any_of( " " ), boost::token_compress_on );
370 } else {
371 LOG_ERROR << "input component library(ies) required";
372 cout << visible << endl;
373 return EXIT_FAILURE;
374 }
375
376 if ( vm.contains( "output-dir" ) ) { out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) ); }
377
378 if ( vm.contains( "debug-level" ) ) { Gaudi::PluginService::SetDebug( vm["debug-level"].as<int>() ); }
379
380 if ( vm.contains( "load-library" ) ) {
381 for ( const auto& lLib : vm["load-library"].as<Strings_t>() ) {
382 // load done through Gaudi helper class
383 System::ImageHandle tmp; // we ignore the library handle
384 unsigned long err = System::loadDynamicLib( lLib, &tmp );
385 if ( err != 1 ) LOG_WARNING << "failed to load: " << lLib;
386 }
387 }
388
389 std::set<conf_t> confTypes;
390 if ( vm.contains( "type" ) ) {
391 for ( const std::string& type : boost::tokenizer{ vm["type"].as<std::string>(), boost::char_separator{ "," } } ) {
392 if ( type == "conf" ) {
393 confTypes.insert( conf_t::CONF );
394 } else if ( type == "conf2" ) {
395 confTypes.insert( conf_t::CONF2 );
396 } else {
397 LOG_ERROR << "unknown configurable type: " << type;
398 cout << visible << endl;
399 return EXIT_FAILURE;
400 }
401 }
402 }
403
404 if ( !fs::exists( out ) ) {
405 try {
406 fs::create_directory( out );
407 } catch ( fs::filesystem_error& err ) {
408 LOG_ERROR << "error creating directory: " << err.what();
409 return EXIT_FAILURE;
410 }
411 }
412
413 {
414 std::ostringstream msg;
415 msg << ":::::: libraries : [ ";
416 std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
417 msg << "] ::::::";
418 LOG_INFO << msg.str();
419 }
420
421 configGenerator py( pkgName, out.string() );
422 py.setConfigurableModule( vm["configurable-module"].as<string>() );
423 py.setConfigurableTypes( confTypes );
424 py.setConfigurableDefaultName( vm["configurable-default-name"].as<string>() );
425 py.setConfigurableAlgorithm( vm["configurable-algorithm"].as<string>() );
426 py.setConfigurableAlgTool( vm["configurable-algtool"].as<string>() );
427 py.setConfigurableAuditor( vm["configurable-auditor"].as<string>() );
428 py.setConfigurableService( vm["configurable-service"].as<string>() );
429
430 int sc = EXIT_FAILURE;
431 try {
432 sc = py.genConfig( libs, userModule );
433 } catch ( exception& e ) {
434 cout << "ERROR: Could not generate Configurable(s) !\n"
435 << "ERROR: Got exception: " << e.what() << endl;
436 return EXIT_FAILURE;
437 }
438
439 {
440 std::ostringstream msg;
441 msg << ":::::: libraries : [ ";
442 std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
443 msg << "] :::::: [DONE]";
444 LOG_INFO << msg.str();
445 }
446 return sc;
447}
std::vector< std::string > Strings_t
Definition genconf.cpp:75
void init_logging(boost::log::trivial::severity_level level)
Definition genconf.cpp:255
#define LOG_ERROR
Definition genconf.cpp:65
#define LOG_WARNING
Definition genconf.cpp:66
#define LOG_INFO
Definition genconf.cpp:67
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition System.cpp:349
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition System.cpp:115
void * ImageHandle
Definition of an image handle.
Definition ModuleInfo.h:25
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition System.cpp:329