The Gaudi Framework  v30r0 (c919700c)
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/Algorithm.h"
#include "GaudiKernel/Auditor.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 "DsoUtils.h"
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 78 of file genconf.cpp.

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )

Definition at line 75 of file genconf.cpp.

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 77 of file genconf.cpp.

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 76 of file genconf.cpp.

Typedef Documentation

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

Definition at line 87 of file genconf.cpp.

Definition at line 83 of file genconf.cpp.

Enumeration Type Documentation

enum component_t
strong

Definition at line 98 of file genconf.cpp.

98  {
99  Module,
100  DefaultName,
101  Algorithm,
102  AlgTool,
103  Auditor,
104  Service,
105  ApplicationMgr,
106  IInterface,
107  Converter,
108  DataObject,
109  Unknown
110  };
Definition of the basic interface.
Definition: IInterface.h:277
Converter base class.
Definition: Converter.h:24
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
Base class for all services.
Definition: Service.h:36
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:35

Function Documentation

int createAppMgr ( )

Definition at line 804 of file genconf.cpp.

806 {
808  SmartIF<IAppMgrUI> appUI( iface );
809  auto propMgr = appUI.as<IProperty>();
810  if ( !propMgr || !appUI ) return EXIT_FAILURE;
811 
812  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
813  propMgr->setProperty( "AppName", "" ); // No initial printout message
814  propMgr->setProperty( "OutputLevel", "7" ); // No other printout messages
815  appUI->configure();
816  auto msgSvc = SmartIF<IMessageSvc>{iface}.as<IProperty>();
817  msgSvc->setProperty( "setWarning", "['DefaultName', 'PropertyHolder']" );
818  msgSvc->setProperty( "Format", "%T %0W%M" );
819  return EXIT_SUCCESS;
820 }
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
Definition of the basic interface.
Definition: IInterface.h:277
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 231 of file genconf.cpp.

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

Definition at line 245 of file genconf.cpp.

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