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/convenience.hpp>
53 #include <boost/filesystem/exception.hpp>
54 #include <boost/filesystem/operations.hpp>
55 #include <boost/log/core.hpp>
56 #include <boost/log/expressions.hpp>
57 #include <boost/log/trivial.hpp>
58 #include <boost/log/utility/setup/common_attributes.hpp>
59 #include <boost/log/utility/setup/console.hpp>
60 #include <boost/program_options.hpp>
61 #include <boost/regex.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 {
119 {
typeid( Gaudi::Algorithm::Factory::FactoryType ).
name(), component_t::Algorithm },
120 {
typeid( Service::Factory::FactoryType ).
name(), component_t::Service },
121 {
typeid( AlgTool::Factory::FactoryType ).
name(), component_t::AlgTool },
122 {
typeid( Auditor::Factory::FactoryType ).
name(), component_t::Auditor },
127 "Auditor",
"Service",
"ApplicationMgr",
"IInterface",
128 "Converter",
"DataObject",
"Unknown" };
129 return names.
at(
static_cast<std::underlying_type_t<component_t>
>(
type ) );
134 {
"IInterface",
"IProperty",
"INamedInterface",
"IAlgorithm",
"IAlgTool",
"IService",
"IAuditor" } };
139 static const string in(
"<>&*,: ()." );
140 static const string out(
"__rp__s___" );
141 auto r = boost::algorithm::replace_all_copy(
name,
", ",
"," );
142 for (
auto&
c :
r ) {
143 auto rep = in.
find(
c );
144 if ( rep != string::npos )
c =
out[rep];
149 template <
typename T>
155 #if defined( _WIN32 )
156 return libName +
".dll";
157 #elif defined( __linux ) || defined( __APPLE__ )
158 return "lib" + libName +
".so";
179 bool m_importGaudiHandles =
false;
180 bool m_importDataHandles =
false;
199 : m_pkgName( pkgName ), m_outputDirName( outputDirName ) {}
205 int genConfig(
const Strings_t& modules,
const string& userModule );
212 m_configurable[component_t::DefaultName] = defaultName;
217 m_configurable[component_t::Algorithm] = cfgAlgorithm;
228 m_configurable[component_t::Service] = cfgService;
229 m_configurable[component_t::ApplicationMgr] = cfgService;
233 bool genComponent(
const std::string& libName,
const std::string& componentName, component_t componentType,
235 const Gaudi::PluginService::Details::Registry::FactoryInfo& info );
245 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype,
string& ptype2 );
252 namespace keywords = boost::log::keywords;
253 namespace expr = boost::log::expressions;
257 << logging::trivial::severity <<
"] " << expr::smessage ) );
267 ? boost::log::trivial::info
268 : boost::log::trivial::warning );
277 po::options_description
generic(
"Generic options" );
278 generic.add_options()(
"help,h",
"produce this help message" )(
279 "package-name,p", po::value<string>(),
"name of the package for which we create the configurables file" )(
280 "input-libraries,i", po::value<string>(),
"libraries to extract the component configurables from" )(
281 "input-cfg,c", po::value<string>(),
282 "path to the cfg file holding the description of the Configurable base "
283 "classes, the python module holding the Configurable definitions, etc..." )(
284 "output-dir,o", po::value<string>()->default_value(
"../genConfDir" ),
285 "output directory for genconf files." )(
"debug-level,d", po::value<int>()->default_value( 0 ),
"debug level" )(
286 "load-library,l", po::value<Strings_t>()->composing(),
"preloading library" )(
287 "user-module,m", po::value<string>(),
"user-defined module to be imported by the genConf-generated one" )(
288 "no-init",
"do not generate the (empty) __init__.py" );
292 po::options_description
config(
"Configuration" );
293 config.add_options()(
"configurable-module", po::value<string>()->default_value(
"AthenaCommon" ),
294 "Name of the module holding the configurable classes" )(
295 "configurable-default-name", po::value<string>()->default_value(
"Configurable.DefaultName" ),
296 "Default name for the configurable instance" )(
"configurable-algorithm",
297 po::value<string>()->default_value(
"ConfigurableAlgorithm" ),
298 "Name of the configurable base class for Algorithm components" )(
299 "configurable-algtool", po::value<string>()->default_value(
"ConfigurableAlgTool" ),
300 "Name of the configurable base class for AlgTool components" )(
301 "configurable-auditor", po::value<string>()->default_value(
"ConfigurableAuditor" ),
302 "Name of the configurable base class for Auditor components" )(
303 "configurable-service", po::value<string>()->default_value(
"ConfigurableService" ),
304 "Name of the configurable base class for Service components" );
306 po::options_description cmdline_options;
307 cmdline_options.add(
generic ).add(
config );
309 po::options_description config_file_options;
310 config_file_options.add(
config );
312 po::options_description visible(
"Allowed options" );
313 visible.add(
generic ).add(
config );
315 po::variables_map vm;
318 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
323 if ( vm.count(
"input-cfg" ) ) {
324 string cfgFileName = vm[
"input-cfg"].as<
string>();
325 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
327 po::store( parse_config_file( ifs, config_file_options ), vm );
331 }
catch ( po::error& err ) {
332 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
337 if ( vm.count(
"help" ) ) {
342 if ( vm.count(
"package-name" ) ) {
343 pkgName = vm[
"package-name"].as<
string>();
350 if ( vm.count(
"user-module" ) ) {
351 userModule = vm[
"user-module"].as<
string>();
352 LOG_INFO <<
"INFO: will import user module " << userModule;
355 if ( vm.count(
"input-libraries" ) ) {
361 boost::split( libs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
363 LOG_ERROR <<
"input component library(ies) required";
368 if ( vm.count(
"output-dir" ) ) {
out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
372 if ( vm.count(
"load-library" ) ) {
373 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
377 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
381 if ( !fs::exists(
out ) ) {
383 fs::create_directory(
out );
384 }
catch ( fs::filesystem_error& err ) {
385 LOG_ERROR <<
"error creating directory: " << err.what();
392 msg <<
":::::: libraries : [ ";
406 int sc = EXIT_FAILURE;
410 cout <<
"ERROR: Could not generate Configurable(s) !\n"
411 <<
"ERROR: Got exception: " << e.
what() <<
endl;
415 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
418 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
423 msg <<
":::::: libraries : [ ";
425 msg <<
"] :::::: [DONE]";
435 const auto endLib = libs.
end();
437 static const std::string gaudiSvc =
"GaudiCoreSvc";
438 const bool isGaudiSvc =
440 return s.find( gaudiSvc ) != std::string::npos;
445 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
450 using Gaudi::PluginService::Details::Registry;
451 const Registry&
registry = Registry::instance();
453 auto bkgNames =
registry.loadedFactoryNames();
462 for (
const auto& iLib : libs ) {
464 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
467 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
470 m_importGaudiHandles =
false;
471 m_importDataHandles =
false;
486 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
487 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
489 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
493 auto entry =
factories.find( factoryName );
495 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
498 const auto& info = entry->second;
499 if ( !info.is_set() )
continue;
502 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
508 if ( libNativeName( lib ) != info.library ) {
509 LOG_WARNING <<
"library [" << lib <<
"] exposes factory [" << factoryName <<
"] which is declared in ["
510 << info.library <<
"] !!";
514 component_t
type = component_t::Unknown;
516 const auto ft = allowedFactories.find( info.factory.type().name() );
517 if ( ft != allowedFactories.end() ) {
519 }
else if ( factoryName ==
"ApplicationMgr" ) {
520 type = component_t::ApplicationMgr;
528 const auto className = info.getprop(
"ClassName" );
532 string cname =
"DefaultName";
536 case component_t::Algorithm:
539 case component_t::Service:
542 case component_t::AlgTool:
548 case component_t::Auditor:
551 case component_t::ApplicationMgr:
558 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
563 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
568 if ( !genComponent( lib,
name,
type, prop->getProperties(), prop->getInterfaceNames(), info ) ) {
573 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
574 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
589 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
591 genTrailer( py, db );
596 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
604 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
609 std::string::size_type pos = 0, nxtpos = 0;
612 while ( std::string::npos != pos ) {
614 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
617 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
623 if ( std::string::npos == nxtpos ) {
625 s << indent <<
import.str() <<
"\n" <<
flush;
626 pos = std::string::npos;
629 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
644 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n"
646 <<
"if sys.version_info >= (3,):\n"
647 <<
" # Python 2 compatibility\n"
650 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
652 if ( m_importDataHandles ) { py <<
"from GaudiKernel.DataHandle import DataHandle\n"; }
654 genImport( py,
"from {}.Configurable import *" );
657 db <<
"## -*- ascii -*- \n"
658 <<
"# db file automatically generated by genconf on: " << now <<
"\n"
666 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
673 const Gaudi::PluginService::Details::Registry::FactoryInfo& info )
676 auto cname = pythonizeName( componentName );
677 const auto decl_loc = info.getprop(
"declaration_location" );
682 m_db2Buf <<
" '" << componentName <<
"': {\n";
683 m_db2Buf <<
" '__component_type__': '";
684 switch ( componentType ) {
685 case component_t::Algorithm:
686 m_db2Buf <<
"Algorithm";
688 case component_t::AlgTool:
689 m_db2Buf <<
"AlgTool";
691 case component_t::ApplicationMgr:
692 case component_t::Service:
693 m_db2Buf <<
"Service";
695 case component_t::Auditor:
696 m_db2Buf <<
"Auditor";
699 m_db2Buf <<
"Unknown";
701 if ( !decl_loc.empty() ) { m_db2Buf <<
"',\n '__declaration_location__': '" << decl_loc; }
702 m_db2Buf <<
"',\n '__interfaces__': (";
704 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
706 m_db2Buf <<
"),\n 'properties': {\n";
708 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
709 m_pyBuf <<
" __slots__ = { \n";
711 const string&
pname = prop->name();
713 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
714 std::cout <<
"ERROR: invalid property name \"" <<
pname <<
"\" in component " << cname
715 <<
" (invalid Python identifier)" <<
std::endl;
717 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n"
722 string pvalue, ptype, ptype2;
723 pythonizeValue( prop, pvalue, ptype, ptype2 );
724 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
726 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype2 <<
"', " << pvalue <<
", '''" << prop->documentation()
727 <<
" [" << prop->ownerTypeName() <<
"]'''";
728 auto sem = prop->semantics();
729 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
732 if ( prop->documentation() !=
"none" ) {
733 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
737 m_pyBuf <<
" _propertyDocDct = { \n";
738 for (
const auto& prop : propDoc ) {
739 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : "
740 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
744 if ( !decl_loc.empty() ) { m_pyBuf <<
" __declaration_location__ = '" << decl_loc <<
"'\n"; }
745 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n"
746 <<
" super(" << cname <<
", self).__init__(name)\n"
747 <<
" for n,v in kwargs.items():\n"
748 <<
" setattr(self, n, v)\n"
749 <<
" def getDlls( self ):\n"
750 <<
" return '" << libName <<
"'\n"
751 <<
" def getType( self ):\n"
752 <<
" return '" << componentName <<
"'\n"
753 <<
" pass # class " << cname <<
"\n"
757 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
758 const string modName = fs::basename(
fs::path( pyName ).leaf() );
760 m_db2Buf <<
" },\n },\n";
763 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
776 if ( ti == typeIndex<bool>() ) {
777 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
779 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
780 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
781 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ||
782 ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
785 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
787 pvalue = boost::to_lower_copy( cvalue );
788 if ( std::string::npos != pvalue.
find(
"nan" ) ) {
789 pvalue =
"float('nan')";
790 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
791 pvalue = cvalue +
".0";
794 }
else if ( ti == typeIndex<string>() ) {
795 pvalue = quote( cvalue );
797 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
802 ptype =
"GaudiHandle";
804 m_importGaudiHandles =
true;
805 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
810 ptype =
"GaudiHandleArray";
812 m_importGaudiHandles =
true;
818 ptype =
"DataHandle";
819 m_importDataHandles =
true;
822 v_str.
setf( std::ios::showpoint );
824 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( );