| The Gaudi Framework v32r1 (f65d50dc)

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