4 #pragma warning ( disable : 4273 )
14 #pragma warning(disable:279)
16 #if !defined(_WIN32) && !defined(_MSC_VER)
21 #include "boost/program_options.hpp"
23 #if defined(__ICC) && !defined(_WIN32) && (_MSC_VER == 0)
28 #include "boost/filesystem/operations.hpp"
29 #include "boost/filesystem/exception.hpp"
30 #include "boost/filesystem/convenience.hpp"
31 #include "boost/algorithm/string/split.hpp"
32 #include "boost/algorithm/string/classification.hpp"
33 #include "boost/algorithm/string/trim.hpp"
34 #include "boost/algorithm/string/case_conv.hpp"
35 #include "boost/format.hpp"
36 #include "boost/regex.hpp"
54 #include "Reflex/PluginService.h"
55 #include "Reflex/Reflex.h"
56 #include "Reflex/SharedLibrary.h"
73 namespace po = boost::program_options;
74 namespace fs = boost::filesystem;
77 using namespace ROOT::Reflex;
88 const boost::regex pythonIdentifier(
"^[a-zA-Z_][a-zA-Z0-9_]*$");
122 const string& outputDirName ) :
123 m_pkgName ( pkgName ),
124 m_outputDirName ( outputDirName ),
126 m_importGaudiHandles( false ),
135 int genConfig(
const Strings_t& modules );
146 m_configurable[
"DefaultName" ] = defaultName;
152 m_configurable[
"Algorithm" ] = cfgAlgorithm;
158 m_configurable[
"AlgTool" ] = cfgAlgTool;
164 m_configurable[
"Auditor" ] = cfgAuditor;
170 m_configurable[
"Service" ] = cfgService;
171 m_configurable[
"ApplicationMgr" ] = cfgService;
183 pyOut << m_pyBuf.str() <<
flush;
184 dbOut << m_dbBuf.str() <<
flush;
190 void pythonizeValue(
const Property* prop,
195 void pythonizeName(
string& name );
210 po::options_description
generic(
"Generic options");
211 generic.add_options()
213 "produce this help message")
216 "name of the package for which we create the configurables file")
217 (
"input-libraries,i",
219 "libraries to extract the component configurables from")
222 "path to the cfg file holding the description of the Configurable base "
223 "classes, the python module holding the Configurable definitions, etc...")
225 po::value<string>()->default_value(
"../genConf"),
226 "output directory for genconf files.")
228 po::value<int>()->default_value(0),
231 po::value< Strings_t >()->composing(),
232 "preloading library")
237 po::options_description
config(
"Configuration");
239 (
"configurable-module",
240 po::value<string>()->default_value(
"AthenaCommon"),
241 "Name of the module holding the configurable classes")
242 (
"configurable-default-name",
243 po::value<string>()->default_value(
"Configurable.DefaultName"),
244 "Default name for the configurable instance")
245 (
"configurable-algorithm",
246 po::value<string>()->default_value(
"ConfigurableAlgorithm"),
247 "Name of the configurable base class for Algorithm components")
248 (
"configurable-algtool",
249 po::value<string>()->default_value(
"ConfigurableAlgTool"),
250 "Name of the configurable base class for AlgTool components")
251 (
"configurable-auditor",
252 po::value<string>()->default_value(
"ConfigurableAuditor"),
253 "Name of the configurable base class for Auditor components")
254 (
"configurable-service",
255 po::value<string>()->default_value(
"ConfigurableService"),
256 "Name of the configurable base class for Service components")
259 po::options_description cmdline_options;
260 cmdline_options.add(
generic).add(config);
262 po::options_description config_file_options;
263 config_file_options.add(config);
265 po::options_description visible(
"Allowed options");
266 visible.add(
generic).add(config);
268 po::variables_map vm;
271 po::store( po::command_line_parser(argc, argv).
272 options(cmdline_options).run(),
278 if( vm.count(
"input-cfg") ) {
279 string cfgFileName = vm[
"input-cfg"].as<
string>();
280 cfgFileName = fs::system_complete(
fs::path( cfgFileName ) ).string();
282 po::store( parse_config_file( ifs, config_file_options ), vm );
287 catch ( po::error& err ) {
288 cout <<
"ERR0R: error detected while parsing command options: "<< err.what() <<
endl;
293 if( vm.count(
"help")) {
298 if( vm.count(
"package-name") ) {
299 pkgName = vm[
"package-name"].as<
string>();
302 cout <<
"ERROR: 'package-name' required" <<
endl;
307 if( vm.count(
"input-libraries") ) {
313 string tmp = vm[
"input-libraries"].as<
string>();
315 boost::split( inputLibs, tmp,
316 boost::is_any_of(
" "),
317 boost::token_compress_on );
323 iLib != inputLibs.
end();
326 if ( 0 == lib.find(
"lib") ) {
335 if ( libs.
empty() ) {
336 cout <<
"ERROR: input component library(ies) required !\n"
337 <<
"ERROR: 'input-libraries' argument was ["
338 << vm[
"input-libraries"].as<
string>()
345 cout <<
"ERROR: input component library(ies) required" <<
endl;
350 if( vm.count(
"output-dir") ) {
351 out = fs::system_complete(
fs::path( vm[
"output-dir"].as<string>() ) );
354 if ( vm.count(
"debug-level") ) {
355 PluginService::SetDebug( vm[
"debug-level"].as<int>() );
358 if ( vm.count(
"load-library") ) {
361 lLib != lLib_list.
end();
364 SharedLibrary tmplib(*lLib) ;
370 if ( !fs::exists( out ) ) {
372 fs::create_directory(out);
374 catch ( fs::filesystem_error &err ) {
375 cout <<
"ERR0R: error creating directory: "<< err.what() <<
endl;
380 cout <<
":::::: libraries : [ ";
382 cout <<
"] ::::::" <<
endl;
386 py.setConfigurableDefaultName(vm[
"configurable-default-name"].as<string>());
387 py.setConfigurableAlgorithm (vm[
"configurable-algorithm"].as<string>());
388 py.setConfigurableAlgTool (vm[
"configurable-algtool"].as<string>());
389 py.setConfigurableAuditor (vm[
"configurable-auditor"].as<string>());
390 py.setConfigurableService (vm[
"configurable-service"].as<string>());
394 sc = py.genConfig( libs );
397 cout <<
"ERROR: Could not generate Configurable(s) !\n"
398 <<
"ERROR: Got exception: " << e.
what() <<
endl;
406 initPy <<
"## Hook for " << pkgName <<
" genConf module\n" <<
flush;
409 cout <<
":::::: libraries : [ ";
411 cout <<
"] :::::: [DONE]" <<
endl;
419 return (m.Properties().HasProperty(
"id") && (m.Properties().PropertyValue(
"id").TypeInfo() ==
typeid(
std::string))) ?
420 m.Properties().PropertyAsString(
"id") :
421 m.Properties().PropertyAsString(
"name") ;
429 #if ROOT_VERSION_CODE < ROOT_VERSION(5,21,6)
430 obj =
static_cast<T*
>(member.Invoke(dummy,args).Address());
432 member.Invoke(dummy,obj,args);
448 const bool isGaudiSvc = (
std::find( libs.
begin(), endLib, gaudiSvc ) != endLib );
452 cout <<
"ERROR: ApplicationMgr can not be created. Check environment" <<
endl;
457 Scope factories = Scope::ByName(PLUGINSVC_FACTORY_NS);
459 cout <<
"ERROR: No PluginSvc factory namespace could be found" <<
endl;
475 m_importGaudiHandles =
false;
483 for ( Member_Iterator it = factories.FunctionMember_Begin();
484 it != factories.FunctionMember_End(); ++it ) {
485 string ident =
getId(*it);
486 if ( PluginService::Debug() > 0 ) {
503 for ( Member_Iterator it = factories.FunctionMember_Begin();
504 it != factories.FunctionMember_End();
506 const string ident =
getId(*it);
507 if ( bkgNames.
find(ident) != bkgNamesEnd ) {
508 if ( PluginService::Debug() > 0 ) {
509 cout <<
"\t==> skipping [" << ident <<
"]..." <<
endl;
520 cout <<
"WARNING: library [" << *iLib <<
"] requested factory "
521 <<
"from another library ["
523 <<
" ==> [" << ident <<
"] !!"
528 const string rtype = it->TypeOf().ReturnType().Name();
531 if ( ident ==
"ApplicationMgr" ) type =
"ApplicationMgr";
532 else if ( rtype ==
"IInterface*" ) type =
"IInterface";
533 else if ( rtype ==
"IAlgorithm*" ) type =
"Algorithm";
534 else if ( rtype ==
"IService*" ) type =
"Service";
535 else if ( rtype ==
"IAlgTool*" ) type =
"AlgTool";
536 else if ( rtype ==
"IAuditor*" ) type =
"Auditor";
537 else if ( rtype ==
"IConverter*" ) type =
"Converter";
538 else if ( rtype ==
"DataObject*" ) type =
"DataObject";
539 else type =
"Unknown", known =
false;
544 cout <<
" - component: " << name <<
endl;
546 if ( type ==
"IInterface" ) {
552 if ( type ==
"Converter" || type ==
"DataObject" ) {
563 cout <<
"WARNING: Unknown (return) type [" << rtype <<
"] !!\n"
564 <<
"WARNING: component [" << ident <<
"] is skipped !"
570 string cname =
"DefaultName";
573 if ( type ==
"AlgTool" ) {
586 if ( type ==
"Algorithm" ) {
587 prop = makeInstance<IAlgorithm>(*it,
args);
589 else if ( type ==
"Service") {
590 prop = makeInstance<IService>(*it,
args);
592 else if ( type ==
"AlgTool") {
593 prop = makeInstance<IAlgTool>(*it,
args);
595 else if ( type ==
"Auditor") {
596 prop = makeInstance<IAuditor>(*it,
args);
598 else if ( type ==
"ApplicationMgr") {
603 prop = makeInstance<IInterface>(*it,
args);
607 cout <<
"ERROR: Error instantiating " << name
608 <<
" from " << *iLib <<
endl;
614 cout <<
"ERROR: Error instantiating " << name
615 <<
" from " << *iLib <<
endl;
620 if (genComponent( *iLib, name, type, prop->
getProperties() )) {
625 cout <<
"ERROR: could not cast IInterface* object to an IProperty* !\n"
626 <<
"ERROR: return type from PluginSvc is [" << rtype <<
"]...\n"
627 <<
"ERROR: NO Configurable will be generated for ["
638 fs::path(*iLib+
"Conf.py") ).
string();
640 fs::path(*iLib+
"_confDb.py") ).
string();
647 genHeader ( py, db );
649 genTrailer( py, db );
666 while ( std::string::npos != pos ){
668 nxtpos = m_configurable[
"Module"].find_first_of(
',',pos);
671 mod = m_configurable[
"Module"].substr(pos,nxtpos-pos);
677 if ( std::string::npos == nxtpos ) {
679 s << indent <<
import.str() <<
"\n" <<
flush;
680 pos = std::string::npos;
683 s << indent <<
"try:\n"
684 << indent << py_tab <<
import.str() <<
"\n"
685 << indent <<
"except ImportError:\n"
702 <<
"\"\"\"Automatically generated. DO NOT EDIT please\"\"\"\n";
703 if ( m_importGaudiHandles ) {
704 py <<
"from GaudiKernel.GaudiHandles import *\n";
707 genImport(py,
boost::format(
"from %1%.Configurable import *"));
710 db <<
"## -*- python -*- \n"
711 <<
"# db file automatically generated by genconf on: "
713 db <<
"## insulates outside world against anything bad that could happen\n"
714 <<
"## also prevents global scope pollution\n"
715 <<
"def _fillCfgDb():\n";
716 genImport(db,
boost::format(
"from %1%.ConfigurableDb import CfgDb"),py_tab);
719 << py_tab <<
"# get a handle on the repository of Configurables\n"
720 << py_tab <<
"cfgDb = CfgDb()\n"
722 << py_tab <<
"# populate the repository with informations on Configurables \n"
732 db << py_tab <<
"return #_fillCfgDb\n"
733 <<
"# fill cfgDb at module import...\n"
735 << py_tab <<
"_fillCfgDb()\n"
736 << py_tab <<
"#house cleaning...\n"
737 << py_tab <<
"del _fillCfgDb\n"
738 <<
"except Exception,err:\n"
739 << py_tab <<
"print \"Py:ConfigurableDb ERROR Problem with [%s] content!"
741 << py_tab <<
"print \"Py:ConfigurableDb ERROR\",err\n"
742 << py_tab <<
"print \"Py:ConfigurableDb ERROR ==> culprit is package ["
743 << m_pkgName <<
"] !\"\n"
755 string cname = componentName;
756 pythonizeName(cname);
759 PropertyDoc_t propDoc;
762 m_pyBuf <<
"class " << cname
763 <<
"( " << m_configurable[componentType] <<
" ) :"
765 m_pyBuf <<
" __slots__ = { \n";
767 it != properties.
end(); ++it ) {
769 const string pname = (*it)->name();
772 std::cout <<
"ERROR: invalid property name \"" << pname
773 <<
"\" in component " << cname
774 <<
" (invalid Python identifier)" <<
std::endl;
776 m_pyBuf <<
" #ERROR-invalid identifier '" << pname <<
"'\n"
781 string pvalue, ptype;
782 pythonizeValue( (*it), pvalue, ptype );
783 m_pyBuf <<
" '" << pname <<
"' : " << pvalue <<
", # " << ptype <<
"\n";
785 if ( (*it)->documentation() !=
"none" ) {
786 propDoc[
pname] = (*it)->documentation();
791 m_pyBuf <<
" _propertyDocDct = { \n";
792 for ( PropertyDoc_t::const_iterator iProp = propDoc.begin();
793 iProp != propDoc.end();
796 <<
"'" << iProp->first <<
"' : "
797 <<
"\"\"\" " << iProp->second <<
" \"\"\",\n";
802 <<
" def __init__(self, name = " << m_configurable[
"DefaultName"]
804 <<
" super(" << cname <<
", self).__init__(name)\n"
805 <<
" for n,v in kwargs.items():\n"
806 <<
" setattr(self, n, v)\n"
807 <<
" def getDlls( self ):\n"
808 <<
" return '" << libName <<
"'\n"
809 <<
" def getType( self ):\n"
810 <<
" return '" << componentName <<
"'\n"
811 <<
" pass # class " << cname <<
"\n"
815 const string pyName = (
fs::path(m_outputDirName) /
816 fs::path(libName+
"Conf.py") ).
string();
821 << py_tab <<
"cfgDb.add( configurable = '" << cname <<
"',\n"
822 << py_tab <<
" package = '" << m_pkgName <<
"',\n"
823 << py_tab <<
" module = '" << m_pkgName <<
"." << modName <<
"',\n"
824 << py_tab <<
" lib = '" << libName <<
"' )\n"
833 static string in(
"<>&*,: ().");
834 static string out(
"__rp__s___");
836 if ( in.find(*
i) != string::npos ) *
i = out[in.find(*
i)];
842 string& pvalue,
string& ptype )
847 if ( ti ==
typeid(
bool) ) {
848 pvalue = ( cvalue ==
"0" || cvalue ==
"False" || cvalue ==
"false" )
853 else if ( ti ==
typeid(
char) || ti ==
typeid(
signed char)
854 || ti ==
typeid(
unsigned char) ||
855 ti ==
typeid(short) || ti ==
typeid(
unsigned short) ||
856 ti ==
typeid(
int) || ti ==
typeid(
unsigned int) ||
857 ti ==
typeid(long) || ti ==
typeid(
unsigned long) ) {
861 else if ( ti ==
typeid(
long long) || ti ==
typeid(
unsigned long long) ) {
862 pvalue = cvalue +
"L";
865 else if ( ti ==
typeid(
float) || ti ==
typeid(double) ) {
867 pvalue = boost::to_lower_copy(cvalue);
868 if ( pvalue ==
"nan" ) {
869 pvalue =
"float('nan')";
870 std::cout <<
"WARNING: default value for ["
871 << p->
name() <<
"] is NaN !!"
873 }
else if ( std::string::npos == pvalue.find(
".") &&
874 std::string::npos == pvalue.find(
"e") ) {
875 pvalue = cvalue +
".0";
879 else if ( ti ==
typeid(
string) ) {
881 pvalue =
"'"+cvalue+
"'";
885 m_importGaudiHandles =
true;
891 ptype =
"GaudiHandle";
894 m_importGaudiHandles =
true;
900 ptype =
"GaudiHandleArray";
904 v_str.
setf(std::ios::fixed);
906 pvalue = v_str.str();
920 propMgr->setProperty(
"JobOptionsType",
"NONE" );
921 propMgr->setProperty(
"AppName",
"");
922 propMgr->setProperty(
"OutputLevel",
"7");