The Gaudi Framework  v33r1 (b1225454)
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

◆ LOG_DEBUG

#define LOG_DEBUG   BOOST_LOG_TRIVIAL( debug )

Definition at line 86 of file genconf.cpp.

◆ LOG_ERROR

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )

Definition at line 83 of file genconf.cpp.

◆ LOG_INFO

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 85 of file genconf.cpp.

◆ LOG_WARNING

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 84 of file genconf.cpp.

Typedef Documentation

◆ LibPathNames_t

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

Definition at line 95 of file genconf.cpp.

◆ Strings_t

Definition at line 91 of file genconf.cpp.

Enumeration Type Documentation

◆ component_t

enum component_t
strong

Definition at line 112 of file genconf.cpp.

112  {
113  Module,
114  DefaultName,
115  Algorithm,
116  AlgTool,
117  Auditor,
118  Service,
119  ApplicationMgr,
120  IInterface,
121  Converter,
122  DataObject,
123  Unknown
124  };
Definition of the basic interface.
Definition: IInterface.h:254
Converter base class.
Definition: Converter.h:34
Alias for backward compatibility.
Definition: Algorithm.h:58
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:57
Base class for all services.
Definition: Service.h:46
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:44

Function Documentation

◆ createAppMgr()

int createAppMgr ( )

Definition at line 848 of file genconf.cpp.

850 {
852  SmartIF<IAppMgrUI> appUI( iface );
853  auto propMgr = appUI.as<IProperty>();
854  if ( !propMgr || !appUI ) return EXIT_FAILURE;
855 
856  propMgr->setProperty( "JobOptionsType", "NONE" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No job
857  // options
858  propMgr->setProperty( "AppName", "" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No initial printout
859  // message
860  propMgr->setProperty( "OutputLevel", "7" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ ); // No other
861  // printout
862  // messages
863  appUI->configure().ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
864  auto msgSvc = SmartIF<IMessageSvc>{iface}.as<IProperty>();
865  msgSvc->setProperty( "setWarning", "['DefaultName', 'PropertyHolder']" )
866  .ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
867  msgSvc->setProperty( "Format", "%T %0W%M" ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
868  return EXIT_SUCCESS;
869 }
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
Definition of the basic interface.
Definition: IInterface.h:254
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:30
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)

◆ init_logging()

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

Definition at line 257 of file genconf.cpp.

257  {
258  namespace logging = boost::log;
259  namespace keywords = boost::log::keywords;
260  namespace expr = boost::log::expressions;
261 
262  logging::add_console_log( std::cout, keywords::format =
263  ( expr::stream << "[" << std::setw( 7 ) << std::left
264  << logging::trivial::severity << "] " << expr::smessage ) );
265 
266  logging::core::get()->set_filter( logging::trivial::severity >= level );
267 }
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
T left(T... args)
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
T setw(T... args)

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 270 of file genconf.cpp.

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