Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
genconf.cpp File Reference
#include "boost/program_options.hpp"
#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/convenience.hpp"
#include "boost/filesystem/exception.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/format.hpp"
#include "boost/regex.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 "GaudiKernel/Bootstrap.h"
#include "GaudiKernel/DataObjectHandleBase.h"
#include "GaudiKernel/DataObjectHandleProperty.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/AlgTool.h"
#include "GaudiKernel/Auditor.h"
#include <Gaudi/Algorithm.h>
#include "GaudiKernel/Time.h"
#include <Gaudi/PluginService.h>
#include <algorithm>
#include <exception>
#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 )
 
#define LOG_DEBUG   BOOST_LOG_TRIVIAL( debug )
 

Typedefs

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

Enumerations

enum  component_t
 

Functions

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

Macro Definition Documentation

#define LOG_DEBUG   BOOST_LOG_TRIVIAL( debug )

Definition at line 76 of file genconf.cpp.

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )

Definition at line 73 of file genconf.cpp.

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 75 of file genconf.cpp.

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 74 of file genconf.cpp.

Typedef Documentation

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

Definition at line 85 of file genconf.cpp.

Definition at line 81 of file genconf.cpp.

Enumeration Type Documentation

enum component_t
strong

Definition at line 95 of file genconf.cpp.

95  {
96  Module,
97  DefaultName,
98  Algorithm,
99  AlgTool,
100  Auditor,
101  Service,
102  ApplicationMgr,
103  IInterface,
104  Converter,
105  DataObject,
106  Unknown
107  };
Definition of the basic interface.
Definition: IInterface.h:244
Converter base class.
Definition: Converter.h:24
Alias for backward compatibility.
Definition: Algorithm.h:56
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
Base class for all services.
Definition: Service.h:36
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:34

Function Documentation

int createAppMgr ( )

Definition at line 778 of file genconf.cpp.

780 {
782  SmartIF<IAppMgrUI> appUI( iface );
783  auto propMgr = appUI.as<IProperty>();
784  if ( !propMgr || !appUI ) return EXIT_FAILURE;
785 
786  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
787  propMgr->setProperty( "AppName", "" ); // No initial printout message
788  propMgr->setProperty( "OutputLevel", "7" ); // No other printout messages
789  appUI->configure();
790  auto msgSvc = SmartIF<IMessageSvc>{iface}.as<IProperty>();
791  msgSvc->setProperty( "setWarning", "['DefaultName', 'PropertyHolder']" );
792  msgSvc->setProperty( "Format", "%T %0W%M" );
793  return EXIT_SUCCESS;
794 }
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
Definition of the basic interface.
Definition: IInterface.h:244
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
void init_logging ( boost::log::trivial::severity_level  level)

Definition at line 235 of file genconf.cpp.

235  {
236  namespace logging = boost::log;
237  namespace keywords = boost::log::keywords;
238  namespace expr = boost::log::expressions;
239 
240  logging::add_console_log( std::cout, keywords::format =
241  ( expr::stream << "[" << std::setw( 7 ) << std::left
242  << logging::trivial::severity << "] " << expr::smessage ) );
243 
244  logging::core::get()->set_filter( logging::trivial::severity >= level );
245 }
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
T left(T...args)
T setw(T...args)
int main ( int  argc,
char **  argv 
)

Definition at line 248 of file genconf.cpp.

