CMS 3D CMS Logo

MassReplace.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
2 
4  """Visitor that travels within a cms.Sequence, looks for a parameter and replace its value
5  It will climb down within PSets, VPSets and VInputTags to find its target"""
6  def __init__(self,paramSearch,paramReplace,verbose=False,moduleLabelOnly=False,skipLabelTest=False):
7  self._paramSearch = self.standardizeInputTagFmt(paramSearch)
8  self._paramReplace = self.standardizeInputTagFmt(paramReplace)
9  self._moduleName = ''
10  self._verbose=verbose
11  self._moduleLabelOnly=moduleLabelOnly
12  self._skipLabelTest=skipLabelTest
13  def doIt(self,pset,base):
14  if isinstance(pset, cms._Parameterizable):
15  for name in pset.parameterNames_():
16  # if I use pset.parameters_().items() I get copies of the parameter values
17  # so I can't modify the nested pset
18  value = getattr(pset,name)
19  type = value.pythonTypeName()
20  if type == 'cms.PSet':
21  self.doIt(value,base+"."+name)
22  elif type == 'cms.VPSet':
23  for (i,ps) in enumerate(value): self.doIt(ps, "%s.%s[%d]"%(base,name,i) )
24  elif type == 'cms.VInputTag':
25  for (i,n) in enumerate(value):
26  # VInputTag can be declared as a list of strings, so ensure that n is formatted correctly
27  n = self.standardizeInputTagFmt(n)
28  if (n == self._paramSearch):
29  if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, self._paramSearch, self._paramReplace)
30  value[i] = self._paramReplace
31  elif self._moduleLabelOnly and n.moduleLabel == self._paramSearch.moduleLabel:
32  nrep = n; nrep.moduleLabel = self._paramReplace.moduleLabel
33  if self._verbose:print "Replace %s.%s[%d] %s ==> %s " % (base, name, i, n, nrep)
34  value[i] = nrep
35  elif type.endswith('.InputTag'):
36  if value == self._paramSearch:
37  if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, self._paramSearch, self._paramReplace)
38  from copy import deepcopy
39  if 'untracked' in type:
40  setattr(pset, name, cms.untracked.InputTag(self._paramReplace.getModuleLabel(),
41  self._paramReplace.getProductInstanceLabel(),
42  self._paramReplace.getProcessName()))
43  else:
44  setattr(pset, name, deepcopy(self._paramReplace) )
45  elif self._moduleLabelOnly and value.moduleLabel == self._paramSearch.moduleLabel:
46  from copy import deepcopy
47  repl = deepcopy(getattr(pset, name))
48  repl.moduleLabel = self._paramReplace.moduleLabel
49  setattr(pset, name, repl)
50  if self._verbose:print "Replace %s.%s %s ==> %s " % (base, name, value, repl)
51 
52 
53  @staticmethod
54  def standardizeInputTagFmt(inputTag):
55  ''' helper function to ensure that the InputTag is defined as cms.InputTag(str) and not as a plain str '''
56  if not isinstance(inputTag, cms.InputTag):
57  return cms.InputTag(inputTag)
58  return inputTag
59 
60  def enter(self,visitee):
61  label = ''
62  if (not self._skipLabelTest):
63  try: label = visitee.label_()
64  except AttributeError: label = '<Module not in a Process>'
65  else:
66  label = '<Module label not tested>'
67  self.doIt(visitee, label)
68  def leave(self,visitee):
69  pass
70 
71 def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag,verbose=False,moduleLabelOnly=False,skipLabelTest=False) :
72  """Replace InputTag oldInputTag with newInputTag, at any level of nesting within PSets, VPSets, VInputTags..."""
73  sequence.visit(MassSearchReplaceAnyInputTagVisitor(oldInputTag,newInputTag,verbose=verbose,moduleLabelOnly=moduleLabelOnly,skipLabelTest=skipLabelTest))
74 
75 def massReplaceInputTag(process,old="rawDataCollector",new="rawDataRepacker",verbose=False,moduleLabelOnly=False,skipLabelTest=False):
76  for s in process.paths_().keys():
77  massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest)
78  for s in process.endpaths_().keys():
79  massSearchReplaceAnyInputTag(getattr(process,s), old, new, verbose, moduleLabelOnly, skipLabelTest)
80  if process.schedule_() is not None:
81  for task in process.schedule_()._tasks:
82  massSearchReplaceAnyInputTag(task, old, new, verbose, moduleLabelOnly, skipLabelTest)
83  return(process)
84 
86  """Visitor that travels within a cms.Sequence, looks for a parameter and returns a list of modules that have it"""
87  def __init__(self,paramName,paramSearch):
88  self._paramName = paramName
89  self._paramSearch = paramSearch
90  self._modules = []
91  def enter(self,visitee):
92  if (hasattr(visitee,self._paramName)):
93  if getattr(visitee,self._paramName) == self._paramSearch:
94  self._modules.append(visitee)
95  def leave(self,visitee):
96  pass
97  def modules(self):
98  return self._modules
99 
101  """Visitor that travels within a cms.Sequence, looks for a parameter and replaces its value"""
102  def __init__(self,paramName,paramSearch,paramValue,verbose=False):
103  self._paramName = paramName
104  self._paramValue = paramValue
105  self._paramSearch = paramSearch
106  self._verbose = verbose
107  def enter(self,visitee):
108  if (hasattr(visitee,self._paramName)):
109  if getattr(visitee,self._paramName) == self._paramSearch:
110  if self._verbose:print "Replaced %s.%s: %s => %s" % (visitee,self._paramName,getattr(visitee,self._paramName),self._paramValue)
111  setattr(visitee,self._paramName,self._paramValue)
112  def leave(self,visitee):
113  pass
114 
115 def massSearchReplaceParam(sequence,paramName,paramOldValue,paramValue,verbose=False):
116  sequence.visit(MassSearchReplaceParamVisitor(paramName,paramOldValue,paramValue,verbose))
117 
118 def massReplaceParameter(process,name="label",old="rawDataCollector",new="rawDataRepacker",verbose=False):
119  for s in process.paths_().keys():
120  massSearchReplaceParam(getattr(process,s),name,old,new,verbose)
121  for s in process.endpaths_().keys():
122  massSearchReplaceParam(getattr(process,s),name,old,new,verbose)
123  if process.schedule_() is not None:
124  for task in process.schedule_()._tasks:
125  massSearchReplaceParam(task, name, old, new, verbose)
126  return(process)
127 
128 if __name__=="__main__":
129  import unittest
130  class TestModuleCommand(unittest.TestCase):
131 
133  p = cms.Process("test")
134  p.a = cms.EDProducer("a", src=cms.InputTag("gen"))
135  p.b = cms.EDProducer("ab", src=cms.InputTag("a"))
136  p.c = cms.EDProducer("ac", src=cms.InputTag("b"),
137  nested = cms.PSet(src = cms.InputTag("b"), src2 = cms.InputTag("c")),
138  nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("b")), cms.PSet(src = cms.InputTag("d"))),
139  vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d"))
140  )
141  p.s = cms.Sequence(p.a*p.b*p.c)
142  massSearchReplaceAnyInputTag(p.s, cms.InputTag("b"), cms.InputTag("new"))
143  self.assertNotEqual(cms.InputTag("new"), p.b.src)
144  self.assertEqual(cms.InputTag("new"), p.c.src)
145  self.assertEqual(cms.InputTag("new"), p.c.nested.src)
146  self.assertEqual(cms.InputTag("new"), p.c.nested.src)
147  self.assertNotEqual(cms.InputTag("new"), p.c.nested.src2)
148  self.assertEqual(cms.InputTag("new"), p.c.nestedv[0].src)
149  self.assertNotEqual(cms.InputTag("new"), p.c.nestedv[1].src)
150  self.assertNotEqual(cms.InputTag("new"), p.c.vec[0])
151  self.assertEqual(cms.InputTag("new"), p.c.vec[1])
152  self.assertNotEqual(cms.InputTag("new"), p.c.vec[2])
153  self.assertNotEqual(cms.InputTag("new"), p.c.vec[3])
154 
156  process1 = cms.Process("test")
157  massReplaceInputTag(process1, "a", "b", False, False, False)
158  self.assertEqual(process1.dumpPython(),
159 """import FWCore.ParameterSet.Config as cms
160 
161 process = cms.Process("test")
162 
163 """)
164  p = cms.Process("test")
165  p.a = cms.EDProducer("a", src=cms.InputTag("gen"))
166  p.b = cms.EDProducer("ab", src=cms.InputTag("a"))
167  p.c = cms.EDProducer("ac", src=cms.InputTag("b"),
168  nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")),
169  nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))),
170  vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d"))
171  )
172  p.d = cms.EDProducer("ab", src=cms.InputTag("a"))
173  p.e = cms.EDProducer("ab", src=cms.InputTag("a"))
174  p.f = cms.EDProducer("ab", src=cms.InputTag("a"))
175  p.g = cms.EDProducer("ab", src=cms.InputTag("a"))
176  p.h = cms.EDProducer("ab", src=cms.InputTag("a"))
177  p.i = cms.EDProducer("ab", src=cms.InputTag("a"))
178  p.s1 = cms.Sequence(p.a*p.b*p.c)
179  p.path1 = cms.Path(p.s1)
180  p.s2 = cms.Sequence(p.d)
181  p.path2 = cms.Path(p.e)
182  p.s3 = cms.Sequence(p.f)
183  p.endpath1 = cms.EndPath(p.s3)
184  p.endpath2 = cms.EndPath(p.g)
185  p.t1 = cms.Task(p.h)
186  p.t2 = cms.Task(p.i)
187  p.schedule = cms.Schedule()
188  p.schedule.associate(p.t1, p.t2)
189  massReplaceInputTag(p, "a", "b", False, False, False)
190  self.assertEqual(cms.InputTag("b"), p.b.src)
191  self.assertEqual(cms.InputTag("b"), p.c.vec[0])
192  self.assertEqual(cms.InputTag("c"), p.c.vec[2])
193  self.assertEqual(cms.InputTag("a"), p.d.src)
194  self.assertEqual(cms.InputTag("b"), p.e.src)
195  self.assertEqual(cms.InputTag("b"), p.f.src)
196  self.assertEqual(cms.InputTag("b"), p.g.src)
197  self.assertEqual(cms.InputTag("b"), p.h.src)
198  self.assertEqual(cms.InputTag("b"), p.i.src)
199 
201  p = cms.Process("test")
202  p.a = cms.EDProducer("a", src=cms.InputTag("gen"))
203  p.b = cms.EDProducer("ab", src=cms.InputTag("a"))
204  p.c = cms.EDProducer("ac", src=cms.InputTag("b"),
205  nested = cms.PSet(src = cms.InputTag("c"))
206  )
207  p.s = cms.Sequence(p.a*p.b*p.c)
208  massSearchReplaceParam(p.s,"src",cms.InputTag("b"),"a")
209  self.assertEqual(cms.InputTag("a"),p.c.src)
210  self.assertNotEqual(cms.InputTag("a"),p.c.nested.src)
211 
213  process1 = cms.Process("test")
214  massReplaceParameter(process1, "src", cms.InputTag("a"), "b", False)
215  self.assertEqual(process1.dumpPython(),
216 """import FWCore.ParameterSet.Config as cms
217 
218 process = cms.Process("test")
219 
220 """)
221  p = cms.Process("test")
222  p.a = cms.EDProducer("a", src=cms.InputTag("gen"))
223  p.b = cms.EDProducer("ab", src=cms.InputTag("a"))
224  p.c = cms.EDProducer("ac", src=cms.InputTag("b"),
225  nested = cms.PSet(src = cms.InputTag("a"), src2 = cms.InputTag("c")),
226  nestedv = cms.VPSet(cms.PSet(src = cms.InputTag("a")), cms.PSet(src = cms.InputTag("d"))),
227  vec = cms.VInputTag(cms.InputTag("a"), cms.InputTag("b"), cms.InputTag("c"), cms.InputTag("d"))
228  )
229  p.d = cms.EDProducer("ab", src=cms.InputTag("a"))
230  p.e = cms.EDProducer("ab", src=cms.InputTag("a"))
231  p.f = cms.EDProducer("ab", src=cms.InputTag("a"))
232  p.g = cms.EDProducer("ab", src=cms.InputTag("a"))
233  p.h = cms.EDProducer("ab", src=cms.InputTag("a"))
234  p.i = cms.EDProducer("ab", src=cms.InputTag("a"))
235  p.s1 = cms.Sequence(p.a*p.b*p.c)
236  p.path1 = cms.Path(p.s1)
237  p.s2 = cms.Sequence(p.d)
238  p.path2 = cms.Path(p.e)
239  p.s3 = cms.Sequence(p.f)
240  p.endpath1 = cms.EndPath(p.s3)
241  p.endpath2 = cms.EndPath(p.g)
242  p.t1 = cms.Task(p.h)
243  p.t2 = cms.Task(p.i)
244  p.schedule = cms.Schedule()
245  p.schedule.associate(p.t1, p.t2)
246  massReplaceParameter(p, "src",cms.InputTag("a"), "b", False)
247  self.assertEqual(cms.InputTag("gen"), p.a.src)
248  self.assertEqual(cms.InputTag("b"), p.b.src)
249  self.assertEqual(cms.InputTag("a"), p.c.vec[0])
250  self.assertEqual(cms.InputTag("c"), p.c.vec[2])
251  self.assertEqual(cms.InputTag("a"), p.d.src)
252  self.assertEqual(cms.InputTag("b"), p.e.src)
253  self.assertEqual(cms.InputTag("b"), p.f.src)
254  self.assertEqual(cms.InputTag("b"), p.g.src)
255  self.assertEqual(cms.InputTag("b"), p.h.src)
256  self.assertEqual(cms.InputTag("b"), p.i.src)
257  unittest.main()
def __init__(self, paramName, paramSearch, paramValue, verbose=False)
Definition: MassReplace.py:102
def massSearchReplaceAnyInputTag(sequence, oldInputTag, newInputTag, verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:71
def massSearchReplaceParam(sequence, paramName, paramOldValue, paramValue, verbose=False)
Definition: MassReplace.py:115
def testMassSearchReplaceAnyInputTag(self)
Definition: MassReplace.py:132
def massReplaceInputTag(process, old="rawDataCollector", new="rawDataRepacker", verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:75
def __init__(self, paramName, paramSearch)
Definition: MassReplace.py:87
return(e1-e2)*(e1-e2)+dp *dp
def massReplaceParameter(process, name="label", old="rawDataCollector", new="rawDataRepacker", verbose=False)
Definition: MassReplace.py:118
def __init__(self, paramSearch, paramReplace, verbose=False, moduleLabelOnly=False, skipLabelTest=False)
Definition: MassReplace.py:6