The Gaudi Framework  v29r0 (ff2e7097)
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 <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
 

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 77 of file genconf.cpp.

#define LOG_ERROR   BOOST_LOG_TRIVIAL( error )

Definition at line 74 of file genconf.cpp.

#define LOG_INFO   BOOST_LOG_TRIVIAL( info )

Definition at line 76 of file genconf.cpp.

#define LOG_WARNING   BOOST_LOG_TRIVIAL( warning )

Definition at line 75 of file genconf.cpp.

Typedef Documentation

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

Definition at line 86 of file genconf.cpp.

Definition at line 82 of file genconf.cpp.

Function Documentation

int createAppMgr ( )

Definition at line 782 of file genconf.cpp.

784 {
786  SmartIF<IAppMgrUI> appUI( iface );
787  auto propMgr = appUI.as<IProperty>();
788 
789  if ( !propMgr || !appUI ) return EXIT_FAILURE;
790  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
791  propMgr->setProperty( "AppName", "" ); // No initial printout message
792  propMgr->setProperty( "OutputLevel", "7" ); // No other printout messages
793  appUI->configure();
795  msgSvc->setProperty( "setWarning", "['DefaultName', 'PropertyHolder']" );
796  msgSvc->setProperty( "Format", "%T %0W%M" );
797  return EXIT_SUCCESS;
798 }
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 189 of file genconf.cpp.

190 {
191  namespace logging = boost::log;
192  namespace keywords = boost::log::keywords;
193  namespace expr = boost::log::expressions;
194 
195  logging::add_console_log( std::cout, keywords::format =
196  ( expr::stream << "[" << std::setw( 7 ) << std::left
197  << logging::trivial::severity << "] " << expr::smessage ) );
198 
199  logging::core::get()->set_filter( logging::trivial::severity >= level );
200 }
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 203 of file genconf.cpp.

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