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