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 #if FMT_VERSION < 80000
81 namespace po = boost::program_options;
82 namespace fs = boost::filesystem;
84 #define LOG_ERROR BOOST_LOG_TRIVIAL( error )
85 #define LOG_WARNING BOOST_LOG_TRIVIAL( warning )
86 #define LOG_INFO BOOST_LOG_TRIVIAL( info )
87 #define LOG_DEBUG BOOST_LOG_TRIVIAL( debug )
102 s << std::quoted( sv,
'\'' );
110 const boost::regex pythonIdentifier(
"^[a-zA-Z_][a-zA-Z0-9_]*$" );
113 enum class component_t {
128 {
typeid( Gaudi::Algorithm::Factory::FactoryType ).
name(), component_t::Algorithm },
129 {
typeid( Service::Factory::FactoryType ).
name(), component_t::Service },
130 {
typeid( AlgTool::Factory::FactoryType ).
name(), component_t::AlgTool },
131 {
typeid( Auditor::Factory::FactoryType ).
name(), component_t::Auditor },
136 "Auditor",
"Service",
"ApplicationMgr",
"IInterface",
137 "Converter",
"DataObject",
"Unknown" };
138 return names.
at(
static_cast<std::underlying_type_t<component_t>
>(
type ) );
143 {
"IInterface",
"IProperty",
"INamedInterface",
"IAlgorithm",
"IAlgTool",
"IService",
"IAuditor" } };
148 static const string in(
"<>&*,: ()." );
149 static const string out(
"__rp__s___" );
150 auto r = boost::algorithm::replace_all_copy(
name,
", ",
"," );
151 for (
auto&
c :
r ) {
152 auto rep = in.
find(
c );
153 if ( rep != string::npos )
c =
out[rep];
158 template <
typename T>
164 #if defined( _WIN32 )
165 return libName +
".dll";
166 #elif defined( __linux ) || defined( __APPLE__ )
167 return "lib" + libName +
".so";
188 bool m_importGaudiHandles =
false;
189 bool m_importDataHandles =
false;
208 : m_pkgName( pkgName ), m_outputDirName( outputDirName ) {}
214 int genConfig(
const Strings_t& modules,
const string& userModule );
221 m_configurable[component_t::DefaultName] = defaultName;
226 m_configurable[component_t::Algorithm] = cfgAlgorithm;
237 m_configurable[component_t::Service] = cfgService;
238 m_configurable[component_t::ApplicationMgr] = cfgService;
242 bool genComponent(
const std::string& libName,
const std::string& componentName, component_t componentType,
244 const Gaudi::PluginService::Details::Registry::FactoryInfo& info );
254 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype,
string& ptype2 );
261 namespace keywords = boost::log::keywords;
262 namespace expr = boost::log::expressions;
266 << logging::trivial::severity <<
"] " << expr::smessage ) );
276 ? boost::log::trivial::info
277 : boost::log::trivial::warning );
286 po::options_description
generic(
"Generic options" );
287 generic.add_options()(
"help,h",
"produce this help message" )(
288 "package-name,p", po::value<string>(),
"name of the package for which we create the configurables file" )(
289 "input-libraries,i", po::value<string>(),
"libraries to extract the component configurables from" )(
290 "input-cfg,c", po::value<string>(),
291 "path to the cfg file holding the description of the Configurable base "
292 "classes, the python module holding the Configurable definitions, etc..." )(
293 "output-dir,o", po::value<string>()->default_value(
"../genConfDir" ),
294 "output directory for genconf files." )(
"debug-level,d", po::value<int>()->default_value( 0 ),
"debug level" )(
295 "load-library,l", po::value<Strings_t>()->composing(),
"preloading library" )(
296 "user-module,m", po::value<string>(),
"user-defined module to be imported by the genConf-generated one" )(
297 "no-init",
"do not generate the (empty) __init__.py" );
301 po::options_description
config(
"Configuration" );
302 config.add_options()(
"configurable-module", po::value<string>()->default_value(
"AthenaCommon" ),
303 "Name of the module holding the configurable classes" )(
304 "configurable-default-name", po::value<string>()->default_value(
"Configurable.DefaultName" ),
305 "Default name for the configurable instance" )(
"configurable-algorithm",
306 po::value<string>()->default_value(
"ConfigurableAlgorithm" ),
307 "Name of the configurable base class for Algorithm components" )(
308 "configurable-algtool", po::value<string>()->default_value(
"ConfigurableAlgTool" ),
309 "Name of the configurable base class for AlgTool components" )(
310 "configurable-auditor", po::value<string>()->default_value(
"ConfigurableAuditor" ),
311 "Name of the configurable base class for Auditor components" )(
312 "configurable-service", po::value<string>()->default_value(
"ConfigurableService" ),
313 "Name of the configurable base class for Service components" );
315 po::options_description cmdline_options;
316 cmdline_options.add(
generic ).add(
config );
318 po::options_description config_file_options;
319 config_file_options.add(
config );
321 po::options_description visible(
"Allowed options" );
322 visible.add(
generic ).add(
config );
324 po::variables_map vm;
327 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
332 if ( vm.count(
"input-cfg" ) ) {
333 string cfgFileName = vm[
"input-cfg"].as<
string>();
334 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
336 po::store( parse_config_file( ifs, config_file_options ), vm );
340 }
catch ( po::error& err ) {
341 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
346 if ( vm.count(
"help" ) ) {
351 if ( vm.count(
"package-name" ) ) {
352 pkgName = vm[
"package-name"].as<
string>();
359 if ( vm.count(
"user-module" ) ) {
360 userModule = vm[
"user-module"].as<
string>();
361 LOG_INFO <<
"INFO: will import user module " << userModule;
364 if ( vm.count(
"input-libraries" ) ) {
370 boost::split( libs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
372 LOG_ERROR <<
"input component library(ies) required";
377 if ( vm.count(
"output-dir" ) ) {
out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
381 if ( vm.count(
"load-library" ) ) {
382 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
386 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
390 if ( !fs::exists(
out ) ) {
392 fs::create_directory(
out );
393 }
catch ( fs::filesystem_error& err ) {
394 LOG_ERROR <<
"error creating directory: " << err.what();
401 msg <<
":::::: libraries : [ ";
415 int sc = EXIT_FAILURE;
419 cout <<
"ERROR: Could not generate Configurable(s) !\n"
420 <<
"ERROR: Got exception: " << e.
what() <<
endl;
424 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
427 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
432 msg <<
":::::: libraries : [ ";
434 msg <<
"] :::::: [DONE]";
444 const auto endLib = libs.
end();
446 static const std::string gaudiSvc =
"GaudiCoreSvc";
447 const bool isGaudiSvc =
449 return s.find( gaudiSvc ) != std::string::npos;
454 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
459 using Gaudi::PluginService::Details::Registry;
460 const Registry&
registry = Registry::instance();
462 auto bkgNames =
registry.loadedFactoryNames();
471 for (
const auto& iLib : libs ) {
473 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
476 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
479 m_importGaudiHandles =
false;
480 m_importDataHandles =
false;
495 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
496 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
498 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
502 auto entry =
factories.find( factoryName );
504 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
507 const auto& info = entry->second;
508 if ( !info.is_set() )
continue;
511 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
517 if ( libNativeName( lib ) != info.library ) {
518 LOG_WARNING <<
"library [" << lib <<
"] exposes factory [" << factoryName <<
"] which is declared in ["
519 << info.library <<
"] !!";
523 component_t
type = component_t::Unknown;
525 const auto ft = allowedFactories.find( info.factory.type().name() );
526 if ( ft != allowedFactories.end() ) {
528 }
else if ( factoryName ==
"ApplicationMgr" ) {
529 type = component_t::ApplicationMgr;
537 const auto className = info.getprop(
"ClassName" );
541 string cname =
"DefaultName";
545 case component_t::Algorithm:
548 case component_t::Service:
551 case component_t::AlgTool:
557 case component_t::Auditor:
560 case component_t::ApplicationMgr:
567 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
572 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
577 if ( !genComponent( lib,
name,
type, prop->getProperties(), prop->getInterfaceNames(), info ) ) {
582 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
583 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
598 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
600 genTrailer( py, db );
605 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
613 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
618 std::string::size_type pos = 0, nxtpos = 0;
621 while ( std::string::npos != pos ) {
623 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
626 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
632 if ( std::string::npos == nxtpos ) {
634 s << indent <<
import.str() <<
"\n" <<
flush;
635 pos = std::string::npos;
638 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
653 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n"
655 <<
"if sys.version_info >= (3,):\n"
656 <<
" # Python 2 compatibility\n"
659 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
661 if ( m_importDataHandles ) { py <<
"from GaudiKernel.DataHandle import DataHandle\n"; }
663 genImport( py,
"from {}.Configurable import *" );
666 db <<
"## -*- ascii -*- \n"
667 <<
"# db file automatically generated by genconf on: " << now <<
"\n"
675 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
682 const Gaudi::PluginService::Details::Registry::FactoryInfo& info )
685 auto cname = pythonizeName( componentName );
686 const auto decl_loc = info.getprop(
"declaration_location" );
691 m_db2Buf <<
" '" << componentName <<
"': {\n";
692 m_db2Buf <<
" '__component_type__': '";
693 switch ( componentType ) {
694 case component_t::Algorithm:
695 m_db2Buf <<
"Algorithm";
697 case component_t::AlgTool:
698 m_db2Buf <<
"AlgTool";
700 case component_t::ApplicationMgr:
701 case component_t::Service:
702 m_db2Buf <<
"Service";
704 case component_t::Auditor:
705 m_db2Buf <<
"Auditor";
708 m_db2Buf <<
"Unknown";
710 if ( !decl_loc.empty() ) { m_db2Buf <<
"',\n '__declaration_location__': '" << decl_loc; }
711 m_db2Buf <<
"',\n '__interfaces__': (";
713 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
715 m_db2Buf <<
"),\n 'properties': {\n";
717 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
718 m_pyBuf <<
" __slots__ = { \n";
720 const string&
pname = prop->name();
722 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
723 std::cout <<
"ERROR: invalid property name \"" <<
pname <<
"\" in component " << cname
724 <<
" (invalid Python identifier)" <<
std::endl;
726 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n"
731 string pvalue, ptype, ptype2;
732 pythonizeValue( prop, pvalue, ptype, ptype2 );
733 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
735 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype2 <<
"', " << pvalue <<
", '''" << prop->documentation()
736 <<
" [" << prop->ownerTypeName() <<
"]'''";
737 auto sem = prop->semantics();
738 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
741 if ( prop->documentation() !=
"none" ) {
742 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
746 m_pyBuf <<
" _propertyDocDct = { \n";
747 for (
const auto& prop : propDoc ) {
748 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : "
749 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
753 if ( !decl_loc.empty() ) { m_pyBuf <<
" __declaration_location__ = '" << decl_loc <<
"'\n"; }
754 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n"
755 <<
" super(" << cname <<
", self).__init__(name)\n"
756 <<
" for n,v in kwargs.items():\n"
757 <<
" setattr(self, n, v)\n"
758 <<
" def getDlls( self ):\n"
759 <<
" return '" << libName <<
"'\n"
760 <<
" def getType( self ):\n"
761 <<
" return '" << componentName <<
"'\n"
762 <<
" pass # class " << cname <<
"\n"
766 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
767 const string modName =
fs::path( pyName ).filename().stem().string();
769 m_db2Buf <<
" },\n },\n";
772 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
785 if ( ti == typeIndex<bool>() ) {
786 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
788 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
789 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
790 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ||
791 ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
794 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
796 pvalue = boost::to_lower_copy( cvalue );
797 if ( std::string::npos != pvalue.
find(
"nan" ) ) {
798 pvalue =
"float('nan')";
799 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
800 pvalue = cvalue +
".0";
803 }
else if ( ti == typeIndex<string>() ) {
804 pvalue = quote( cvalue );
806 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
811 ptype =
"GaudiHandle";
813 m_importGaudiHandles =
true;
814 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
819 ptype =
"GaudiHandleArray";
821 m_importGaudiHandles =
true;
827 ptype =
"DataHandle";
828 m_importDataHandles =
true;
831 v_str.
setf( std::ios::showpoint );
833 pvalue = v_str.
str();
845 if ( !propMgr || !appUI )
return EXIT_FAILURE;
849 propMgr->setProperty(
"AppName",
"" ).
ignore( );
851 propMgr->setProperty(
"OutputLevel", 7 ).
ignore( );
853 appUI->configure().ignore( );
855 msgSvc->setPropertyRepr(
"setWarning",
"['DefaultName', 'PropertyHolder']" )
857 msgSvc->setProperty(
"Format",
"%T %0W%M" ).ignore( );