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,
244 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype,
string& ptype2 );
251 namespace keywords = boost::log::keywords;
252 namespace expr = boost::log::expressions;
256 << logging::trivial::severity <<
"] " << expr::smessage ) );
266 ? boost::log::trivial::info
267 : boost::log::trivial::warning );
276 po::options_description
generic(
"Generic options" );
277 generic.add_options()(
"help,h",
"produce this help message" )(
278 "package-name,p", po::value<string>(),
"name of the package for which we create the configurables file" )(
279 "input-libraries,i", po::value<string>(),
"libraries to extract the component configurables from" )(
280 "input-cfg,c", po::value<string>(),
281 "path to the cfg file holding the description of the Configurable base "
282 "classes, the python module holding the Configurable definitions, etc..." )(
283 "output-dir,o", po::value<string>()->default_value(
"../genConfDir" ),
284 "output directory for genconf files." )(
"debug-level,d", po::value<int>()->default_value( 0 ),
"debug level" )(
285 "load-library,l", po::value<Strings_t>()->composing(),
"preloading library" )(
286 "user-module,m", po::value<string>(),
"user-defined module to be imported by the genConf-generated one" )(
287 "no-init",
"do not generate the (empty) __init__.py" );
291 po::options_description
config(
"Configuration" );
292 config.add_options()(
"configurable-module", po::value<string>()->default_value(
"AthenaCommon" ),
293 "Name of the module holding the configurable classes" )(
294 "configurable-default-name", po::value<string>()->default_value(
"Configurable.DefaultName" ),
295 "Default name for the configurable instance" )(
"configurable-algorithm",
296 po::value<string>()->default_value(
"ConfigurableAlgorithm" ),
297 "Name of the configurable base class for Algorithm components" )(
298 "configurable-algtool", po::value<string>()->default_value(
"ConfigurableAlgTool" ),
299 "Name of the configurable base class for AlgTool components" )(
300 "configurable-auditor", po::value<string>()->default_value(
"ConfigurableAuditor" ),
301 "Name of the configurable base class for Auditor components" )(
302 "configurable-service", po::value<string>()->default_value(
"ConfigurableService" ),
303 "Name of the configurable base class for Service components" );
305 po::options_description cmdline_options;
306 cmdline_options.add(
generic ).add(
config );
308 po::options_description config_file_options;
309 config_file_options.add(
config );
311 po::options_description visible(
"Allowed options" );
312 visible.add(
generic ).add(
config );
314 po::variables_map vm;
317 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
322 if ( vm.count(
"input-cfg" ) ) {
323 string cfgFileName = vm[
"input-cfg"].as<
string>();
324 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
326 po::store( parse_config_file( ifs, config_file_options ), vm );
330 }
catch ( po::error& err ) {
331 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
336 if ( vm.count(
"help" ) ) {
341 if ( vm.count(
"package-name" ) ) {
342 pkgName = vm[
"package-name"].as<
string>();
349 if ( vm.count(
"user-module" ) ) {
350 userModule = vm[
"user-module"].as<
string>();
351 LOG_INFO <<
"INFO: will import user module " << userModule;
354 if ( vm.count(
"input-libraries" ) ) {
360 boost::split( libs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
362 LOG_ERROR <<
"input component library(ies) required";
367 if ( vm.count(
"output-dir" ) ) {
out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
371 if ( vm.count(
"load-library" ) ) {
372 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
376 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
380 if ( !fs::exists(
out ) ) {
382 fs::create_directory(
out );
383 }
catch ( fs::filesystem_error& err ) {
384 LOG_ERROR <<
"error creating directory: " << err.what();
391 msg <<
":::::: libraries : [ ";
405 int sc = EXIT_FAILURE;
409 cout <<
"ERROR: Could not generate Configurable(s) !\n"
410 <<
"ERROR: Got exception: " << e.
what() <<
endl;
414 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
417 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
422 msg <<
":::::: libraries : [ ";
424 msg <<
"] :::::: [DONE]";
434 const auto endLib = libs.
end();
436 static const std::string gaudiSvc =
"GaudiCoreSvc";
437 const bool isGaudiSvc =
439 return s.find( gaudiSvc ) != std::string::npos;
444 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
449 using Gaudi::PluginService::Details::Registry;
450 const Registry&
registry = Registry::instance();
452 auto bkgNames =
registry.loadedFactoryNames();
461 for (
const auto& iLib : libs ) {
463 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
466 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
469 m_importGaudiHandles =
false;
470 m_importDataHandles =
false;
485 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
486 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
488 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
492 auto entry =
factories.find( factoryName );
494 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
497 const auto& info = entry->second;
498 if ( !info.is_set() )
continue;
501 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
507 if ( libNativeName( lib ) != info.library ) {
508 LOG_WARNING <<
"library [" << lib <<
"] exposes factory [" << factoryName <<
"] which is declared in ["
509 << info.library <<
"] !!";
513 component_t
type = component_t::Unknown;
515 const auto ft = allowedFactories.find( info.factory.type().name() );
516 if ( ft != allowedFactories.end() ) {
518 }
else if ( factoryName ==
"ApplicationMgr" ) {
519 type = component_t::ApplicationMgr;
527 const auto className = info.getprop(
"ClassName" );
531 string cname =
"DefaultName";
535 case component_t::Algorithm:
538 case component_t::Service:
541 case component_t::AlgTool:
547 case component_t::Auditor:
550 case component_t::ApplicationMgr:
557 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
562 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
567 if ( !genComponent( lib,
name,
type, prop->getProperties(), prop->getInterfaceNames() ) ) { allGood =
false; }
570 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
571 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
586 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
588 genTrailer( py, db );
593 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
601 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
606 std::string::size_type pos = 0, nxtpos = 0;
609 while ( std::string::npos != pos ) {
611 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
614 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
620 if ( std::string::npos == nxtpos ) {
622 s << indent <<
import.str() <<
"\n" <<
flush;
623 pos = std::string::npos;
626 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
641 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n"
643 <<
"if sys.version_info >= (3,):\n"
644 <<
" # Python 2 compatibility\n"
647 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
649 if ( m_importDataHandles ) { py <<
"from GaudiKernel.DataHandle import DataHandle\n"; }
651 genImport( py,
"from {}.Configurable import *" );
654 db <<
"## -*- ascii -*- \n"
655 <<
"# db file automatically generated by genconf on: " << now <<
"\n"
663 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
672 auto cname = pythonizeName( componentName );
677 m_db2Buf <<
" '" << componentName <<
"': {\n";
678 m_db2Buf <<
" '__component_type__': '";
679 switch ( componentType ) {
680 case component_t::Algorithm:
681 m_db2Buf <<
"Algorithm";
683 case component_t::AlgTool:
684 m_db2Buf <<
"AlgTool";
686 case component_t::ApplicationMgr:
687 case component_t::Service:
688 m_db2Buf <<
"Service";
690 case component_t::Auditor:
691 m_db2Buf <<
"Auditor";
694 m_db2Buf <<
"Unknown";
696 m_db2Buf <<
"',\n '__interfaces__': (";
698 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
700 m_db2Buf <<
"),\n 'properties': {\n";
702 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
703 m_pyBuf <<
" __slots__ = { \n";
705 const string&
pname = prop->name();
707 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
708 std::cout <<
"ERROR: invalid property name \"" <<
pname <<
"\" in component " << cname
709 <<
" (invalid Python identifier)" <<
std::endl;
711 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n"
716 string pvalue, ptype, ptype2;
717 pythonizeValue( prop, pvalue, ptype, ptype2 );
718 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
720 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype2 <<
"', " << pvalue <<
", '''" << prop->documentation()
721 <<
" [" << prop->ownerTypeName() <<
"]'''";
722 auto sem = prop->semantics();
723 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
726 if ( prop->documentation() !=
"none" ) {
727 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
731 m_pyBuf <<
" _propertyDocDct = { \n";
732 for (
const auto& prop : propDoc ) {
733 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : "
734 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
738 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n"
739 <<
" super(" << cname <<
", self).__init__(name)\n"
740 <<
" for n,v in kwargs.items():\n"
741 <<
" setattr(self, n, v)\n"
742 <<
" def getDlls( self ):\n"
743 <<
" return '" << libName <<
"'\n"
744 <<
" def getType( self ):\n"
745 <<
" return '" << componentName <<
"'\n"
746 <<
" pass # class " << cname <<
"\n"
750 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
751 const string modName = fs::basename(
fs::path( pyName ).leaf() );
753 m_db2Buf <<
" },\n },\n";
756 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
769 if ( ti == typeIndex<bool>() ) {
770 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
772 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
773 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
774 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ||
775 ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
778 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
780 pvalue = boost::to_lower_copy( cvalue );
781 if ( std::string::npos != pvalue.
find(
"nan" ) ) {
782 pvalue =
"float('nan')";
784 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
785 pvalue = cvalue +
".0";
788 }
else if ( ti == typeIndex<string>() ) {
789 pvalue = quote( cvalue );
791 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
796 ptype =
"GaudiHandle";
798 m_importGaudiHandles =
true;
799 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
804 ptype =
"GaudiHandleArray";
806 m_importGaudiHandles =
true;
812 ptype =
"DataHandle";
813 m_importDataHandles =
true;
816 v_str.
setf( std::ios::showpoint );
818 pvalue = v_str.
str();
830 if ( !propMgr || !appUI )
return EXIT_FAILURE;
834 propMgr->setProperty(
"AppName",
"" ).
ignore( );
836 propMgr->setProperty(
"OutputLevel", 7 ).
ignore( );
838 appUI->configure().ignore( );
840 msgSvc->setPropertyRepr(
"setWarning",
"['DefaultName', 'PropertyHolder']" )
842 msgSvc->setProperty(
"Format",
"%T %0W%M" ).ignore( );