The Gaudi Framework  master (37c0b60a)
genconf.cpp File Reference
#include <Gaudi/Algorithm.h>
#include <Gaudi/PluginService.h>
#include <GaudiKernel/AlgTool.h>
#include <GaudiKernel/Auditor.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/IAuditor.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::stringStrings_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 76 of file genconf.cpp.

◆ LOG_INFO

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 78 of file genconf.cpp.

◆ LOG_WARNING

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 77 of file genconf.cpp.

Typedef Documentation

◆ LibPathNames_t

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

Definition at line 86 of file genconf.cpp.

◆ Strings_t

Definition at line 85 of file genconf.cpp.

Function Documentation

◆ createAppMgr()

int createAppMgr ( )

Definition at line 883 of file genconf.cpp.

885 {
887  SmartIF<IAppMgrUI> appUI( iface );
888  auto propMgr = appUI.as<IProperty>();
889  if ( !propMgr || !appUI ) return EXIT_FAILURE;
890 
891  propMgr->setProperty( "JobOptionsType", "NONE" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No job
892  // options
893  propMgr->setProperty( "AppName", "" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No initial printout
894  // message
895  propMgr->setProperty( "OutputLevel", 7 ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No other printout
896  // messages
897  appUI->configure().ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
898  auto msgSvc = SmartIF<IMessageSvc>{ iface }.as<IProperty>();
899  msgSvc->setPropertyRepr( "setWarning", "['DefaultName', 'PropertyHolder']" )
900  .ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
901  msgSvc->setProperty( "Format", "%T %0W%M" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
902  return EXIT_SUCCESS;
903 }

◆ init_logging()

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

Definition at line 266 of file genconf.cpp.

266  {
267  namespace logging = boost::log;
268  namespace keywords = boost::log::keywords;
269  namespace expr = boost::log::expressions;
270 
271  logging::add_console_log( std::cout, keywords::format =
272  ( expr::stream << "[" << std::setw( 7 ) << std::left
273  << logging::trivial::severity << "] " << expr::smessage ) );
274 
275  logging::core::get()->set_filter( logging::trivial::severity >= level );
276 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 279 of file genconf.cpp.

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