CMS 3D CMS Logo

duplicateReflexLibrarySearch.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import optparse
5 import os
6 import re
7 import sys
8 import pprint
9 import subprocess
10 from XML2Python import xml2obj
11 import six
12 try:
13  from subprocess import getoutput
14 except:
15  from commands import getoutput
16 # These aren't all typedefs, but can sometimes make the output more
17 # readable
18 typedefsDict = \
19  {
20  # What we want <= What we have
21  'unsigned int' : ['unsignedint', 'UInt32_t', 'uint32_t'],
22  'unsigned long': ['unsignedlong'],
23  'int' : ['Int32_t'],
24  'float' : ['Float_t'],
25  'double' : ['Double_t'],
26  'char' : ['Char_t'],
27  '< ' : ['<', '&lt;'],
28  ' >' : ['>', '&gt;'],
29  ', ' : [','],
30  }
31 
32 
33 # Equivalent names for packages - lets script know that, for example,
34 # 'TrackReco' package should have objects 'reco::Track'.
35 #Ordered List to search for matched packages
36 equivDict = \
37  [
38  {'TrajectoryState' : ['TrajectoryStateOnSurface']},
39  {'TrackTriggerAssociation' : ['(TTClusterAssociationMap|TTStubAssociationMap|TTTrackAssociationMap|TrackingParticle).*Phase2TrackerDigi',
40  '(TTStub|TTCluster|TTTrack).*Phase2TrackerDigi.*TrackingParticle']},
41  {'L1TrackTrigger' : ['(TTStub|TTCluster|TTTrack).*Phase2TrackerDigi']},
42  {'L1TCalorimeterPhase2' : ['l1tp2::CaloTower.*']},
43  {'L1TCalorimeter' : ['l1t::CaloTower.*']},
44  {'GsfTracking' : ['reco::GsfTrack(Collection|).*(MomentumConstraint|VertexConstraint)', 'Trajectory.*reco::GsfTrack']},
45  {'ParallelAnalysis' : ['examples::TrackAnalysisAlgorithm']},
46  {'PatCandidates' : ['pat::PATObject','pat::Lepton', 'reco::RecoCandidate','pat::[A-Za-z]+Ref(Vector|)', 'pat::UserHolder']},
47  {'BTauReco' : ['reco::.*SoftLeptonTagInfo', 'reco::SoftLeptonProperties','reco::SecondaryVertexTagInfo','reco::IPTagInfo','reco::TemplatedSecondaryVertexTagInfo', 'reco::CATopJetProperties','reco::HTTTopJetProperties']},
48  {'CastorReco' : ['reco::CastorJet']},
49  {'JetMatching' : ['reco::JetFlavourInfo', 'reco::JetFlavour','reco::MatchedPartons']},
50  {'RecoCandidate' : ['reco::Candidate','edm::Wrapper.+edm::AssociationMap.+TrackingParticle']},
51  {'TrackingAnalysis' : ['TrackingParticle']},
52  {'Egamma' : ['reco::ElectronID']},
53  {'TopObjects' : ['reco::CATopJetProperties']},
54  {'TauReco' : ['reco::L2TauIsolationInfo','reco::RecoTauPiZero','reco::BaseTau']},
55  {'ValidationFormats' : ['PGlobalDigi::.+','PGlobalRecHit::.+']},
56  {'TrajectorySeed' : ['TrajectorySeed']},
57  {'TrackCandidate' : ['TrackCandidate']},
58  {'PatternTools' : ['MomentumConstraint','VertexConstraint','Trajectory']},
59  {'TrackerRecHit2D' : ['SiStrip(Matched|)RecHit[12]D','SiTrackerGSRecHit[12]D','SiPixelRecHit']},
60  {'MuonReco' : ['reco::Muon(Ref|)(Vector|)']},
61  {'MuonSeed' : ['L3MuonTrajectorySeed']},
62  {'HepMCCandidate' : ['reco::GenParticle.*']},
63  {'L1Trigger' : ['l1extra::L1.+Particle']},
64  {'TrackInfo' : ['reco::TrackingRecHitInfo']},
65  {'EgammaCandidates' : ['reco::GsfElectron.*','reco::Photon.*']},
66  {'HcalIsolatedTrack' : ['reco::IsolatedPixelTrackCandidate', 'reco::EcalIsolatedParticleCandidate', 'reco::HcalIsolatedTrackCandidate']},
67  {'HcalRecHit' : ['HFRecHit','HORecHit','ZDCRecHit','HBHERecHit']},
68  {'PFRootEvent' : ['EventColin::']},
69  {'CaloTowers' : ['CaloTower.*']},
70  {'GsfTrackReco' : ['GsfTrack.*']},
71  {'METReco' : ['reco::(Calo|PF|Gen|)MET','reco::PFClusterMET']},
72  {'ParticleFlowReco' : ['reco::RecoPFClusterRefCandidateRef.*']},
73  {'ParticleFlowCandidate' : ['reco::PFCandidateRef','reco::PFCandidateFwdRef','reco::PFCandidate']},
74  {'PhysicsToolsObjects' : ['PhysicsTools::Calibration']},
75  {'TrackReco' : ['reco::Track','reco::TrackRef']},
76  {'VertexReco' : ['reco::Vertex']},
77  {'TFWLiteSelectorTest' : ['tfwliteselectortest']},
78  {'TauReco' : ['reco::PFJetRef']},
79  {'JetReco' : ['reco::.*Jet','reco::.*Jet(Collection|Ref)']},
80  {'HGCDigi' : ['HGCSample']},
81  {'SiPixelObjects' : ['SiPixelQuality.*']},
82  ]
83 
84 ignoreEdmDP = {
85  'LCGReflex/__gnu_cxx::__normal_iterator<std::basic_string<char>*,std::vector<std::basic_string<char>%>%>' : 1,
86  '' : 1
87 }
88 
90  """ Searches through the requested directory looking at
91  'classes_def.xml' files looking for duplicate Reflex definitions."""
92  # compile necessary RE statements
93  classNameRE = re.compile (r'class\s+name\s*=\s*"([^"]*)"')
94  spacesRE = re.compile (r'\s+')
95  stdRE = re.compile (r'std::')
96  srcClassNameRE = re.compile (r'(\w+)/src/classes_def.*[.]xml')
97  ignoreSrcRE = re.compile (r'.*/FWCore/Skeletons/scripts/mkTemplates/.+')
98  braketRE = re.compile (r'<.+>')
99  print("Searching for 'classes_def.xml' in '%s'." % os.path.join(os.environ.get('CMSSW_BASE'),'src'))
100  xmlFiles = []
101  for srcDir in [os.environ.get('CMSSW_BASE'),os.environ.get('CMSSW_RELEASE_BASE')]:
102  if not len(srcDir): continue
103  for xml in getoutput ('cd '+os.path.join(srcDir,'src')+'; find . -name "*classes_def*.xml" -follow -print').split ('\n'):
104  if xml and (not xml in xmlFiles):
105  xmlFiles.append(xml)
106  if options.showXMLs:
107  pprint.pprint (xmlFiles)
108  # try and figure out the names of the packages
109  xmlPackages = []
110  packagesREs = {}
111  equivREs = {}
112  explicitREs = []
113  for item in equivDict:
114  for pack in item:
115  for equiv in item[pack]:
116  explicitREs.append( (re.compile(r'\b' + equiv + r'\b'),pack))
117  if options.lostDefs:
118  for filename in xmlFiles:
119  if (not filename) or (ignoreSrcRE.match(filename)): continue
120  match = srcClassNameRE.search (filename)
121  if not match: continue
122  packageName = match.group(1)
123  xmlPackages.append (packageName)
124  matchString = r'\b' + packageName + r'\b'
125  packagesREs[packageName] = re.compile (matchString)
126  equivList = equivREs.setdefault (packageName, [])
127  for item in equivDict:
128  for equiv in item.get (packageName, []):
129  matchString = re.compile(r'\b' + equiv + r'\b')
130  equivList.append( (matchString, equiv) )
131  equivList.append( (packagesREs[packageName], packageName) )
132  classDict = {}
133  ncdict = {'class' : 'className', 'function' : 'functionName'}
134  for filename in xmlFiles:
135  if (not filename) or (ignoreSrcRE.match(filename)): continue
136  dupProblems = ''
137  exceptName = ''
138  regexList = []
139  localObjects = []
140  simpleObjectREs = []
141  if options.lostDefs:
142  lostMatch = srcClassNameRE.search (filename)
143  if lostMatch:
144  exceptName = lostMatch.group (1)
145  regexList = equivREs[exceptName]
146  xcount = len(regexList)-1
147  if not regexList[xcount][0].search (exceptName):
148  print('%s not found in' % exceptName, end=' ')
149  print(regexList[xcount][0])
150  sys.exit()
151  else: continue
152  if options.verbose:
153  print("filename", filename)
154  try:
155  filepath = os.path.join(os.environ.get('CMSSW_BASE'),'src',filename)
156  if not os.path.exists(filepath):
157  filepath = os.path.join(os.environ.get('CMSSW_RELEASE_BASE'),'src',filename)
158  xmlObj = xml2obj (filename = filepath,
159  filtering = True,
160  nameChangeDict = ncdict)
161  except Exception as detail:
162  print("File %s is malformed XML. Please fix." % filename)
163  print(" ", detail)
164  continue
165  try:
166  classList = xmlObj.selection.className
167  except:
168  try:
169  classList = xmlObj.className
170  except:
171  # this isn't a real classes_def.xml file. Skip it
172  print("**** SKIPPING '%s' - Doesn't seem to have proper information." % filename)
173  continue
174  if not classList:
175  classList = xmlObj.functionName
176  if not classList:
177  print("**** SKIPPING '%s' - Dosen't seem to have proper information(not class/function)." % filename)
178  continue
179  for piece in classList:
180  try:
181  className = spacesRE.sub ('', piece.name)
182  except:
183  # must be one of these class pattern things. Skip it
184  #print " skipping %s" % filename, piece.__repr__()
185  continue
186  className = stdRE.sub ('', className)
187  # print " ", className
188  # Now get rid of any typedefs
189  for typedef, tdList in six.iteritems(typedefsDict):
190  for alias in tdList:
191  className = re.sub (alias, typedef, className)
192  classDict.setdefault (className, set()).add (filename)
193  # should we check for lost definitions?
194  if not options.lostDefs:
195  continue
196  localObjects.append (className)
197  if options.lazyLostDefs and not braketRE.search (className):
198  #print " ", className
199  matchString = r'\b' + className + r'\b'
200  simpleObjectREs.append( (re.compile (matchString), className ) )
201  for className in localObjects:
202  # if we see our name (or equivalent) here, then let's
203  # skip complaining about this
204  foundEquiv = False
205  for equivRE in regexList:
206  #print "searching %s for %s" % (equivRE[1], className)
207  if equivRE[0].search (className):
208  foundEquiv = True
209  break
210  for simpleRE in simpleObjectREs:
211  if simpleRE[0].search (className):
212  foundEquiv = True
213  if options.verbose and simpleRE[1] != className:
214  print(" Using %s to ignore %s" \
215  % (simpleRE[1], className))
216  break
217  if foundEquiv: continue
218  for exRes in explicitREs:
219  if exRes[0].search(className):
220  dupProblems += " %s : %s\n" % (exRes[1], className)
221  foundEquiv = True
222  break
223  if foundEquiv: continue
224  for packageName in xmlPackages:
225  # don't bother looking for the name of this
226  # package in this package
227  if packagesREs[packageName].search (className):
228  dupProblems += " %s : %s\n" % (packageName, className)
229  break
230  # for piece
231  if dupProblems:
232  print('\n%s\n%s\n' % (filename, dupProblems))
233  # for filename
234  if options.dups:
235  for name, fileSet in sorted( six.iteritems(classDict) ):
236  if len (fileSet) < 2:
237  continue
238  print(name)
239  fileList = sorted (fileSet)
240  for filename in fileList:
241  print(" ", filename)
242  print()
243  # for name, fileSet
244  # if not noDups
245  #pprint.pprint (classDict)
246 
247 
249  """ Searches the edmpluginFile to find any duplicate
250  plugins."""
251  edmpluginFile = ''
252  libenv = 'LD_LIBRARY_PATH'
253  if os.environ.get('SCRAM_ARCH').startswith('osx'): libenv = 'DYLD_FALLBACK_LIBRARY_PATH'
254  biglib = '/biglib/'+os.environ.get('SCRAM_ARCH')
255  for libdir in os.environ.get(libenv).split(':'):
256  if libdir.endswith(biglib): continue
257  if os.path.exists(libdir+'/.edmplugincache'): edmpluginFile = edmpluginFile + ' ' + libdir+'/.edmplugincache'
258  if edmpluginFile == '': edmpluginFile = os.path.join(os.environ.get('CMSSW_BASE'),'lib',os.environ.get('SCRAM_ARCH'),'.edmplugincache')
259  cmd = "cat %s | awk '{print $2\" \"$1}' | sort | uniq | awk '{print $1}' | sort | uniq -c | grep '2 ' | awk '{print $2}'" % edmpluginFile
260  output = getoutput (cmd).split('\n')
261  for line in output:
262  if line in ignoreEdmDP: continue
263  line = line.replace("*","\*")
264  cmd = "cat %s | grep ' %s ' | awk '{print $1}' | sort | uniq " % (edmpluginFile,line)
265  out1 = getoutput (cmd).split('\n')
266  print(line)
267  for plugin in out1:
268  if plugin:
269  print(" **"+plugin+"**")
270  print()
271 
272 if __name__ == "__main__":
273  # setup options parser
274  parser = optparse.OptionParser ("Usage: %prog [options]\n"\
275  "Searches classes_def.xml for wrong/duplicate "\
276  "definitions")
277  xmlGroup = optparse.OptionGroup (parser, "ClassDef XML options")
278  dumpGroup = optparse.OptionGroup (parser, "EdmPluginDump options")
279  xmlGroup.add_option ('--dups', dest='dups', action='store_true',
280  default=False,
281  help="Search for duplicate definitions")
282  xmlGroup.add_option ('--lostDefs', dest='lostDefs', action='store_true',
283  default=False,
284  help="Looks for definitions in the wrong libraries")
285  xmlGroup.add_option ('--lazyLostDefs', dest='lazyLostDefs',
286  action='store_true',
287  default=False,
288  help="Will try to ignore as many lost defs as reasonable")
289  xmlGroup.add_option ('--verbose', dest='verbose',
290  action='store_true',
291  default=False,
292  help="Prints out a lot of information")
293  xmlGroup.add_option ('--showXMLs', dest='showXMLs', action='store_true',
294  default=False,
295  help="Shows all 'classes_def.xml' files")
296  xmlGroup.add_option ('--dir', dest='srcdir', type='string', default='',
297  help="Obsolete")
298  dumpGroup.add_option ('--edmPD', dest='edmPD', action='store_true',
299  default=False,
300  help="Searches EDM Plugin Dump for duplicates")
301  dumpGroup.add_option ('--edmFile', dest='edmFile', type='string',
302  default='',
303  help="Obsolete")
304  parser.add_option_group (xmlGroup)
305  parser.add_option_group (dumpGroup)
306  (options, args) = parser.parse_args()
307 
308  # Let's go:
309  if options.lazyLostDefs:
310  options.lostDefs = True
311  if options.showXMLs or options.lostDefs or options.dups:
312  searchClassDefXml ()
313  if options.edmPD:
314  searchDuplicatePlugins ()
cms::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
cond::persistency::search
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:19
duplicateReflexLibrarySearch.searchClassDefXml
def searchClassDefXml()
Definition: duplicateReflexLibrarySearch.py:89
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
duplicateReflexLibrarySearch.searchDuplicatePlugins
def searchDuplicatePlugins()
Definition: duplicateReflexLibrarySearch.py:248