14 # pragma warning( disable : 4273 ) 24 # pragma warning( disable : 279 ) 27 #include "boost/program_options.hpp" 30 #include "boost/algorithm/string/case_conv.hpp" 31 #include "boost/algorithm/string/classification.hpp" 32 #include "boost/algorithm/string/replace.hpp" 33 #include "boost/algorithm/string/split.hpp" 34 #include "boost/algorithm/string/trim.hpp" 35 #include "boost/filesystem/convenience.hpp" 36 #include "boost/filesystem/exception.hpp" 37 #include "boost/filesystem/operations.hpp" 38 #include "boost/format.hpp" 39 #include "boost/regex.hpp" 41 #include <boost/log/core.hpp> 42 #include <boost/log/expressions.hpp> 43 #include <boost/log/trivial.hpp> 44 #include <boost/log/utility/setup/common_attributes.hpp> 45 #include <boost/log/utility/setup/console.hpp> 77 #include <type_traits> 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_]*$" );
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_importDataObjectHandles =
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;
252 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype,
string& ptype2 );
259 namespace keywords = boost::log::keywords;
260 namespace expr = boost::log::expressions;
264 << logging::trivial::severity <<
"] " << expr::smessage ) );
274 ? boost::log::trivial::info
275 : boost::log::trivial::warning );
284 po::options_description
generic(
"Generic options" );
285 generic.add_options()(
"help,h",
"produce this help message" )(
286 "package-name,p", po::value<string>(),
"name of the package for which we create the configurables file" )(
287 "input-libraries,i", po::value<string>(),
"libraries to extract the component configurables from" )(
288 "input-cfg,c", po::value<string>(),
289 "path to the cfg file holding the description of the Configurable base " 290 "classes, the python module holding the Configurable definitions, etc..." )(
291 "output-dir,o", po::value<string>()->default_value(
"../genConf" ),
292 "output directory for genconf files." )(
"debug-level,d", po::value<int>()->default_value( 0 ),
"debug level" )(
293 "load-library,l", po::value<Strings_t>()->composing(),
"preloading library" )(
294 "user-module,m", po::value<string>(),
"user-defined module to be imported by the genConf-generated one" )(
295 "no-init",
"do not generate the (empty) __init__.py" );
299 po::options_description
config(
"Configuration" );
300 config.add_options()(
"configurable-module", po::value<string>()->default_value(
"AthenaCommon" ),
301 "Name of the module holding the configurable classes" )(
302 "configurable-default-name", po::value<string>()->default_value(
"Configurable.DefaultName" ),
303 "Default name for the configurable instance" )(
"configurable-algorithm",
304 po::value<string>()->default_value(
"ConfigurableAlgorithm" ),
305 "Name of the configurable base class for Algorithm components" )(
306 "configurable-algtool", po::value<string>()->default_value(
"ConfigurableAlgTool" ),
307 "Name of the configurable base class for AlgTool components" )(
308 "configurable-auditor", po::value<string>()->default_value(
"ConfigurableAuditor" ),
309 "Name of the configurable base class for Auditor components" )(
310 "configurable-service", po::value<string>()->default_value(
"ConfigurableService" ),
311 "Name of the configurable base class for Service components" );
313 po::options_description cmdline_options;
314 cmdline_options.add(
generic ).add(
config );
316 po::options_description config_file_options;
317 config_file_options.add(
config );
319 po::options_description visible(
"Allowed options" );
320 visible.add(
generic ).add(
config );
322 po::variables_map vm;
325 po::store( po::command_line_parser(
argc,
argv ).
options( cmdline_options ).run(), vm );
330 if ( vm.count(
"input-cfg" ) ) {
331 string cfgFileName = vm[
"input-cfg"].as<
string>();
332 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).
string();
334 po::store( parse_config_file( ifs, config_file_options ), vm );
338 }
catch ( po::error& err ) {
339 LOG_ERROR <<
"error detected while parsing command options: " << err.what();
344 if ( vm.count(
"help" ) ) {
349 if ( vm.count(
"package-name" ) ) {
350 pkgName = vm[
"package-name"].as<
string>();
357 if ( vm.count(
"user-module" ) ) {
358 userModule = vm[
"user-module"].as<
string>();
359 LOG_INFO <<
"INFO: will import user module " << userModule;
362 if ( vm.count(
"input-libraries" ) ) {
370 boost::split( inputLibs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
375 for (
const auto& iLib : inputLibs ) {
377 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
383 if ( libs.
empty() ) {
384 LOG_ERROR <<
"input component library(ies) required !\n";
385 LOG_ERROR <<
"'input-libraries' argument was [" << vm[
"input-libraries"].as<
string>() <<
"]";
389 LOG_ERROR <<
"input component library(ies) required";
394 if ( vm.count(
"output-dir" ) ) { out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) ); }
398 if ( vm.count(
"load-library" ) ) {
399 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
403 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
407 if ( !fs::exists( out ) ) {
409 fs::create_directory( out );
410 }
catch ( fs::filesystem_error& err ) {
411 LOG_ERROR <<
"error creating directory: " << err.what();
418 msg <<
":::::: libraries : [ ";
426 py.setConfigurableDefaultName( vm[
"configurable-default-name"].as<string>() );
427 py.setConfigurableAlgorithm( vm[
"configurable-algorithm"].as<string>() );
428 py.setConfigurableAlgTool( vm[
"configurable-algtool"].as<string>() );
429 py.setConfigurableAuditor( vm[
"configurable-auditor"].as<string>() );
430 py.setConfigurableService( vm[
"configurable-service"].as<string>() );
432 int sc = EXIT_FAILURE;
434 sc = py.genConfig( libs, userModule );
436 cout <<
"ERROR: Could not generate Configurable(s) !\n" 437 <<
"ERROR: Got exception: " << e.
what() <<
endl;
441 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
443 std::fstream initPy( ( out /
fs::path(
"__init__.py" ) ).
string(), std::ios_base::out | std::ios_base::trunc );
444 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
449 msg <<
":::::: libraries : [ ";
451 msg <<
"] :::::: [DONE]";
464 const auto endLib = libs.
end();
466 static const std::string gaudiSvc =
"GaudiCoreSvc";
467 const bool isGaudiSvc = (
std::find( libs.
begin(), endLib, gaudiSvc ) != endLib );
471 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
476 using Gaudi::PluginService::Details::Registry;
477 const Registry&
registry = Registry::instance();
479 auto bkgNames =
registry.loadedFactoryNames();
488 for (
const auto& iLib : libs ) {
490 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
493 m_importGaudiHandles =
false;
494 m_importDataObjectHandles =
false;
509 for (
const auto& factoryName :
registry.loadedFactoryNames() ) {
510 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
512 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
516 auto entry =
factories.find( factoryName );
518 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
521 const auto& info = entry->second;
522 if ( !info.is_set() )
continue;
525 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
531 if ( libNativeName( iLib ) != info.library ) {
532 LOG_WARNING <<
"library [" << iLib <<
"] exposes factory [" << factoryName <<
"] which is declared in [" 533 << info.library <<
"] !!";
539 const auto ft = allowedFactories.find( info.factory.type().name() );
540 if ( ft != allowedFactories.end() ) {
542 }
else if ( factoryName ==
"ApplicationMgr" ) {
543 type = component_t::ApplicationMgr;
551 const auto className = info.getprop(
"ClassName" );
555 string cname =
"DefaultName";
559 case component_t::Algorithm:
560 prop =
SmartIF<IAlgorithm>( Gaudi::Algorithm::Factory::create( factoryName, cname, svcLoc ).release() );
562 case component_t::Service:
563 prop =
SmartIF<IService>( Service::Factory::create( factoryName, cname, svcLoc ).release() );
565 case component_t::AlgTool:
571 case component_t::Auditor:
572 prop =
SmartIF<IAuditor>( Auditor::Factory::create( factoryName, cname, svcLoc ).release() );
574 case component_t::ApplicationMgr:
581 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
586 LOG_ERROR <<
"Error instantiating " <<
name <<
" from " << iLib;
594 LOG_ERROR <<
"could not cast IInterface* object to an IProperty* !";
595 LOG_ERROR <<
"NO Configurable will be generated for [" <<
name <<
"] !";
606 std::fstream py( pyName, std::ios_base::out | std::ios_base::trunc );
607 std::fstream db( dbName, std::ios_base::out | std::ios_base::trunc );
610 if ( !userModule.
empty() ) py <<
"from " << userModule <<
" import *" <<
endl;
612 genTrailer( py, db );
616 std::fstream db2( db2Name, std::ios_base::out | std::ios_base::trunc );
617 db2 <<
"{\n" << m_db2Buf.str() <<
"}\n";
625 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
630 std::string::size_type pos = 0, nxtpos = 0;
633 while ( std::string::npos != pos ) {
635 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
638 mod = m_configurable[component_t::Module].substr( pos, nxtpos - pos );
644 if ( std::string::npos == nxtpos ) {
646 s << indent <<
import.str() <<
"\n" <<
flush;
647 pos = std::string::npos;
650 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
665 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n" 667 <<
"if sys.version_info >= (3,):\n" 668 <<
" # Python 2 compatibility\n" 671 if ( m_importGaudiHandles ) { py <<
"from GaudiKernel.GaudiHandles import *\n"; }
673 if ( m_importDataObjectHandles ) { py <<
"from GaudiKernel.DataObjectHandleBase import DataObjectHandleBase\n"; }
675 genImport( py,
boost::format(
"from %1%.Configurable import *" ) );
678 db <<
"## -*- ascii -*- \n" 679 <<
"# db file automatically generated by genconf on: " << now <<
"\n" 687 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
696 auto cname = pythonizeName( componentName );
701 m_db2Buf <<
" '" << componentName <<
"': {\n";
702 m_db2Buf <<
" '__component_type__': '";
703 switch ( componentType ) {
704 case component_t::Algorithm:
705 m_db2Buf <<
"Algorithm";
707 case component_t::AlgTool:
708 m_db2Buf <<
"AlgTool";
710 case component_t::ApplicationMgr:
711 case component_t::Service:
712 m_db2Buf <<
"Service";
714 case component_t::Auditor:
715 m_db2Buf <<
"Auditor";
718 m_db2Buf <<
"Unknown";
720 m_db2Buf <<
"',\n '__interfaces__': (";
722 if ( ignored_interfaces.find( intf ) ==
end( ignored_interfaces ) ) { m_db2Buf <<
'\'' << intf <<
"', "; }
724 m_db2Buf <<
"),\n 'properties': {\n";
726 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
727 m_pyBuf <<
" __slots__ = { \n";
729 const string&
pname = prop->name();
731 if ( !boost::regex_match(
pname, pythonIdentifier ) ) {
732 std::cout <<
"ERROR: invalid property name \"" <<
pname <<
"\" in component " << cname
733 <<
" (invalid Python identifier)" <<
std::endl;
735 m_pyBuf <<
" #ERROR-invalid identifier '" <<
pname <<
"'\n" 740 string pvalue, ptype, ptype2;
741 pythonizeValue( prop, pvalue, ptype, ptype2 );
742 m_pyBuf <<
" '" <<
pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
744 m_db2Buf <<
" '" <<
pname <<
"': ('" << ptype2 <<
"', " << pvalue <<
", '''" << prop->documentation()
745 <<
" [" << prop->ownerTypeName() <<
"]'''";
746 auto sem = prop->semantics();
747 if ( !sem.empty() ) { m_db2Buf <<
", '" << sem <<
'\''; }
750 if ( prop->documentation() !=
"none" ) {
751 propDoc.
emplace_back(
pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
755 m_pyBuf <<
" _propertyDocDct = { \n";
756 for (
const auto& prop : propDoc ) {
757 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : " 758 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
762 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n" 763 <<
" super(" << cname <<
", self).__init__(name)\n" 764 <<
" for n,v in kwargs.items():\n" 765 <<
" setattr(self, n, v)\n" 766 <<
" def getDlls( self ):\n" 767 <<
" return '" << libName <<
"'\n" 768 <<
" def getType( self ):\n" 769 <<
" return '" << componentName <<
"'\n" 770 <<
" pass # class " << cname <<
"\n" 774 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
777 m_db2Buf <<
" },\n },\n";
780 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
793 if ( ti == typeIndex<bool>() ) {
794 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
796 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
797 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
798 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ||
799 ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
802 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
804 pvalue = boost::to_lower_copy( cvalue );
805 if ( std::string::npos != pvalue.
find(
"nan" ) ) {
806 pvalue =
"float('nan')";
808 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
809 pvalue = cvalue +
".0";
812 }
else if ( ti == typeIndex<string>() ) {
813 pvalue = quote( cvalue );
815 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
820 ptype =
"GaudiHandle";
822 m_importGaudiHandles =
true;
823 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
828 ptype =
"GaudiHandleArray";
830 m_importGaudiHandles =
true;
831 }
else if ( ti == typeIndex<DataObjectHandleBase>() ) {
836 ptype =
"DataObjectHandleBase";
837 m_importDataObjectHandles =
true;
840 v_str.
setf( std::ios::showpoint );
842 pvalue = v_str.
str();
854 if ( !propMgr || !appUI )
return EXIT_FAILURE;
858 propMgr->setProperty(
"AppName",
"" ).
ignore( );
860 propMgr->setProperty(
"OutputLevel",
"7" ).
ignore( );
865 msgSvc->setProperty(
"setWarning",
"['DefaultName', 'PropertyHolder']" )
867 msgSvc->setProperty(
"Format",
"%T %0W%M" ).ignore( );
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
virtual std::vector< std::string > getInterfaceNames() const =0
Returns a vector of strings containing the names of all the implemented interfaces.
int genConfig(const Strings_t &modules, const string &userModule)
main entry point of this class:
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
The data converters are responsible to translate data from one representation into another.
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
const GaudiHandleArrayBase & value() const
std::vector< std::string > Strings_t
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
std::string pythonRepr() const override
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
std::string pythonPropertyClassName() const override
Name of the componentType with "Handle" appended.
static Time current()
Returns the current time.
virtual void toStream(std::ostream &out) const =0
value -> stream
void pythonizeValue(const PropertyBase *prop, string &pvalue, string &ptype, string &ptype2)
handle the "marshalling" of Properties
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
void setConfigurableDefaultName(const std::string &defaultName)
customize the default name for configurable instances
std::vector< fs::path > LibPathNames_t
void setConfigurableAlgTool(const std::string &cfgAlgTool)
customize the configurable base class for AlgTool component
const std::string name() const
property name
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
void * ImageHandle
Definition of an image handle.
std::string pythonRepr() const override
Python representation of handle, i.e.
bool genComponent(const std::string &libName, const std::string &componentName, component_t componentType, const vector< PropertyBase * > &properties, const std::vector< std::string > &interfaces)
stringstream m_pyBuf
buffer of auto-generated configurables
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
void setConfigurableModule(const std::string &moduleName)
customize the Module name where configurable base classes are defined
const std::type_info * type_info() const
property type-info
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
int main(int argc, char **argv)
GAUDI_API ISvcLocator * svcLocator()
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition of the basic interface.
void genHeader(std::ostream &pyOut, std::ostream &dbOut)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
stringstream m_dbBuf
buffer of generated configurables informations for the "Db" file The "Db" file is holding information...
configGenerator(const string &pkgName, const string &outputDirName)
void setConfigurableAlgorithm(const std::string &cfgAlgorithm)
customize the configurable base class for Algorithm component
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
const DataObjectHandleBase & value() const
void init_logging(boost::log::trivial::severity_level level)
Alias for backward compatibility.
const StatusCode & ignore() const
Ignore/check StatusCode.
Base class of array's of various gaudihandles.
string m_outputDirName
absolute path to the directory where genconf will store auto-generated files (Configurables and Confi...
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
virtual unsigned long release()=0
Release Interface instance.
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
void genImport(std::ostream &s, const boost::format &frmt, std::string indent)
void setConfigurableService(const std::string &cfgService)
customize the configurable base class for Service component
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
std::map< component_t, std::string > m_configurable
Configurable customization.
string m_pkgName
name of the package we are processing
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
static GAUDI_API void disableChecking()
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
void genTrailer(std::ostream &pyOut, std::ostream &dbOut)
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
const GaudiHandleBase & value() const
std::string pythonPropertyClassName() const override
Name of the componentType with "HandleArray" appended.
virtual StatusCode configure()=0
Configure the job.
The IProperty is the basic interface for all components which have properties that can be set or get.
Base class for all services.
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
virtual std::string toString() const =0
value -> string
void genBody(std::ostream &pyOut, std::ostream &dbOut)
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
std::ostream & operator<<(std::ostream &str, const GaudiAlg::ID &id)
Operator overloading for ostream.
Base class from which all concrete auditor classes should be derived.
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
void setConfigurableAuditor(const std::string &cfgAuditor)
customize the configurable base class for AlgTool component
T emplace_back(T... args)
std::string pythonRepr() const override
Python representation of array of handles, i.e.