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