4 #pragma warning( disable : 4273 ) 14 #pragma warning( disable : 279 ) 17 #include "boost/program_options.hpp" 20 #include "boost/algorithm/string/case_conv.hpp" 21 #include "boost/algorithm/string/classification.hpp" 22 #include "boost/algorithm/string/replace.hpp" 23 #include "boost/algorithm/string/split.hpp" 24 #include "boost/algorithm/string/trim.hpp" 25 #include "boost/filesystem/convenience.hpp" 26 #include "boost/filesystem/exception.hpp" 27 #include "boost/filesystem/operations.hpp" 28 #include "boost/format.hpp" 29 #include "boost/regex.hpp" 31 #include <boost/log/core.hpp> 32 #include <boost/log/expressions.hpp> 33 #include <boost/log/trivial.hpp> 34 #include <boost/log/utility/setup/common_attributes.hpp> 35 #include <boost/log/utility/setup/console.hpp> 68 #include <type_traits> 72 namespace fs = boost::filesystem;
74 #define LOG_ERROR BOOST_LOG_TRIVIAL( error ) 75 #define LOG_WARNING BOOST_LOG_TRIVIAL( warning ) 76 #define LOG_INFO BOOST_LOG_TRIVIAL( info ) 77 #define LOG_DEBUG BOOST_LOG_TRIVIAL( debug ) 94 const boost::regex pythonIdentifier(
"^[a-zA-Z_][a-zA-Z0-9_]*$" );
112 {
typeid( Algorithm::Factory::FactoryType ).
name(), component_t::Algorithm},
113 {
typeid( Service::Factory::FactoryType ).
name(), component_t::Service},
114 {
typeid( AlgTool::Factory::FactoryType ).
name(), component_t::AlgTool},
115 {
typeid( Auditor::Factory::FactoryType ).
name(), component_t::Auditor},
121 "Auditor",
"Service",
"ApplicationMgr",
"IInterface",
122 "Converter",
"DataObject",
"Unknown"};
123 return names.
at(
static_cast<std::underlying_type_t<component_t>
>( type ) );
131 static const string in(
"<>&*,: ()." );
132 static const string out(
"__rp__s___" );
133 auto r = boost::algorithm::replace_all_copy( name,
", ",
"," );
134 for (
auto&
c : r ) {
135 auto rep = in.
find(
c );
136 if ( rep != string::npos )
c = out[rep];
141 template <
typename T>
149 #if defined( _WIN32 ) 150 return libName +
".dll";
151 #elif defined( __linux ) || defined( __APPLE__ ) 152 return "lib" + libName +
".so";
174 bool m_importGaudiHandles =
false;
175 bool m_importDataObjectHandles =
false;
176 bool m_importDataHandles =
false;
193 : m_pkgName( pkgName ), m_outputDirName( outputDirName )
201 int genConfig(
const Strings_t& modules,
const string& userModule );
209 m_configurable[component_t::DefaultName] = defaultName;
215 m_configurable[component_t::Algorithm] = cfgAlgorithm;
227 m_configurable[component_t::Service] = cfgService;
228 m_configurable[component_t::ApplicationMgr] = cfgService;
244 void pythonizeValue(
const PropertyBase* prop,
string& pvalue,
string& ptype );
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>(),
"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(
"../genConf" ),
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" ) ) {
362 boost::split( inputLibs, tmp, boost::is_any_of(
" " ), boost::token_compress_on );
367 for (
const auto& iLib : inputLibs ) {
369 if ( lib.
compare( 0, 3,
"lib" ) == 0 ) {
377 if ( libs.
empty() ) {
378 LOG_ERROR <<
"input component library(ies) required !\n";
379 LOG_ERROR <<
"'input-libraries' argument was [" << vm[
"input-libraries"].as<
string>() <<
"]";
383 LOG_ERROR <<
"input component library(ies) required";
388 if ( vm.count(
"output-dir" ) ) {
389 out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) );
392 if ( vm.count(
"debug-level" ) ) {
396 if ( vm.count(
"load-library" ) ) {
397 for (
const auto& lLib : vm[
"load-library"].as<Strings_t>() ) {
401 if ( err != 1 )
LOG_WARNING <<
"failed to load: " << lLib;
405 if ( !fs::exists( out ) ) {
407 fs::create_directory( out );
408 }
catch ( fs::filesystem_error& err ) {
409 LOG_ERROR <<
"error creating directory: " << err.what();
416 msg <<
":::::: libraries : [ ";
424 py.setConfigurableDefaultName( vm[
"configurable-default-name"].as<string>() );
425 py.setConfigurableAlgorithm( vm[
"configurable-algorithm"].as<string>() );
426 py.setConfigurableAlgTool( vm[
"configurable-algtool"].as<string>() );
427 py.setConfigurableAuditor( vm[
"configurable-auditor"].as<string>() );
428 py.setConfigurableService( vm[
"configurable-service"].as<string>() );
430 int sc = EXIT_FAILURE;
432 sc = py.genConfig( libs, userModule );
434 cout <<
"ERROR: Could not generate Configurable(s) !\n" 435 <<
"ERROR: Got exception: " << e.
what() <<
endl;
439 if ( EXIT_SUCCESS == sc && !vm.count(
"no-init" ) ) {
441 std::fstream initPy( ( out /
fs::path(
"__init__.py" ) ).
string(), std::ios_base::out | std::ios_base::trunc );
442 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
447 msg <<
":::::: libraries : [ ";
449 msg <<
"] :::::: [DONE]";
462 const auto endLib = libs.
end();
464 static const std::string gaudiSvc =
"GaudiCoreSvc";
465 const bool isGaudiSvc = (
std::find( libs.
begin(), endLib, gaudiSvc ) != endLib );
469 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
474 using Gaudi::PluginService::Details::Registry;
475 const Registry&
registry = Registry::instance();
477 auto bkgNames = registry.loadedFactoryNames();
486 for (
const auto& iLib : libs ) {
488 LOG_INFO <<
":::: processing library: " << iLib <<
"...";
491 m_importGaudiHandles =
false;
492 m_importDataObjectHandles =
false;
493 m_importDataHandles =
false;
506 const auto&
factories = registry.factories();
507 for (
const auto& factoryName : registry.loadedFactoryNames() ) {
508 if ( bkgNames.find( factoryName ) != bkgNames.end() ) {
510 LOG_INFO <<
"\t==> skipping [" << factoryName <<
"]...";
514 auto entry =
factories.find( factoryName );
516 LOG_ERROR <<
"inconsistency in component factories list: I cannot find anymore " << factoryName;
519 const auto& info = entry->second;
520 if ( !info.is_set() )
continue;
523 if ( !info.getprop(
"ReflexName" ).empty() )
continue;
529 if ( libNativeName( iLib ) != info.library ) {
530 LOG_WARNING <<
"library [" << iLib <<
"] exposes factory [" << factoryName <<
"] which is declared in [" 531 << info.library <<
"] !!";
537 const auto ft = allowedFactories.find( info.factory.type().name() );
538 if ( ft != allowedFactories.end() ) {
540 }
else if ( factoryName ==
"ApplicationMgr" ) {
541 type = component_t::ApplicationMgr;
549 const auto className = info.getprop(
"ClassName" );
550 LOG_INFO <<
" - component: " << className <<
" (" << ( className != name ? ( name +
": " ) :
std::string() )
553 string cname =
"DefaultName";
557 case component_t::Algorithm:
558 prop =
SmartIF<IAlgorithm>( Algorithm::Factory::create( factoryName, cname, svcLoc ).release() );
560 case component_t::Service:
561 prop =
SmartIF<IService>( Service::Factory::create( factoryName, cname, svcLoc ).release() );
563 case component_t::AlgTool:
569 case component_t::Auditor:
570 prop =
SmartIF<IAuditor>( Auditor::Factory::create( factoryName, cname, svcLoc ).release() );
572 case component_t::ApplicationMgr:
579 LOG_ERROR <<
"Error instantiating " << name <<
" from " << iLib;
584 LOG_ERROR <<
"Error instantiating " << name <<
" from " << iLib;
589 if ( !genComponent( iLib, name, type, prop->
getProperties() ) ) {
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 );
619 return allGood ? EXIT_SUCCESS : EXIT_FAILURE;
625 std::string::size_type pos = 0, nxtpos = 0;
628 while ( std::string::npos != pos ) {
630 nxtpos = m_configurable[component_t::Module].find_first_of(
',', pos );
633 mod = m_configurable[component_t::Module].
substr( pos, nxtpos - pos );
639 if ( std::string::npos == nxtpos ) {
641 s << indent <<
import.str() <<
"\n" <<
flush;
642 pos = std::string::npos;
645 s << indent <<
"try:\n" << indent << py_tab <<
import.str() <<
"\n" << indent <<
"except ImportError:\n" <<
flush;
660 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n";
661 if ( m_importGaudiHandles ) {
662 py <<
"from GaudiKernel.GaudiHandles import *\n";
665 if ( m_importDataObjectHandles ) {
666 py <<
"from GaudiKernel.DataObjectHandleBase import DataObjectHandleBase\n";
669 if ( m_importDataHandles ) {
670 py <<
"from GaudiKernel.DataHandle import DataHandle\n";
673 genImport( py,
boost::format(
"from %1%.Configurable import *" ) );
676 db <<
"## -*- ascii -*- \n" 677 <<
"# db file automatically generated by genconf on: " << now <<
"\n" 685 db <<
"## " << m_pkgName <<
"\n" <<
std::flush;
693 auto cname = pythonizeName( componentName );
698 m_pyBuf <<
"\nclass " << cname <<
"( " << m_configurable[componentType] <<
" ) :\n";
699 m_pyBuf <<
" __slots__ = { \n";
700 for (
const auto& prop : properties ) {
701 const string&
pname = prop->name();
703 if ( !boost::regex_match( pname, pythonIdentifier ) ) {
704 std::cout <<
"ERROR: invalid property name \"" << pname <<
"\" in component " << cname
705 <<
" (invalid Python identifier)" <<
std::endl;
707 m_pyBuf <<
" #ERROR-invalid identifier '" << pname <<
"'\n" 712 string pvalue, ptype;
713 pythonizeValue( prop, pvalue, ptype );
714 m_pyBuf <<
" '" << pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
716 if ( prop->documentation() !=
"none" ) {
717 propDoc.
emplace_back( pname, prop->documentation() +
" [" + prop->ownerTypeName() +
"]" );
721 m_pyBuf <<
" _propertyDocDct = { \n";
722 for (
const auto& prop : propDoc ) {
723 m_pyBuf <<
std::setw( 5 ) <<
"'" << prop.first <<
"' : " 724 <<
"\"\"\" " << prop.second <<
" \"\"\",\n";
728 m_pyBuf <<
" def __init__(self, name = " << m_configurable[component_t::DefaultName] <<
", **kwargs):\n" 729 <<
" super(" << cname <<
", self).__init__(name)\n" 730 <<
" for n,v in kwargs.items():\n" 731 <<
" setattr(self, n, v)\n" 732 <<
" def getDlls( self ):\n" 733 <<
" return '" << libName <<
"'\n" 734 <<
" def getType( self ):\n" 735 <<
" return '" << componentName <<
"'\n" 736 <<
" pass # class " << cname <<
"\n" 740 const string pyName = (
fs::path( m_outputDirName ) /
fs::path( libName +
"Conf.py" ) ).
string();
744 m_dbBuf << m_pkgName <<
"." << modName <<
" " << libName <<
" " << cname <<
"\n" <<
flush;
755 if ( ti == typeIndex<bool>() ) {
756 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" ) ?
"False" :
"True";
758 }
else if ( ti == typeIndex<char>() || ti == typeIndex<signed char>() || ti == typeIndex<unsigned char>() ||
759 ti == typeIndex<short>() || ti == typeIndex<unsigned short>() || ti == typeIndex<int>() ||
760 ti == typeIndex<unsigned int>() || ti == typeIndex<long>() || ti == typeIndex<unsigned long>() ) {
763 }
else if ( ti == typeIndex<long long>() || ti == typeIndex<unsigned long long>() ) {
764 pvalue = cvalue +
"L";
766 }
else if ( ti == typeIndex<float>() || ti == typeIndex<double>() ) {
768 pvalue = boost::to_lower_copy( cvalue );
769 if ( pvalue ==
"nan" ) {
770 pvalue =
"float('nan')";
772 }
else if ( std::string::npos == pvalue.
find(
"." ) && std::string::npos == pvalue.
find(
"e" ) ) {
773 pvalue = cvalue +
".0";
776 }
else if ( ti == typeIndex<string>() ) {
777 pvalue =
"'" + cvalue +
"'";
779 }
else if ( ti == typeIndex<GaudiHandleBase>() ) {
784 ptype =
"GaudiHandle";
785 m_importGaudiHandles =
true;
786 }
else if ( ti == typeIndex<GaudiHandleArrayBase>() ) {
791 ptype =
"GaudiHandleArray";
792 m_importGaudiHandles =
true;
793 }
else if ( ti == typeIndex<DataObjectHandleBase>() ) {
798 ptype =
"DataObjectHandleBase";
799 m_importDataObjectHandles =
true;
802 v_str.
setf( std::ios::showpoint );
804 pvalue = v_str.
str();
808 if ( ti == typeIndex<Gaudi::DataHandleConfigurable>() ) {
809 m_importDataHandles =
true;
821 if ( !propMgr || !appUI )
return EXIT_FAILURE;
824 propMgr->setProperty(
"AppName",
"" );
825 propMgr->setProperty(
"OutputLevel",
"7" );
828 msgSvc->setProperty(
"setWarning",
"['DefaultName', 'PropertyHolder']" );
829 msgSvc->setProperty(
"Format",
"%T %0W%M" );
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
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(...)".
std::vector< std::string > Strings_t
const std::string name() const
property name
static Time current()
Returns the current time.
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
void * ImageHandle
Definition of an image handle.
virtual std::string toString() const =0
value -> string
stringstream m_pyBuf
buffer of auto-generated configurables
virtual void toStream(std::ostream &out) const =0
value -> stream
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
void setConfigurableModule(const std::string &moduleName)
customize the Module name where configurable base classes are defined
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
int main(int argc, char **argv)
GAUDI_API ISvcLocator * svcLocator()
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition of the basic interface.
const GaudiHandleBase & value() const
void genHeader(std::ostream &pyOut, std::ostream &dbOut)
const GaudiHandleArrayBase & value() const
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)
std::string pythonRepr() const override
Python representation of array of handles, i.e.
void setConfigurableAlgorithm(const std::string &cfgAlgorithm)
customize the configurable base class for Algorithm component
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
void init_logging(boost::log::trivial::severity_level level)
Base class from which all concrete algorithm classes should be derived.
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...
void pythonizeValue(const PropertyBase *prop, string &pvalue, string &ptype)
handle the "marshalling" of Properties
virtual unsigned long release()=0
Release Interface instance.
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
void genImport(std::ostream &s, const boost::format &frmt, std::string indent)
std::string pythonRepr() const override
Python representation of handle, i.e.
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.
std::string pythonRepr() const override
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)
const DataObjectHandleBase & value() const
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.
virtual StatusCode configure()=0
Configure the job.
bool genComponent(const std::string &libName, const std::string &componentName, component_t componentType, const vector< PropertyBase * > &properties)
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.
void genBody(std::ostream &pyOut, std::ostream &dbOut)
std::string toString(const Type &)
const std::type_info * type_info() const
property type-info
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
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