| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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