250 {
251  init_logging( ( System::isEnvSet( "VERBOSE" ) && !System::getEnv( "VERBOSE" ).empty() )
252  ? boost::log::trivial::info
253  : boost::log::trivial::warning );
254 
255  fs::path pwd = fs::initial_path();
256  fs::path out;
257  Strings_t libs;
258  std::string pkgName;
259  std::string userModule;
260 
261  // declare a group of options that will be allowed only on command line
262  po::options_description generic( "Generic options" );
263  generic.add_options()( "help,h", "produce this help message" )(
264  "package-name,p", po::value<string>(), "name of the package for which we create the configurables file" )(
265  "input-libraries,i", po::value<string>(), "libraries to extract the component configurables from" )(
266  "input-cfg,c", po::value<string>(),
267  "path to the cfg file holding the description of the Configurable base "
268  "classes, the python module holding the Configurable definitions, etc..." )(
269  "output-dir,o", po::value<string>()->default_value( "../genConf" ),
270  "output directory for genconf files." )( "debug-level,d", po::value<int>()->default_value( 0 ), "debug level" )(
271  "load-library,l", po::value<Strings_t>()->composing(), "preloading library" )(
272  "user-module,m", po::value<string>(), "user-defined module to be imported by the genConf-generated one" )(
273  "no-init", "do not generate the (empty) __init__.py" );
274 
275  // declare a group of options that will be allowed both on command line
276  // _and_ in configuration file
277  po::options_description config( "Configuration" );
278  config.add_options()( "configurable-module", po::value<string>()->default_value( "AthenaCommon" ),
279  "Name of the module holding the configurable classes" )(
280  "configurable-default-name", po::value<string>()->default_value( "Configurable.DefaultName" ),
281  "Default name for the configurable instance" )( "configurable-algorithm",
282  po::value<string>()->default_value( "ConfigurableAlgorithm" ),
283  "Name of the configurable base class for Algorithm components" )(
284  "configurable-algtool", po::value<string>()->default_value( "ConfigurableAlgTool" ),
285  "Name of the configurable base class for AlgTool components" )(
286  "configurable-auditor", po::value<string>()->default_value( "ConfigurableAuditor" ),
287  "Name of the configurable base class for Auditor components" )(
288  "configurable-service", po::value<string>()->default_value( "ConfigurableService" ),
289  "Name of the configurable base class for Service components" );
290 
291  po::options_description cmdline_options;
292  cmdline_options.add( generic ).add( config );
293 
294  po::options_description config_file_options;
295  config_file_options.add( config );
296 
297  po::options_description visible( "Allowed options" );
298  visible.add( generic ).add( config );
299 
300  po::variables_map vm;
301 
302  try {
303  po::store( po::command_line_parser( argc, argv ).options( cmdline_options ).run(), vm );
304 
305  po::notify( vm );
306 
307  // try to read configuration from the optionally given configuration file
308  if ( vm.count( "input-cfg" ) ) {
309  string cfgFileName = vm["input-cfg"].as<string>();
310  cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
311  std::ifstream ifs( cfgFileName );
312  po::store( parse_config_file( ifs, config_file_options ), vm );
313  }
314 
315  po::notify( vm );
316  } catch ( po::error& err ) {
317  LOG_ERROR << "error detected while parsing command options: " << err.what();
318  return EXIT_FAILURE;
319  }
320 
321  //--- Process command options -----------------------------------------------
322  if ( vm.count( "help" ) ) {
323  cout << visible << endl;
324  return EXIT_FAILURE;
325  }
326 
327  if ( vm.count( "package-name" ) ) {
328  pkgName = vm["package-name"].as<string>();
329  } else {
330  LOG_ERROR << "'package-name' required";
331  cout << visible << endl;
332  return EXIT_FAILURE;
333  }
334 
335  if ( vm.count( "user-module" ) ) {
336  userModule = vm["user-module"].as<string>();
337  LOG_INFO << "INFO: will import user module " << userModule;
338  }
339 
340  if ( vm.count( "input-libraries" ) ) {
341  // re-shape the input arguments:
342  // - removes spurious spaces,
343  // - split into tokens.
344  Strings_t inputLibs;
345  {
346  std::string tmp = vm["input-libraries"].as<std::string>();
347  boost::trim( tmp );
348  boost::split( inputLibs, tmp, boost::is_any_of( " " ), boost::token_compress_on );
349  }
350 
351  //
352  libs.reserve( inputLibs.size() );
353  for ( const auto& iLib : inputLibs ) {
354  std::string lib = fs::path( iLib ).stem().string();
355  if ( lib.compare( 0, 3, "lib" ) == 0 ) {
356  lib = lib.substr( 3 ); // For *NIX remove "lib"
357  }
358  // remove duplicates
359  if ( !lib.empty() && std::find( libs.begin(), libs.end(), lib ) == libs.end() ) { libs.push_back( lib ); }
360  } //> end loop over input-libraries
361  if ( libs.empty() ) {
362  LOG_ERROR << "input component library(ies) required !\n";
363  LOG_ERROR << "'input-libraries' argument was [" << vm["input-libraries"].as<string>() << "]";
364  return EXIT_FAILURE;
365  }
366  } else {
367  LOG_ERROR << "input component library(ies) required";
368  cout << visible << endl;
369  return EXIT_FAILURE;
370  }
371 
372  if ( vm.count( "output-dir" ) ) { out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) ); }
373 
374  if ( vm.count( "debug-level" ) ) { Gaudi::PluginService::SetDebug( vm["debug-level"].as<int>() ); }
375 
376  if ( vm.count( "load-library" ) ) {
377  for ( const auto& lLib : vm["load-library"].as<Strings_t>() ) {
378  // load done through Gaudi helper class
379  System::ImageHandle tmp; // we ignore the library handle
380  unsigned long err = System::loadDynamicLib( lLib, &tmp );
381  if ( err != 1 ) LOG_WARNING << "failed to load: " << lLib;
382  }
383  }
384 
385  if ( !fs::exists( out ) ) {
386  try {
387  fs::create_directory( out );
388  } catch ( fs::filesystem_error& err ) {
389  LOG_ERROR << "error creating directory: " << err.what();
390  return EXIT_FAILURE;
391  }
392  }
393 
394  {
396  msg << ":::::: libraries : [ ";
397  std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
398  msg << "] ::::::";
399  LOG_INFO << msg.str();
400  }
401 
402  configGenerator py( pkgName, out.string() );
403  py.setConfigurableModule( vm["configurable-module"].as<string>() );
404  py.setConfigurableDefaultName( vm["configurable-default-name"].as<string>() );
405  py.setConfigurableAlgorithm( vm["configurable-algorithm"].as<string>() );
406  py.setConfigurableAlgTool( vm["configurable-algtool"].as<string>() );
407  py.setConfigurableAuditor( vm["configurable-auditor"].as<string>() );
408  py.setConfigurableService( vm["configurable-service"].as<string>() );
409 
410  int sc = EXIT_FAILURE;
411  try {
412  sc = py.genConfig( libs, userModule );
413  } catch ( exception& e ) {
414  cout << "ERROR: Could not generate Configurable(s) !\n"
415  << "ERROR: Got exception: " << e.what() << endl;
416  return EXIT_FAILURE;
417  }
418 
419  if ( EXIT_SUCCESS == sc && !vm.count( "no-init" ) ) {
420  // create an empty __init__.py file in the output dir
421  std::fstream initPy( ( out / fs::path( "__init__.py" ) ).string(), std::ios_base::out | std::ios_base::trunc );
422  initPy << "## Hook for " << pkgName << " genConf module\n" << flush;
423  }
424 
425  {
427  msg << ":::::: libraries : [ ";
428  std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
429  msg << "] :::::: [DONE]";
430  LOG_INFO << msg.str();
431  }
432  return sc;
433 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:380
T empty(T...args)
T copy(T...args)
list argv
Definition: gaudirun.py:294
#define LOG_ERROR
Definition: genconf.cpp:73
T endl(T...args)
T end(T...args)
void * ImageHandle
Definition of an image handle.
Definition: ModuleInfo.h:30
void setConfigurableModule(const std::string &moduleName)
customize the Module name where configurable base classes are defined
Definition: genconf.cpp:194
STL class.
T push_back(T...args)
T what(T...args)
#define LOG_INFO
Definition: genconf.cpp:75
STL class.
STL class.
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:400
T flush(T...args)
void init_logging(boost::log::trivial::severity_level level)
Definition: genconf.cpp:235
T find(T...args)
T size(T...args)
#define LOG_WARNING
Definition: genconf.cpp:74
T begin(T...args)
T substr(T...args)
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
STL class.
T compare(T...args)
T reserve(T...args)
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:146