The Gaudi Framework  v30r4 (9b837755)
genconf.cpp
Go to the documentation of this file.
1 #ifdef _WIN32
2 // Disable a warning in Boost program_options headers:
3 // inconsistent linkage in program_options/variables_map.hpp
4 #pragma warning( disable : 4273 )
5 
6 // Avoid conflicts between windows and the message service.
7 #define NOMSG
8 #define NOGDI
9 #endif
10 
11 #ifdef __ICC
12 // disable icc warning #279: controlling expression is constant
13 // ... a lot of noise produced by the boost/filesystem/operations.hpp
14 #pragma warning( disable : 279 )
15 #endif
16 
17 #include "boost/program_options.hpp"
18 
19 // Include files----------------------------------------------------------------
20 #include "boost/algorithm/string/case_conv.hpp"
21 #include "boost/algorithm/string/classification.hpp"
22 #include "boost/algorithm/string/replace.hpp"
23 #include "boost/algorithm/string/split.hpp"
24 #include "boost/algorithm/string/trim.hpp"
25 #include "boost/filesystem/convenience.hpp"
26 #include "boost/filesystem/exception.hpp"
27 #include "boost/filesystem/operations.hpp"
28 #include "boost/format.hpp"
29 #include "boost/regex.hpp"
30 
31 #include <boost/log/core.hpp>
32 #include <boost/log/expressions.hpp>
33 #include <boost/log/trivial.hpp>
34 #include <boost/log/utility/setup/common_attributes.hpp>
35 #include <boost/log/utility/setup/console.hpp>
36 
37 #include "GaudiKernel/Bootstrap.h"
42 #include "GaudiKernel/HashMap.h"
43 #include "GaudiKernel/IAlgTool.h"
44 #include "GaudiKernel/IAlgorithm.h"
45 #include "GaudiKernel/IAppMgrUI.h"
46 #include "GaudiKernel/IAuditor.h"
47 #include "GaudiKernel/IProperty.h"
49 #include "GaudiKernel/Service.h"
50 #include "GaudiKernel/SmartIF.h"
51 #include "GaudiKernel/System.h"
52 
53 #include "GaudiKernel/AlgTool.h"
54 #include "GaudiKernel/Algorithm.h"
55 #include "GaudiKernel/Auditor.h"
56 #include "GaudiKernel/Service.h"
57 
58 #include "GaudiKernel/Time.h"
59 
60 #include <Gaudi/PluginService.h>
61 
62 #include <algorithm>
63 #include <exception>
64 #include <fstream>
65 #include <iostream>
66 #include <set>
67 #include <sstream>
68 #include <type_traits>
69 #include <vector>
70 
71 namespace po = boost::program_options;
72 namespace fs = boost::filesystem;
73 
74 #define LOG_ERROR BOOST_LOG_TRIVIAL( error )
75 #define LOG_WARNING BOOST_LOG_TRIVIAL( warning )
76 #define LOG_INFO BOOST_LOG_TRIVIAL( info )
77 #define LOG_DEBUG BOOST_LOG_TRIVIAL( debug )
78 
79 using namespace std;
81 
82 class IConverter;
83 
84 // useful typedefs
87 
88 namespace
89 {
90  const std::string py_tab = " ";
91 
94  const boost::regex pythonIdentifier( "^[a-zA-Z_][a-zA-Z0-9_]*$" );
95 
96  //-----------------------------------------------------------------------------
97  enum class component_t {
98  Module,
99  DefaultName,
100  Algorithm,
101  AlgTool,
102  Auditor,
103  Service,
104  ApplicationMgr,
105  IInterface,
106  Converter,
107  DataObject,
108  Unknown
109  };
110 
111  const std::map<std::string, component_t> allowedFactories{
112  {typeid( Algorithm::Factory::FactoryType ).name(), component_t::Algorithm},
113  {typeid( Service::Factory::FactoryType ).name(), component_t::Service},
114  {typeid( AlgTool::Factory::FactoryType ).name(), component_t::AlgTool},
115  {typeid( Auditor::Factory::FactoryType ).name(), component_t::Auditor},
116  };
117 
119  {
120  static const std::array<std::string, 11> names = {"Module", "DefaultName", "Algorithm", "AlgTool",
121  "Auditor", "Service", "ApplicationMgr", "IInterface",
122  "Converter", "DataObject", "Unknown"};
123  return names.at( static_cast<std::underlying_type_t<component_t>>( type ) );
124  }
125  std::ostream& operator<<( std::ostream& os, component_t type ) { return os << toString( type ); }
126 
127  //-----------------------------------------------------------------------------
129  std::string pythonizeName( const std::string& name )
130  {
131  static const string in( "<>&*,: ()." );
132  static const string out( "__rp__s___" );
133  auto r = boost::algorithm::replace_all_copy( name, ", ", "," );
134  for ( auto& c : r ) {
135  auto rep = in.find( c );
136  if ( rep != string::npos ) c = out[rep];
137  }
138  return r;
139  }
140  //-----------------------------------------------------------------------------
141  template <typename T>
142  std::type_index typeIndex()
143  {
144  return std::type_index{typeid( T )};
145  }
146  //-----------------------------------------------------------------------------
147  inline std::string libNativeName( const std::string& libName )
148  {
149 #if defined( _WIN32 )
150  return libName + ".dll";
151 #elif defined( __linux ) || defined( __APPLE__ )
152  return "lib" + libName + ".so";
153 #else
154  // variant of the GIGO design pattern
155  return libName;
156 #endif
157  }
158 }
159 
161 {
163  string m_pkgName;
164 
168 
171 
174  bool m_importGaudiHandles = false;
175  bool m_importDataObjectHandles = false;
176  bool m_importDataHandles = false;
177 
183 
190 
191 public:
192  configGenerator( const string& pkgName, const string& outputDirName )
193  : m_pkgName( pkgName ), m_outputDirName( outputDirName )
194  {
195  }
196 
201  int genConfig( const Strings_t& modules, const string& userModule );
202 
204  void setConfigurableModule( const std::string& moduleName ) { m_configurable[component_t::Module] = moduleName; }
205 
207  void setConfigurableDefaultName( const std::string& defaultName )
208  {
209  m_configurable[component_t::DefaultName] = defaultName;
210  }
211 
213  void setConfigurableAlgorithm( const std::string& cfgAlgorithm )
214  {
215  m_configurable[component_t::Algorithm] = cfgAlgorithm;
216  }
217 
219  void setConfigurableAlgTool( const std::string& cfgAlgTool ) { m_configurable[component_t::AlgTool] = cfgAlgTool; }
220 
222  void setConfigurableAuditor( const std::string& cfgAuditor ) { m_configurable[component_t::Auditor] = cfgAuditor; }
223 
225  void setConfigurableService( const std::string& cfgService )
226  {
227  m_configurable[component_t::Service] = cfgService;
228  m_configurable[component_t::ApplicationMgr] = cfgService;
229  }
230 
231 private:
232  bool genComponent( const std::string& libName, const std::string& componentName, component_t componentType,
234  void genImport( std::ostream& s, const boost::format& frmt, std::string indent );
235  void genHeader( std::ostream& pyOut, std::ostream& dbOut );
236  void genBody( std::ostream& pyOut, std::ostream& dbOut )
237  {
238  pyOut << m_pyBuf.str() << flush;
239  dbOut << m_dbBuf.str() << flush;
240  }
241  void genTrailer( std::ostream& pyOut, std::ostream& dbOut );
242 
244  void pythonizeValue( const PropertyBase* prop, string& pvalue, string& ptype );
245 };
246 
247 int createAppMgr();
248 
249 void init_logging( boost::log::trivial::severity_level level )
250 {
251  namespace logging = boost::log;
252  namespace keywords = boost::log::keywords;
253  namespace expr = boost::log::expressions;
254 
255  logging::add_console_log( std::cout, keywords::format =
256  ( expr::stream << "[" << std::setw( 7 ) << std::left
257  << logging::trivial::severity << "] " << expr::smessage ) );
258 
259  logging::core::get()->set_filter( logging::trivial::severity >= level );
260 }
261 
262 //--- Command main program-----------------------------------------------------
263 int main( int argc, char** argv )
264 //-----------------------------------------------------------------------------
265 {
266  init_logging( ( System::isEnvSet( "VERBOSE" ) && !System::getEnv( "VERBOSE" ).empty() )
267  ? boost::log::trivial::info
268  : boost::log::trivial::warning );
269 
270  fs::path pwd = fs::initial_path();
271  fs::path out;
272  Strings_t libs;
273  std::string pkgName;
274  std::string userModule;
275 
276  // declare a group of options that will be allowed only on command line
277  po::options_description generic( "Generic options" );
278  generic.add_options()( "help,h", "produce this help message" )(
279  "package-name,p", po::value<string>(), "name of the package for which we create the configurables file" )(
280  "input-libraries,i", po::value<string>(), "libraries to extract the component configurables from" )(
281  "input-cfg,c", po::value<string>(), "path to the cfg file holding the description of the Configurable base "
282  "classes, the python module holding the Configurable definitions, etc..." )(
283  "output-dir,o", po::value<string>()->default_value( "../genConf" ),
284  "output directory for genconf files." )( "debug-level,d", po::value<int>()->default_value( 0 ), "debug level" )(
285  "load-library,l", po::value<Strings_t>()->composing(), "preloading library" )(
286  "user-module,m", po::value<string>(), "user-defined module to be imported by the genConf-generated one" )(
287  "no-init", "do not generate the (empty) __init__.py" );
288 
289  // declare a group of options that will be allowed both on command line
290  // _and_ in configuration file
291  po::options_description config( "Configuration" );
292  config.add_options()( "configurable-module", po::value<string>()->default_value( "AthenaCommon" ),
293  "Name of the module holding the configurable classes" )(
294  "configurable-default-name", po::value<string>()->default_value( "Configurable.DefaultName" ),
295  "Default name for the configurable instance" )( "configurable-algorithm",
296  po::value<string>()->default_value( "ConfigurableAlgorithm" ),
297  "Name of the configurable base class for Algorithm components" )(
298  "configurable-algtool", po::value<string>()->default_value( "ConfigurableAlgTool" ),
299  "Name of the configurable base class for AlgTool components" )(
300  "configurable-auditor", po::value<string>()->default_value( "ConfigurableAuditor" ),
301  "Name of the configurable base class for Auditor components" )(
302  "configurable-service", po::value<string>()->default_value( "ConfigurableService" ),
303  "Name of the configurable base class for Service components" );
304 
305  po::options_description cmdline_options;
306  cmdline_options.add( generic ).add( config );
307 
308  po::options_description config_file_options;
309  config_file_options.add( config );
310 
311  po::options_description visible( "Allowed options" );
312  visible.add( generic ).add( config );
313 
314  po::variables_map vm;
315 
316  try {
317  po::store( po::command_line_parser( argc, argv ).options( cmdline_options ).run(), vm );
318 
319  po::notify( vm );
320 
321  // try to read configuration from the optionally given configuration file
322  if ( vm.count( "input-cfg" ) ) {
323  string cfgFileName = vm["input-cfg"].as<string>();
324  cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
325  std::ifstream ifs( cfgFileName );
326  po::store( parse_config_file( ifs, config_file_options ), vm );
327  }
328 
329  po::notify( vm );
330  } catch ( po::error& err ) {
331  LOG_ERROR << "error detected while parsing command options: " << err.what();
332  return EXIT_FAILURE;
333  }
334 
335  //--- Process command options -----------------------------------------------
336  if ( vm.count( "help" ) ) {
337  cout << visible << endl;
338  return EXIT_FAILURE;
339  }
340 
341  if ( vm.count( "package-name" ) ) {
342  pkgName = vm["package-name"].as<string>();
343  } else {
344  LOG_ERROR << "'package-name' required";
345  cout << visible << endl;
346  return EXIT_FAILURE;
347  }
348 
349  if ( vm.count( "user-module" ) ) {
350  userModule = vm["user-module"].as<string>();
351  LOG_INFO << "INFO: will import user module " << userModule;
352  }
353 
354  if ( vm.count( "input-libraries" ) ) {
355  // re-shape the input arguments:
356  // - removes spurious spaces,
357  // - split into tokens.
358  Strings_t inputLibs;
359  {
360  std::string tmp = vm["input-libraries"].as<std::string>();
361  boost::trim( tmp );
362  boost::split( inputLibs, tmp, boost::is_any_of( " " ), boost::token_compress_on );
363  }
364 
365  //
366  libs.reserve( inputLibs.size() );
367  for ( const auto& iLib : inputLibs ) {
368  std::string lib = fs::path( iLib ).stem().string();
369  if ( lib.compare( 0, 3, "lib" ) == 0 ) {
370  lib = lib.substr( 3 ); // For *NIX remove "lib"
371  }
372  // remove duplicates
373  if ( !lib.empty() && std::find( libs.begin(), libs.end(), lib ) == libs.end() ) {
374  libs.push_back( lib );
375  }
376  } //> end loop over input-libraries
377  if ( libs.empty() ) {
378  LOG_ERROR << "input component library(ies) required !\n";
379  LOG_ERROR << "'input-libraries' argument was [" << vm["input-libraries"].as<string>() << "]";
380  return EXIT_FAILURE;
381  }
382  } else {
383  LOG_ERROR << "input component library(ies) required";
384  cout << visible << endl;
385  return EXIT_FAILURE;
386  }
387 
388  if ( vm.count( "output-dir" ) ) {
389  out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) );
390  }
391 
392  if ( vm.count( "debug-level" ) ) {
393  Gaudi::PluginService::SetDebug( vm["debug-level"].as<int>() );
394  }
395 
396  if ( vm.count( "load-library" ) ) {
397  for ( const auto& lLib : vm["load-library"].as<Strings_t>() ) {
398  // load done through Gaudi helper class
399  System::ImageHandle tmp; // we ignore the library handle
400  unsigned long err = System::loadDynamicLib( lLib, &tmp );
401  if ( err != 1 ) LOG_WARNING << "failed to load: " << lLib;
402  }
403  }
404 
405  if ( !fs::exists( out ) ) {
406  try {
407  fs::create_directory( out );
408  } catch ( fs::filesystem_error& err ) {
409  LOG_ERROR << "error creating directory: " << err.what();
410  return EXIT_FAILURE;
411  }
412  }
413 
414  {
416  msg << ":::::: libraries : [ ";
417  std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
418  msg << "] ::::::";
419  LOG_INFO << msg.str();
420  }
421 
422  configGenerator py( pkgName, out.string() );
423  py.setConfigurableModule( vm["configurable-module"].as<string>() );
424  py.setConfigurableDefaultName( vm["configurable-default-name"].as<string>() );
425  py.setConfigurableAlgorithm( vm["configurable-algorithm"].as<string>() );
426  py.setConfigurableAlgTool( vm["configurable-algtool"].as<string>() );
427  py.setConfigurableAuditor( vm["configurable-auditor"].as<string>() );
428  py.setConfigurableService( vm["configurable-service"].as<string>() );
429 
430  int sc = EXIT_FAILURE;
431  try {
432  sc = py.genConfig( libs, userModule );
433  } catch ( exception& e ) {
434  cout << "ERROR: Could not generate Configurable(s) !\n"
435  << "ERROR: Got exception: " << e.what() << endl;
436  return EXIT_FAILURE;
437  }
438 
439  if ( EXIT_SUCCESS == sc && !vm.count( "no-init" ) ) {
440  // create an empty __init__.py file in the output dir
441  std::fstream initPy( ( out / fs::path( "__init__.py" ) ).string(), std::ios_base::out | std::ios_base::trunc );
442  initPy << "## Hook for " << pkgName << " genConf module\n" << flush;
443  }
444 
445  {
447  msg << ":::::: libraries : [ ";
448  std::copy( libs.begin(), libs.end(), std::ostream_iterator<std::string>( msg, " " ) );
449  msg << "] :::::: [DONE]";
450  LOG_INFO << msg.str();
451  }
452  return sc;
453 }
454 
455 //-----------------------------------------------------------------------------
456 int configGenerator::genConfig( const Strings_t& libs, const string& userModule )
457 //-----------------------------------------------------------------------------
458 {
459  //--- Disable checking StatusCode -------------------------------------------
461 
462  const auto endLib = libs.end();
463 
464  static const std::string gaudiSvc = "GaudiCoreSvc";
465  const bool isGaudiSvc = ( std::find( libs.begin(), endLib, gaudiSvc ) != endLib );
466 
467  //--- Instantiate ApplicationMgr --------------------------------------------
468  if ( !isGaudiSvc && createAppMgr() ) {
469  cout << "ERROR: ApplicationMgr can not be created. Check environment" << endl;
470  return EXIT_FAILURE;
471  }
472 
473  //--- Iterate over component factories --------------------------------------
474  using Gaudi::PluginService::Details::Registry;
475  const Registry& registry = Registry::instance();
476 
477  auto bkgNames = registry.loadedFactoryNames();
478 
479  ISvcLocator* svcLoc = Gaudi::svcLocator();
480  IInterface* dummySvc = new Service( "DummySvc", svcLoc );
481  dummySvc->addRef();
482 
483  bool allGood = true;
484 
485  // iterate over all the requested libraries
486  for ( const auto& iLib : libs ) {
487 
488  LOG_INFO << ":::: processing library: " << iLib << "...";
489 
490  // reset state
491  m_importGaudiHandles = false;
492  m_importDataObjectHandles = false;
493  m_importDataHandles = false;
494  m_pyBuf.str( "" );
495  m_dbBuf.str( "" );
496 
497  //--- Load component library ----------------------------------------------
498  System::ImageHandle handle;
499  unsigned long err = System::loadDynamicLib( iLib, &handle );
500  if ( err != 1 ) {
502  allGood = false;
503  continue;
504  }
505 
506  const auto& factories = registry.factories();
507  for ( const auto& factoryName : registry.loadedFactoryNames() ) {
508  if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
510  LOG_INFO << "\t==> skipping [" << factoryName << "]...";
511  }
512  continue;
513  }
514  auto entry = factories.find( factoryName );
515  if ( entry == end( factories ) ) {
516  LOG_ERROR << "inconsistency in component factories list: I cannot find anymore " << factoryName;
517  continue;
518  }
519  const auto& info = entry->second;
520  if ( !info.is_set() ) continue;
521 
522  // do not generate configurables for the Reflex-compatible aliases
523  if ( !info.getprop( "ReflexName" ).empty() ) continue;
524 
525  // Atlas contributed code (patch #1247)
526  // Skip the generation of configurables if the component does not come
527  // from the same library we are processing (i.e. we found a symbol that
528  // is coming from a library loaded by the linker).
529  if ( libNativeName( iLib ) != info.library ) {
530  LOG_WARNING << "library [" << iLib << "] exposes factory [" << factoryName << "] which is declared in ["
531  << info.library << "] !!";
532  continue;
533  }
534 
535  component_t type = component_t::Unknown;
536  {
537  const auto ft = allowedFactories.find( info.factory.type().name() );
538  if ( ft != allowedFactories.end() ) {
539  type = ft->second;
540  } else if ( factoryName == "ApplicationMgr" ) {
541  type = component_t::ApplicationMgr;
542  } else
543  continue;
544  }
545 
546  // handle possible problems with templated components
547  std::string name = boost::trim_copy( factoryName );
548 
549  const auto className = info.getprop( "ClassName" );
550  LOG_INFO << " - component: " << className << " (" << ( className != name ? ( name + ": " ) : std::string() )
551  << type << ")";
552 
553  string cname = "DefaultName";
554  SmartIF<IProperty> prop;
555  try {
556  switch ( type ) {
557  case component_t::Algorithm:
558  prop = SmartIF<IAlgorithm>( Algorithm::Factory::create( factoryName, cname, svcLoc ).release() );
559  break;
560  case component_t::Service:
561  prop = SmartIF<IService>( Service::Factory::create( factoryName, cname, svcLoc ).release() );
562  break;
563  case component_t::AlgTool:
564  prop =
565  SmartIF<IAlgTool>( AlgTool::Factory::create( factoryName, cname, toString( type ), dummySvc ).release() );
566  // FIXME: AlgTool base class increase artificially by 1 the refcount.
567  prop->release();
568  break;
569  case component_t::Auditor:
570  prop = SmartIF<IAuditor>( Auditor::Factory::create( factoryName, cname, svcLoc ).release() );
571  break;
572  case component_t::ApplicationMgr:
573  prop = SmartIF<ISvcLocator>( svcLoc );
574  break;
575  default:
576  continue; // unknown
577  }
578  } catch ( exception& e ) {
579  LOG_ERROR << "Error instantiating " << name << " from " << iLib;
580  LOG_ERROR << "Got exception: " << e.what();
581  allGood = false;
582  continue;
583  } catch ( ... ) {
584  LOG_ERROR << "Error instantiating " << name << " from " << iLib;
585  allGood = false;
586  continue;
587  }
588  if ( prop ) {
589  if ( !genComponent( iLib, name, type, prop->getProperties() ) ) {
590  allGood = false;
591  }
592  prop.reset();
593  } else {
594  LOG_ERROR << "could not cast IInterface* object to an IProperty* !";
595  LOG_ERROR << "NO Configurable will be generated for [" << name << "] !";
596  allGood = false;
597  }
598  } //> end loop over factories
599 
603  const std::string pyName = ( fs::path( m_outputDirName ) / fs::path( iLib + "Conf.py" ) ).string();
604  const std::string dbName = ( fs::path( m_outputDirName ) / fs::path( iLib + ".confdb" ) ).string();
605 
606  std::fstream py( pyName, std::ios_base::out | std::ios_base::trunc );
607  std::fstream db( dbName, std::ios_base::out | std::ios_base::trunc );
608 
609  genHeader( py, db );
610  if ( !userModule.empty() ) py << "from " << userModule << " import *" << endl;
611  genBody( py, db );
612  genTrailer( py, db );
613 
614  } //> end loop over libraries
615 
616  dummySvc->release();
617  dummySvc = 0;
618 
619  return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
620 }
621 
623 {
624 
625  std::string::size_type pos = 0, nxtpos = 0;
627 
628  while ( std::string::npos != pos ) {
629  // find end of module name
630  nxtpos = m_configurable[component_t::Module].find_first_of( ',', pos );
631 
632  // Prepare import string
633  mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
634  std::ostringstream import;
635  import << boost::format( frmt ) % mod;
636 
637  // append a normal import or a try/except enclosed one depending
638  // on availability of a fall-back module (next in the list)
639  if ( std::string::npos == nxtpos ) {
640  // last possible module
641  s << indent << import.str() << "\n" << flush;
642  pos = std::string::npos;
643  } else {
644  // we have a fallback for this
645  s << indent << "try:\n" << indent << py_tab << import.str() << "\n" << indent << "except ImportError:\n" << flush;
646  pos = nxtpos + 1;
647  }
648  // increase indentation level for next iteration
649  indent += py_tab;
650  }
651 }
652 
653 //-----------------------------------------------------------------------------
655 //-----------------------------------------------------------------------------
656 {
657  // python file part
658  std::string now = Gaudi::Time::current().format( true );
659  py << "#" << now //<< "\n"
660  << "\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n";
661  if ( m_importGaudiHandles ) {
662  py << "from GaudiKernel.GaudiHandles import *\n";
663  }
664 
665  if ( m_importDataObjectHandles ) {
666  py << "from GaudiKernel.DataObjectHandleBase import DataObjectHandleBase\n";
667  }
668 
669  if ( m_importDataHandles ) {
670  py << "from GaudiKernel.DataHandle import DataHandle\n";
671  }
672 
673  genImport( py, boost::format( "from %1%.Configurable import *" ) );
674 
675  // db file part
676  db << "## -*- ascii -*- \n"
677  << "# db file automatically generated by genconf on: " << now << "\n"
678  << flush;
679 }
680 //-----------------------------------------------------------------------------
682 //-----------------------------------------------------------------------------
683 {
684  // db file part
685  db << "## " << m_pkgName << "\n" << std::flush;
686 }
687 
688 //-----------------------------------------------------------------------------
689 bool configGenerator::genComponent( const std::string& libName, const std::string& componentName,
690  component_t componentType, const vector<PropertyBase*>& properties )
691 //-----------------------------------------------------------------------------
692 {
693  auto cname = pythonizeName( componentName );
694 
696  propDoc.reserve( properties.size() );
697 
698  m_pyBuf << "\nclass " << cname << "( " << m_configurable[componentType] << " ) :\n";
699  m_pyBuf << " __slots__ = { \n";
700  for ( const auto& prop : properties ) {
701  const string& pname = prop->name();
702  // Validate property name (it must be a valid Python identifier)
703  if ( !boost::regex_match( pname, pythonIdentifier ) ) {
704  std::cout << "ERROR: invalid property name \"" << pname << "\" in component " << cname
705  << " (invalid Python identifier)" << std::endl;
706  // try to make the buffer at least more or less valid python code.
707  m_pyBuf << " #ERROR-invalid identifier '" << pname << "'\n"
708  << " }\n";
709  return false;
710  }
711 
712  string pvalue, ptype;
713  pythonizeValue( prop, pvalue, ptype );
714  m_pyBuf << " '" << pname << "' : " << pvalue << ", # " << ptype << "\n";
715 
716  if ( prop->documentation() != "none" ) {
717  propDoc.emplace_back( pname, prop->documentation() + " [" + prop->ownerTypeName() + "]" );
718  }
719  }
720  m_pyBuf << " }\n";
721  m_pyBuf << " _propertyDocDct = { \n";
722  for ( const auto& prop : propDoc ) {
723  m_pyBuf << std::setw( 5 ) << "'" << prop.first << "' : "
724  << "\"\"\" " << prop.second << " \"\"\",\n";
725  }
726  m_pyBuf << " }\n";
727 
728  m_pyBuf << " def __init__(self, name = " << m_configurable[component_t::DefaultName] << ", **kwargs):\n"
729  << " super(" << cname << ", self).__init__(name)\n"
730  << " for n,v in kwargs.items():\n"
731  << " setattr(self, n, v)\n"
732  << " def getDlls( self ):\n"
733  << " return '" << libName << "'\n"
734  << " def getType( self ):\n"
735  << " return '" << componentName << "'\n"
736  << " pass # class " << cname << "\n"
737  << flush;
738 
739  // name of the auto-generated module
740  const string pyName = ( fs::path( m_outputDirName ) / fs::path( libName + "Conf.py" ) ).string();
741  const string modName = fs::basename( fs::path( pyName ).leaf() );
742 
743  // now the db part
744  m_dbBuf << m_pkgName << "." << modName << " " << libName << " " << cname << "\n" << flush;
745 
746  return true;
747 }
748 
749 //-----------------------------------------------------------------------------
750 void configGenerator::pythonizeValue( const PropertyBase* p, string& pvalue, string& ptype )
751 //-----------------------------------------------------------------------------
752 {
753  const std::string cvalue = p->toString();
754  const std::type_index ti = std::type_index( *p->type_info() );
755  if ( ti == typeIndex<bool>() ) {
756  pvalue = ( cvalue == "0" || cvalue == "False" || cvalue == "false" ) ? "False" : "True";
757  ptype = "bool";
758  } else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
759  ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
760  ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ) {
761  pvalue = cvalue;
762  ptype = "int";
763  } else if ( ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
764  pvalue = cvalue + "L";
765  ptype = "long";
766  } else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
767  // forces python to handle this as a float: put a dot in there...
768  pvalue = boost::to_lower_copy( cvalue );
769  if ( pvalue == "nan" ) {
770  pvalue = "float('nan')";
771  std::cout << "WARNING: default value for [" << p->name() << "] is NaN !!" << std::endl;
772  } else if ( std::string::npos == pvalue.find( "." ) && std::string::npos == pvalue.find( "e" ) ) {
773  pvalue = cvalue + ".0";
774  }
775  ptype = "float";
776  } else if ( ti == typeIndex<string>() ) {
777  pvalue = "'" + cvalue + "'";
778  ptype = "str";
779  } else if ( ti == typeIndex<GaudiHandleBase>() ) {
780  const GaudiHandleProperty& hdl = dynamic_cast<const GaudiHandleProperty&>( *p );
781  const GaudiHandleBase& base = hdl.value();
782 
783  pvalue = base.pythonRepr();
784  ptype = "GaudiHandle";
785  m_importGaudiHandles = true;
786  } else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
787  const GaudiHandleArrayProperty& hdl = dynamic_cast<const GaudiHandleArrayProperty&>( *p );
788  const GaudiHandleArrayBase& base = hdl.value();
789 
790  pvalue = base.pythonRepr();
791  ptype = "GaudiHandleArray";
792  m_importGaudiHandles = true;
793  } else if ( ti == typeIndex<DataObjectHandleBase>() ) {
794  const DataObjectHandleProperty& hdl = dynamic_cast<const DataObjectHandleProperty&>( *p );
795  const DataObjectHandleBase& base = hdl.value();
796 
797  pvalue = base.pythonRepr();
798  ptype = "DataObjectHandleBase";
799  m_importDataObjectHandles = true;
800  } else {
801  std::ostringstream v_str;
802  v_str.setf( std::ios::showpoint ); // to correctly display floats
803  p->toStream( v_str );
804  pvalue = v_str.str();
805  ptype = "list";
806 
807  // To load the Python configuration module associated with DataHandles
808  if ( ti == typeIndex<Gaudi::DataHandleConfigurable>() ) {
809  m_importDataHandles = true;
810  }
811  }
812 }
813 
814 //-----------------------------------------------------------------------------
816 //-----------------------------------------------------------------------------
817 {
819  SmartIF<IAppMgrUI> appUI( iface );
820  auto propMgr = appUI.as<IProperty>();
821  if ( !propMgr || !appUI ) return EXIT_FAILURE;
822 
823  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
824  propMgr->setProperty( "AppName", "" ); // No initial printout message
825  propMgr->setProperty( "OutputLevel", "7" ); // No other printout messages
826  appUI->configure();
827  auto msgSvc = SmartIF<IMessageSvc>{iface}.as<IProperty>();
828  msgSvc->setProperty( "setWarning", "['DefaultName', 'PropertyHolder']" );
829  msgSvc->setProperty( "Format", "%T %0W%M" );
830  return EXIT_SUCCESS;
831 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:411
int genConfig(const Strings_t &modules, const string &userModule)
main entry point of this class:
Definition: genconf.cpp:456
T setf(T...args)
T empty(T...args)
T copy(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
The data converters are responsible to translate data from one representation into another...
Definition: IConverter.h:58
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
component_t
Definition: genconf.cpp:97
std::vector< std::string > Strings_t
Definition: genconf.cpp:82
const std::string name() const
property name
Definition: Property.h:39
list argv
Definition: gaudirun.py:235
#define LOG_ERROR
Definition: genconf.cpp:74
T endl(T...args)
static Time current()
Returns the current time.
Definition: Time.cpp:112
T left(T...args)
void setConfigurableDefaultName(const std::string &defaultName)
customize the default name for configurable instances
Definition: genconf.cpp:207
STL namespace.
std::vector< fs::path > LibPathNames_t
Definition: genconf.cpp:86
void setConfigurableAlgTool(const std::string &cfgAlgTool)
customize the configurable base class for AlgTool component
Definition: genconf.cpp:219
T end(T...args)
void * ImageHandle
Definition of an image handle.
Definition: ModuleInfo.h:31
virtual std::string toString() const =0
value -> string
STL class.
stringstream m_pyBuf
buffer of auto-generated configurables
Definition: genconf.cpp:170
virtual void toStream(std::ostream &out) const =0
value -> stream
T setw(T...args)
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
void setConfigurableModule(const std::string &moduleName)
customize the Module name where configurable base classes are defined
Definition: genconf.cpp:204
STL class.
T at(T...args)
T push_back(T...args)
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:115
int main(int argc, char **argv)
Definition: genconf.cpp:263
GAUDI_API ISvcLocator * svcLocator()
GAUDIPS_API Logger & logger()
Return the current logger instance.
T what(T...args)
#define LOG_INFO
Definition: genconf.cpp:76
Definition of the basic interface.
Definition: IInterface.h:277
STL class.
const GaudiHandleBase & value() const
Definition: Property.h:984
void genHeader(std::ostream &pyOut, std::ostream &dbOut)
Definition: genconf.cpp:654
T str(T...args)
const GaudiHandleArrayBase & value() const
Definition: Property.h:1024
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:34
stringstream m_dbBuf
buffer of generated configurables informations for the "Db" file The "Db" file is holding information...
Definition: genconf.cpp:182
Converter base class.
Definition: Converter.h:24
configGenerator(const string &pkgName, const string &outputDirName)
Definition: genconf.cpp:192
STL class.
std::string pythonRepr() const override
Python representation of array of handles, i.e.
Definition: GaudiHandle.cpp:86
void setConfigurableAlgorithm(const std::string &cfgAlgorithm)
customize the configurable base class for Algorithm component
Definition: genconf.cpp:213
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:433
T flush(T...args)
void init_logging(boost::log::trivial::severity_level level)
Definition: genconf.cpp:249
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
T find(T...args)
T size(T...args)
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:354
string m_outputDirName
absolute path to the directory where genconf will store auto-generated files (Configurables and Confi...
Definition: genconf.cpp:167
void pythonizeValue(const PropertyBase *prop, string &pvalue, string &ptype)
handle the "marshalling" of Properties
Definition: genconf.cpp:750
virtual unsigned long release()=0
Release Interface instance.
#define LOG_WARNING
Definition: genconf.cpp:75
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
void genImport(std::ostream &s, const boost::format &frmt, std::string indent)
Definition: genconf.cpp:622
T begin(T...args)
std::string pythonRepr() const override
Python representation of handle, i.e.
Definition: GaudiHandle.cpp:53
void setConfigurableService(const std::string &cfgService)
customize the configurable base class for Service component
Definition: genconf.cpp:225
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
Definition: ModuleInfo.cpp:54
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
string s
Definition: gaudirun.py:253
STL class.
std::string pythonRepr() const override
std::map< component_t, std::string > m_configurable
Configurable customization.
Definition: genconf.cpp:189
string m_pkgName
name of the package we are processing
Definition: genconf.cpp:163
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
T substr(T...args)
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:42
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
void genTrailer(std::ostream &pyOut, std::ostream &dbOut)
Definition: genconf.cpp:681
const DataObjectHandleBase & value() const
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:294
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:94
virtual StatusCode configure()=0
Configure the job.
bool genComponent(const std::string &libName, const std::string &componentName, component_t componentType, const vector< PropertyBase * > &properties)
Definition: genconf.cpp:689
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
Base class for all services.
Definition: Service.h:36
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
int createAppMgr()
Definition: genconf.cpp:815
STL class.
void genBody(std::ostream &pyOut, std::ostream &dbOut)
Definition: genconf.cpp:236
STL class.
std::string toString(const Type &)
T compare(T...args)
const std::type_info * type_info() const
property type-info
Definition: Property.h:43
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
Definition: Time.cpp:260
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
T reserve(T...args)
std::ostream & operator<<(std::ostream &str, const GaudiAlg::ID &id)
Operator overloading for ostream.
Definition: GaudiHistoID.h:136
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:35
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:154
void setConfigurableAuditor(const std::string &cfgAuditor)
customize the configurable base class for AlgTool component
Definition: genconf.cpp:222
T emplace_back(T...args)