14 # pragma warning( disable : 4273 )
24 # pragma warning( disable : 279 )
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>
63 #include <fmt/format.h>
68 #include <type_traits>
71 #if FMT_VERSION < 80000
80 namespace po = boost::program_options;
81 namespace fs = boost::filesystem;
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 )
101 s << std::quoted( sv,
'\'' );
109 const boost::regex pythonIdentifier(
"^[a-zA-Z_][a-zA-Z0-9_]*$" );
112 enum class component_t {
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 },
135 "Auditor",
"Service",
"ApplicationMgr",
"IInterface",
136 "Converter",
"DataObject",
"Unknown" };
137 return names.
at(
static_cast<std::underlying_type_t<component_t>
>(
type ) );
142 {
"IInterface",
"IProperty",
"INamedInterface",
"IAlgorithm",
"IAlgTool",
"IService",
"IAuditor" } };
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];
157 template <
typename T>
163 #if defined( _WIN32 )
164 return libName +
".dll";
165 #elif defined( __linux ) || defined( __APPLE__ )
166 return "lib" + libName +
".so";
187 bool m_importGaudiHandles =
false;
188 bool m_importDataHandles =
false;
207 : m_pkgName( pkgName ), m_outputDirName( outputDirName ) {}
213 int genConfig(
const Strings_t& modules,
const string& userModule );
220 m_configurable[component_t::DefaultName] = defaultName;
225 m_configurable[component_t::Algorithm] = cfgAlgorithm;
236 m_configurable[component_t::Service] = cfgService;
237 m_configurable[component_t::ApplicationMgr] = cfgService;
241 bool genComponent(
const std::string& libName,
const std::string& componentName, component_t componentType,
243 const Gaudi::PluginService::Details::Registry::FactoryInfo& info );
253 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype );
260 namespace keywords = boost::log::keywords;
261 namespace expr = boost::log::expressions;
265 << logging::trivial::severity <<
"] " << expr::smessage ) );
275 ? boost::log::trivial::info
276 : boost::log::trivial::warning );
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" );
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" );
314 po::options_description cmdline_options;
315 cmdline_options.add(
generic ).add(
config );
317 po::options_description config_file_options;
318 config_file_options.add(
config );
320 po::options_description visible(
"Allowed options" );
321 visible.add(
generic ).add(
config );
323 po::variables_map vm;
326 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
331 if ( vm.count(
"input-cfg" ) ) {
332 string cfgFileName = vm[
"input-cfg"].as<
string>();
333 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
335 po::store( parse_config_file( ifs, config_file_options ), vm );
339 }
catch ( po::error& err ) {
340 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
345 if ( vm.count(
"help" ) ) {
350 if ( vm.count(
"package-name" ) ) {
351 pkgName = vm[
"package-name"].as<
string>();
358 if ( vm.count(
"user-module" ) ) {
359 userModule = vm[
"user-module"].as<
string>();
360 LOG_INFO <<
"INFO: will import user module " << userModule;
363 if ( vm.count(
"input-libraries" ) ) {
369 boost::split( libs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
371 LOG_ERROR <<
"input component library(ies) required";
376 if ( vm.count(
"output-dir" ) ) {
out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
380 if ( vm.count(
"load-library" ) ) {
381 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
385 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
389 if ( !fs::exists(
out ) ) {
391 fs::create_directory(
out );
392 }
catch ( fs::filesystem_error& err ) {
393 LOG_ERROR <<
"error creating directory: " << err.what();
400 msg <<
":::::: libraries : [ ";
414 int sc = EXIT_FAILURE;
418 cout <<
"ERROR: Could not generate Configurable(s) !\n"
419 <<
"ERROR: Got exception: " << e.
what() <<
endl;
423 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
426 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
431 msg <<
":::::: libraries : [ ";
433 msg <<
"] :::::: [DONE]";
443 const auto endLib = libs.
end();
445 static const std::string gaudiSvc =
"GaudiCoreSvc";
446 const bool isGaudiSvc =
448 return s.find( gaudiSvc ) != std::string::npos;
453 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
458 using Gaudi::PluginService::Details::Registry;
459 const Registry&
registry = Registry::instance();
461 auto bkgNames =
registry.loadedFactoryNames();
464 IInterface* dummySvc =
new Service(
"DummySvc", svcLoc );
470 for (
const auto& iLib : libs ) {
472 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
475 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
478 m_importGaudiHandles =
false;
479 m_importDataHandles =
false;
494 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
495 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
497 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
501 auto entry =
factories.find( factoryName );
503 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
506 const auto& info = entry->second;
507 if ( !info.is_set() )
continue;
510 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
516 if ( libNativeName( lib ) != info.library ) {
517 LOG_WARNING <<
"library [" << lib <<
"] exposes factory [" << factoryName <<
"] which is declared in ["
518 << info.library <<
"] !!";
522 component_t
type = component_t::Unknown;
524 const auto ft = allowedFactories.find( info.factory.type().name() );
525 if ( ft != allowedFactories.end() ) {
527 }
else if ( factoryName ==
"ApplicationMgr" ) {
528 type = component_t::ApplicationMgr;
536 const auto className = info.getprop(
"ClassName" );
540 string cname =
"DefaultName";
544 case component_t::Algorithm:
547 case component_t::Service:
550 case component_t::AlgTool:
556 case component_t::Auditor:
559 case component_t::ApplicationMgr:
566 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
571 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
576 if ( !genComponent( lib,
name,
type, prop->getProperties(), prop->getInterfaceNames(), info ) ) {
581 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
582 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
597 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
599 genTrailer( py, db );
604 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
612 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
617 std::string::size_type pos = 0, nxtpos = 0;
620 while ( std::string::npos != pos ) {
622 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
625 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
631 if ( std::string::npos == nxtpos ) {
633 s << indent <<
import.str() <<
"\n" <<
flush;
634 pos = std::string::npos;
637 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
652 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n"
654 <<
"if sys.version_info >= (3,):\n"
655 <<
" # Python 2 compatibility\n"
658 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
660 if ( m_importDataHandles ) { py <<
"from GaudiKernel.DataHandle import DataHandle\n"; }
662 genImport( py,
"from {}.Configurable import *" );
665 db <<
"## -*- ascii -*- \n"
666 <<
"# db file automatically generated by genconf on: " << now <<
"\n"
674 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
681 const Gaudi::PluginService::Details::Registry::FactoryInfo& info )
684 auto cname = pythonizeName( componentName );
685 const auto decl_loc = info.getprop(
"declaration_location" );
690 m_db2Buf <<
" '" << componentName <<
"': {\n";
691 m_db2Buf <<
" '__component_type__': '";
692 switch ( componentType ) {
693 case component_t::Algorithm:
694 m_db2Buf <<
"Algorithm";
696 case component_t::AlgTool:
697 m_db2Buf <<
"AlgTool";
699 case component_t::ApplicationMgr:
700 case component_t::Service:
701 m_db2Buf <<
"Service";
703 case component_t::Auditor:
704 m_db2Buf <<
"Auditor";
707 m_db2Buf <<
"Unknown";
709 if ( !decl_loc.empty() ) { m_db2Buf <<
"',\n '__declaration_location__': '" << decl_loc; }
710 m_db2Buf <<
"',\n '__interfaces__': (";
712 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
714 m_db2Buf <<
"),\n 'properties': {\n";
716 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
717 m_pyBuf <<
" __slots__ = { \n";
719 const string&
pname = prop->name();
721 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
722 std::cout <<
"ERROR: invalid property name \"" <<
pname <<
"\" in component " << cname
723 <<
" (invalid Python identifier)" <<
std::endl;
725 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n"
730 string pvalue, ptype;
731 pythonizeValue( prop, pvalue, ptype );
732 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
",\n";
734 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype <<
"', " << pvalue <<
", '''" << prop->documentation()
735 <<
" [" << prop->ownerTypeName() <<
"]'''";
736 auto sem = prop->semantics();
737 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
740 if ( prop->documentation() !=
"none" ) {
741 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
745 m_pyBuf <<
" _propertyDocDct = { \n";
746 for (
const auto& prop : propDoc ) {
747 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : "
748 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
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"
765 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
766 const string modName =
fs::path( pyName ).filename().stem().string();
768 m_db2Buf <<
" },\n },\n";
771 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
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>() ) {
791 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
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";
799 }
else if ( ti == typeIndex<string>() ) {
800 pvalue = quote( cvalue );
801 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
805 pvalue =
base.pythonRepr();
806 ptype =
base.pythonPropertyClassName();
807 m_importGaudiHandles =
true;
808 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
812 pvalue =
base.pythonRepr();
813 ptype =
base.pythonPropertyClassName();
814 m_importGaudiHandles =
true;
819 pvalue =
base.pythonRepr();
820 m_importDataHandles =
true;
823 v_str.
setf( std::ios::showpoint );
825 pvalue = v_str.
str();
836 if ( !propMgr || !appUI )
return EXIT_FAILURE;
840 propMgr->setProperty(
"AppName",
"" ).
ignore( );
842 propMgr->setProperty(
"OutputLevel", 7 ).
ignore( );
844 appUI->configure().ignore( );
846 msgSvc->setPropertyRepr(
"setWarning",
"['DefaultName', 'PropertyHolder']" )
848 msgSvc->setProperty(
"Format",
"%T %0W%M" ).ignore( );