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>
61 #include <boost/tokenizer.hpp>
64 #include <fmt/format.h>
69 #include <type_traits>
72 namespace po = boost::program_options;
73 namespace fs = boost::filesystem;
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 )
93 s << std::quoted( sv,
'\'' );
101 const boost::regex pythonIdentifier(
"^[a-zA-Z_][a-zA-Z0-9_]*$" );
104 enum class component_t {
124 {
typeid( Gaudi::Algorithm::Factory::FactoryType ).
name(), component_t::Algorithm },
125 {
typeid( Service::Factory::FactoryType ).
name(), component_t::Service },
126 {
typeid( AlgTool::Factory::FactoryType ).
name(), component_t::AlgTool },
127 {
typeid( Auditor::Factory::FactoryType ).
name(), component_t::Auditor },
132 "Auditor",
"Service",
"ApplicationMgr",
"IInterface",
133 "Converter",
"DataObject",
"Unknown" };
134 return names.
at(
static_cast<std::underlying_type_t<component_t>
>(
type ) );
139 {
"IInterface",
"IProperty",
"INamedInterface",
"IAlgorithm",
"IAlgTool",
"IService",
"IAuditor" } };
144 static const string in(
"<>&*,: ()." );
145 static const string out(
"__rp__s___" );
146 auto r = boost::algorithm::replace_all_copy(
name,
", ",
"," );
147 for (
auto&
c : r ) {
148 auto rep = in.
find(
c );
149 if ( rep != string::npos )
c =
out[rep];
154 template <
typename T>
160 #if defined( _WIN32 )
161 return libName +
".dll";
162 #elif defined( __linux ) || defined( __APPLE__ )
163 return "lib" + libName +
".so";
184 bool m_importGaudiHandles =
false;
185 bool m_importDataHandles =
false;
208 : m_pkgName( pkgName ), m_outputDirName( outputDirName ) {}
214 int genConfig(
const Strings_t& modules,
const string& userModule );
224 m_configurable[component_t::DefaultName] = defaultName;
229 m_configurable[component_t::Algorithm] = cfgAlgorithm;
240 m_configurable[component_t::Service] = cfgService;
241 m_configurable[component_t::ApplicationMgr] = cfgService;
245 bool genComponent(
const std::string& libName,
const std::string& componentName, component_t componentType,
247 const Gaudi::PluginService::Details::Registry::FactoryInfo& info );
249 bool genComponent2(
const std::string& componentName, component_t componentType,
251 const Gaudi::PluginService::Details::Registry::FactoryInfo& info );
262 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype );
269 namespace keywords = boost::log::keywords;
270 namespace expr = boost::log::expressions;
274 << logging::trivial::severity <<
"] " << expr::smessage ) );
284 ? boost::log::trivial::info
285 : boost::log::trivial::warning );
294 po::options_description
generic(
"Generic options" );
295 generic.add_options()(
"help,h",
"produce this help message" )(
296 "package-name,p", po::value<string>(),
"name of the package for which we create the configurables file" )(
297 "input-libraries,i", po::value<string>(),
"libraries to extract the component configurables from" )(
298 "input-cfg,c", po::value<string>(),
299 "path to the cfg file holding the description of the Configurable base "
300 "classes, the python module holding the Configurable definitions, etc..." )(
301 "output-dir,o", po::value<string>()->default_value(
"../genConfDir" ),
302 "output directory for genconf files." )(
"debug-level,d", po::value<int>()->default_value( 0 ),
"debug level" )(
303 "load-library,l", po::value<Strings_t>()->composing(),
"preloading library" )(
304 "user-module,m", po::value<string>(),
"user-defined module to be imported by the genConf-generated one" )(
305 "no-init",
"do not generate the (empty) __init__.py" )(
306 "type", po::value<string>()->default_value(
"conf,conf2" ),
"comma-separate types of configurables to generate" );
310 po::options_description
config(
"Configuration" );
311 config.add_options()(
"configurable-module", po::value<string>()->default_value(
"AthenaCommon" ),
312 "Name of the module holding the configurable classes" )(
313 "configurable-default-name", po::value<string>()->default_value(
"Configurable.DefaultName" ),
314 "Default name for the configurable instance" )(
"configurable-algorithm",
315 po::value<string>()->default_value(
"ConfigurableAlgorithm" ),
316 "Name of the configurable base class for Algorithm components" )(
317 "configurable-algtool", po::value<string>()->default_value(
"ConfigurableAlgTool" ),
318 "Name of the configurable base class for AlgTool components" )(
319 "configurable-auditor", po::value<string>()->default_value(
"ConfigurableAuditor" ),
320 "Name of the configurable base class for Auditor components" )(
321 "configurable-service", po::value<string>()->default_value(
"ConfigurableService" ),
322 "Name of the configurable base class for Service components" );
324 po::options_description cmdline_options;
325 cmdline_options.add(
generic ).add(
config );
327 po::options_description config_file_options;
328 config_file_options.add(
config );
330 po::options_description visible(
"Allowed options" );
331 visible.add(
generic ).add(
config );
333 po::variables_map vm;
336 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
341 if ( vm.contains(
"input-cfg" ) ) {
342 string cfgFileName = vm[
"input-cfg"].as<
string>();
343 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
345 po::store( parse_config_file( ifs, config_file_options ), vm );
349 }
catch ( po::error& err ) {
350 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
355 if ( vm.contains(
"help" ) ) {
360 if ( vm.contains(
"package-name" ) ) {
361 pkgName = vm[
"package-name"].as<
string>();
368 if ( vm.contains(
"user-module" ) ) {
369 userModule = vm[
"user-module"].as<
string>();
370 LOG_INFO <<
"INFO: will import user module " << userModule;
373 if ( vm.contains(
"input-libraries" ) ) {
379 boost::split( libs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
381 LOG_ERROR <<
"input component library(ies) required";
386 if ( vm.contains(
"output-dir" ) ) {
out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
390 if ( vm.contains(
"load-library" ) ) {
391 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
395 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
400 if ( vm.contains(
"type" ) ) {
402 if (
type ==
"conf" ) {
403 confTypes.
insert( conf_t::CONF );
404 }
else if (
type ==
"conf2" ) {
405 confTypes.
insert( conf_t::CONF2 );
414 if ( !fs::exists(
out ) ) {
416 fs::create_directory(
out );
417 }
catch ( fs::filesystem_error& err ) {
418 LOG_ERROR <<
"error creating directory: " << err.what();
425 msg <<
":::::: libraries : [ ";
440 int sc = EXIT_FAILURE;
444 cout <<
"ERROR: Could not generate Configurable(s) !\n"
445 <<
"ERROR: Got exception: " << e.
what() <<
endl;
449 if ( EXIT_SUCCESS == sc && !vm.contains(
"no-init" ) ) {
452 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
457 msg <<
":::::: libraries : [ ";
459 msg <<
"] :::::: [DONE]";
469 const auto endLib = libs.
end();
471 static const std::string gaudiSvc =
"GaudiCoreSvc";
472 const bool isGaudiSvc =
474 return s.find( gaudiSvc ) != std::string::npos;
479 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
484 using Gaudi::PluginService::Details::Registry;
485 const Registry&
registry = Registry::instance();
487 auto bkgNames =
registry.loadedFactoryNames();
490 IInterface* dummySvc =
new Service(
"DummySvc", svcLoc );
496 for (
const auto& iLib : libs ) {
498 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
501 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
504 m_importGaudiHandles =
false;
505 m_importDataHandles =
false;
520 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
521 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
523 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
527 auto entry =
factories.find( factoryName );
529 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
532 const auto& info = entry->second;
533 if ( !info.is_set() )
continue;
536 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
542 if ( libNativeName( lib ) != info.library ) {
543 LOG_WARNING <<
"library [" << lib <<
"] exposes factory [" << factoryName <<
"] which is declared in ["
544 << info.library <<
"] !!";
548 component_t
type = component_t::Unknown;
550 const auto ft = allowedFactories.find( info.factory.type().name() );
551 if ( ft != allowedFactories.end() ) {
553 }
else if ( factoryName ==
"ApplicationMgr" ) {
554 type = component_t::ApplicationMgr;
562 const auto className = info.getprop(
"ClassName" );
566 string cname =
"DefaultName";
570 case component_t::Algorithm:
573 case component_t::Service:
576 case component_t::AlgTool:
582 case component_t::Auditor:
585 case component_t::ApplicationMgr:
592 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
597 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
602 if ( m_confTypes.contains( conf_t::CONF ) && !genComponent( lib,
name,
type, prop->getProperties(), info ) ) {
605 if ( m_confTypes.contains( conf_t::CONF2 ) &&
606 !genComponent2(
name,
type, prop->getProperties(), prop->getInterfaceNames(), info ) ) {
611 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
612 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
620 if ( m_confTypes.contains( conf_t::CONF ) ) {
628 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
630 genTrailer( py, db );
632 if ( m_confTypes.contains( conf_t::CONF2 ) ) {
635 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
643 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
648 std::string::size_type pos = 0, nxtpos = 0;
651 while ( std::string::npos != pos ) {
653 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
656 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
662 if ( std::string::npos == nxtpos ) {
664 s << indent <<
import.str() <<
"\n" <<
flush;
665 pos = std::string::npos;
668 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
683 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n";
685 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
687 if ( m_importDataHandles ) { py <<
"from GaudiKernel.DataHandle import DataHandle\n"; }
689 genImport( py,
"from {}.Configurable import *" );
692 db <<
"## -*- ascii -*- \n"
693 <<
"# db file automatically generated by genconf on: " << now <<
"\n"
701 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
707 const Gaudi::PluginService::Details::Registry::FactoryInfo& info )
710 auto cname = pythonizeName( componentName );
711 const auto decl_loc = info.getprop(
"declaration_location" );
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 LOG_ERROR <<
"invalid property name \"" <<
pname <<
"\" in component " << cname <<
" (invalid Python identifier)"
725 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n"
730 string pvalue, ptype;
731 pythonizeValue( prop, pvalue, ptype );
732 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
",\n";
734 if ( prop->documentation() !=
"none" ) {
735 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
739 m_pyBuf <<
" _propertyDocDct = { \n";
740 for (
const auto& prop : propDoc ) {
741 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : "
742 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
746 if ( !decl_loc.empty() ) { m_pyBuf <<
" __declaration_location__ = '" << decl_loc <<
"'\n"; }
747 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n"
748 <<
" super(" << cname <<
", self).__init__(name)\n"
749 <<
" for n,v in kwargs.items():\n"
750 <<
" setattr(self, n, v)\n"
751 <<
" def getDlls( self ):\n"
752 <<
" return '" << libName <<
"'\n"
753 <<
" def getType( self ):\n"
754 <<
" return '" << componentName <<
"'\n"
755 <<
" pass # class " << cname <<
"\n"
759 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
760 const string modName =
fs::path( pyName ).filename().stem().string();
763 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
771 const Gaudi::PluginService::Details::Registry::FactoryInfo& info )
774 m_db2Buf <<
" '" << componentName <<
"': {\n";
775 m_db2Buf <<
" '__component_type__': '";
776 switch ( componentType ) {
777 case component_t::Algorithm:
778 m_db2Buf <<
"Algorithm";
780 case component_t::AlgTool:
781 m_db2Buf <<
"AlgTool";
783 case component_t::ApplicationMgr:
784 case component_t::Service:
785 m_db2Buf <<
"Service";
787 case component_t::Auditor:
788 m_db2Buf <<
"Auditor";
791 m_db2Buf <<
"Unknown";
794 const auto decl_loc = info.getprop(
"declaration_location" );
795 if ( !decl_loc.empty() ) { m_db2Buf <<
"',\n '__declaration_location__': '" << decl_loc; }
797 m_db2Buf <<
"',\n '__interfaces__': (";
799 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
801 m_db2Buf <<
"),\n 'properties': {\n";
805 const string&
pname = prop->name();
807 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
808 LOG_ERROR <<
"invalid property name \"" <<
pname <<
"\" in component " << componentName
809 <<
" (invalid Python identifier)" <<
std::endl;
810 m_db2Buf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n";
815 string pvalue, ptype;
816 pythonizeValue( prop, pvalue, ptype );
818 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype <<
"', " << pvalue <<
", '''" << prop->documentation()
819 <<
" [" << prop->ownerTypeName() <<
"]'''";
820 auto sem = prop->semantics();
821 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
825 m_db2Buf <<
" },\n },\n";
838 if ( ti == typeIndex<bool>() ) {
839 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
840 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
841 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
842 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ||
843 ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
845 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
847 pvalue = boost::to_lower_copy( cvalue );
848 if ( std::string::npos != pvalue.
find(
"nan" ) ) {
849 pvalue =
"float('nan')";
850 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
851 pvalue = cvalue +
".0";
853 }
else if ( ti == typeIndex<string>() ) {
854 pvalue = quote( cvalue );
855 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
859 pvalue =
base.pythonRepr();
860 ptype =
base.pythonPropertyClassName();
861 m_importGaudiHandles =
true;
862 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
866 pvalue =
base.pythonRepr();
867 ptype =
base.pythonPropertyClassName();
868 m_importGaudiHandles =
true;
873 pvalue =
base.pythonRepr();
874 m_importDataHandles =
true;
877 v_str.
setf( std::ios::showpoint );
879 pvalue = v_str.
str();
890 if ( !propMgr || !appUI )
return EXIT_FAILURE;
894 propMgr->setProperty(
"AppName",
"" ).
ignore( );
896 propMgr->setProperty(
"OutputLevel", 7 ).
ignore( );
898 appUI->configure().ignore( );
900 msgSvc->setPropertyRepr(
"setWarning",
"['DefaultName', 'PropertyHolder']" )
902 msgSvc->setProperty(
"Format",
"%T %0W%M" ).ignore( );