CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CondCore/Utilities/python/generateWrappers.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from CondCore.Utilities.payloadInspectorTemplate import *
00004 import string, os, sys
00005 
00006 
00007 def walkPlugins(a):
00008     for dir in os.listdir('.'):
00009         if(dir.find('Plugins')<0) : continue
00010         os.chdir(dir)
00011         a.act()
00012         os.chdir('../')
00013 
00014 
00015 # All those below run in CondCore/XyzPlugin directory
00016 
00017 def guessPackage() :
00018     lines = ( line for line in file('src/plugin.cc')
00019               if (line.find('CondFormats')>0 and line.find('DataRecord')<0)
00020     )
00021     _f = lines.next()
00022     _f = _f[_f.find('CondFormats/')+len('CondFormats/'):]
00023     return _f[:_f.find('/')]
00024  
00025 def guessClasses() :
00026     _ret = []
00027     lines = ( line for line in file('src/plugin.cc') if line[0:3]=='REG')
00028     for line in lines:
00029         _ret.append(line[line.find(',')+1:line.find(')')])
00030     return _ret
00031 
00032 # generate the comment in classes.h
00033 def generateClassesHeader(package):
00034     _header = '/* Condition Objects'
00035     _footer = '\n */\n\n#include "CondFormats/Common/interface/PayloadWrapper.h"\n\n'
00036     _leader = '\n * '
00037 
00038     _classes = guessClasses()
00039     
00040     _newch = file('../../CondFormats/'+package+'/src/classes_new.h','w')
00041     _newch.write(_header)
00042     for cl in _classes :
00043         _newch.write(_leader+cl)
00044     _newch.write(_footer)
00045     for line in file('../../CondFormats/'+package+'/src/classes.h'):
00046         _newch.write(line)
00047     _newch.close()
00048 
00049 def getClasses(package) :
00050     _header = 'Condition Objects'
00051     _ret = []
00052     _ch = file('../../CondFormats/'+package+'/src/classes.h')
00053     if (_ch.readline().find(_header)<0) :
00054         print 'comment header not found in '+package
00055         return _ret
00056     for line in _ch:
00057         if (line.find('*/')>0) : break
00058         _ret.append(line[3:].rstrip())
00059     _ch.close()
00060     return _ret
00061 
00062 wrapperDeclarationHeader = """
00063 // wrapper declarations
00064 namespace {
00065    struct wrappers {
00066 """
00067 wrapperDeclarationFooter = """
00068    };
00069 }
00070 """
00071 
00072 def declareCondWrappers(package):
00073     _newch = file('../../CondFormats/'+package+'/src/classes_new.h','w')
00074     for line in file('../../CondFormats/'+package+'/src/classes.h'):
00075         if (line.find('wrapper declarations')>0) : break
00076         _newch.write(line)
00077     _newch.write(wrapperDeclarationHeader)
00078     _n=0
00079     for cl in getClasses(package):
00080         _newch.write('      pool::Ptr<'+cl+' > p'+str(_n)+';\n')
00081         _newch.write('      cond::DataWrapper<'+cl+' > dw'+str(_n)+';\n')
00082         _n=_n+1
00083     _newch.write(wrapperDeclarationFooter)
00084     _newch.close()
00085 
00086 def declareXMLCondWrappers(package):
00087     _wrapperComment = '<-- wrapper declarations -->\n'
00088     _newxml = file('../../CondFormats/'+package+'/src/classes_def_new.xml','w')
00089     for line in file('../../CondFormats/'+package+'/src/classes_def.xml'):
00090         if (line.find('wrapper declarations')>0) : break
00091         _newch.write(line)
00092     _newch.write_(wrapperComment)
00093     for cl in getClasses(package):
00094         _newch.write(' <class name="pool::Ptr<'+cl+' >"/>\n')
00095         _newch.write(' <class name="cond::DataWrapper<'+cl+' >"/>\n')
00096     _newch.close()
00097 
00098 
00099 
00100 
00101 def generateBuildFile(package,classes) :
00102     f = file('plugins/BuildFile','w')
00103     f.seek(0,2)
00104     s = string.Template(buildfileTemplate)
00105     for classname in classes:
00106         f.write(s.substitute(_PACKAGE_=package, _CLASS_NAME_=classname).replace('-',''))
00107 
00108 def generateWrapper(package,classes) :
00109     s = string.Template(wrapperTemplate)
00110     for classname in classes:
00111         f = file('plugins/'+classname+'PyWrapper.cc','w')
00112         print "generating file:", f.name
00113         f.write(s.substitute(_PACKAGE_=package, _CLASS_NAME_=classname, _HEADER_FILE_=classname))
00114         f.close()
00115           
00116 
00117 def generateDict(package):
00118     os.system('cd ../../;cvs co CondFormats/'+package)
00119     declareCondWrappers(package)
00120     declareXMLCondWrappers(package)
00121