The Gaudi Framework  v40r0 (475e45c1)
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 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

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

Definition at line 85 of file genconf.cpp.

Function Documentation

◆ createAppMgr()

int createAppMgr ( )

Definition at line 884 of file genconf.cpp.

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

◆ init_logging()

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

Definition at line 267 of file genconf.cpp.

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 280 of file genconf.cpp.

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