11 import FWCore.ParameterSet.Config
as cms
12 import FWCore.ParameterSet.Modules
as mod
13 import FWCore.ParameterSet.Types
as typ
20 def __init__(self, label, parent=None, parameters=None):
25 parent._configChildren += [self]
26 if parameters !=
None:
37 logging.debug(__name__ +
": __init__")
71 """ Read cms objects recursively from path """ 73 if isinstance(pth, (cms.Path, cms.EndPath, cms.Sequence, cms.SequencePlaceholder, cms.Source, mod._Module, cms.Service, cms.ESSource, cms.ESProducer, cms.ESPrefer, cms.PSet, cms.VPSet)):
75 entry._configChildren=[]
78 if not pth
in mother._configChildren:
79 mother._configChildren += [pth]
85 if isinstance(pth, list):
88 if hasattr(sqt,
"_SequenceCollection"):
90 if isinstance(pth, (sqt._ModuleSequenceType)):
91 if isinstance(pth._seq, (sqt._SequenceCollection)):
92 for o
in pth._seq._collection:
96 elif isinstance(pth, sqt._UnarySequenceOperator):
102 if isinstance(o, sqt._Sequenceable):
106 """ Read connection between objects """ 108 checkedObjects = set()
114 compareObjectList+=[(obj,o)
for o
in self.
_allObjects]
115 compareObjectList+=[(o,obj)
for o
in self.
_allObjects]
117 compareObjectList=[(o1,o2)
for o1
in objects
for o2
in objects]
118 for connection
in compareObjectList:
122 if (
not connection
in checkedObjects)
and (connection
not in self.
_connections):
123 checkedObjects.add(connection)
124 for key, value
in self.
inputTags(connection[1]):
131 if module == self.
label(connection[0]):
151 result.sort(
lambda x, y: cmp(self.
label(x).lower(), self.
label(y).lower()))
155 """ Open config file and read it. 157 logging.debug(__name__ +
": open")
160 global imported_configs
165 for i
in imported_configs.iterkeys():
166 if i
in sys.modules.keys():
168 sys.path.insert(0, os.path.dirname(self.
_filename))
169 common_imports = sys.modules.copy()
176 imported_configs = sys.modules.copy() 177 for i
in common_imports.iterkeys():
178 del imported_configs[i]
181 for j
in six.itervalues(imported_configs):
184 if entry[0] !=
"_" and entry !=
"cms":
186 for k
in six.itervalues(imported_configs):
187 if hasattr(k, entry):
189 if len(setk) < len(setj)
and setk < setj:
193 if hasattr(j,
"__file__"):
195 file_dict[entry] = filen
207 o=getattr(self.
_file, entry)
208 if entry[0] !=
"_" and entry !=
"cms" and hasattr(o,
"label_"):
209 getattr(self.
_file, entry).setLabel(entry)
210 text = os.path.splitext(os.path.basename(file_dict[o.label_()]))[0]
219 for obj
in reversed(self.
children(object)):
227 self._file.process=process
229 parameters = {
"name": self.
process().process}
230 process_folder =
ConfigFolder(
"process",
None, parameters)
236 folder_list += [(
"source", [self.
process().source])]
237 if self.
process().schedule !=
None:
238 folder_list += [(
"paths", self.
process().schedule)]
240 folder_list += [(
"paths", six.itervalues(self.
process().paths))]
241 folder_list += [(
"endpaths", six.itervalues(self.
process().endpaths))]
246 folder_list += [(
"essources", self.
_sort_list(self.
process().es_sources.values()))]
247 folder_list += [(
"esproducers", self.
_sort_list(self.
process().es_producers.values()))]
248 folder_list += [(
"esprefers", self.
_sort_list(self.
process().es_prefers.values()))]
250 for foldername, entry
in folder_list:
253 folders[foldername]=folder
257 print "Creating schedule...",
260 self._scheduledObjects.reverse()
263 if str(obj)
in names:
265 scheduled_folder =
ConfigFolder(
"scheduled", folders[
"paths"])
267 folders[
"paths"]._configChildren.remove(scheduled_folder)
268 folders[
"paths"]._configChildren.insert(0,scheduled_folder)
275 if hasattr(self.
_file,
"process"):
276 return self._file.process
281 foundHeaderPart1 =
False 282 foundHeaderPart2 =
False 284 search_paths=[os.path.abspath(os.path.dirname(self.
_filename))]
285 while theFile
and not (foundHeaderPart1
and foundHeaderPart2)
and lines > 0:
286 line = theFile.readline()
288 if "Generated by ConfigEditor" in line:
289 foundHeaderPart1 =
True 290 splitline = line.split(
"'")
291 if foundHeaderPart1
and len(splitline) == 5
and splitline[0] ==
"sys.path.append(os.path.abspath(os.path.expandvars(os.path.join(" and splitline[4] ==
"))))\n":
292 search_paths+=[os.path.abspath(os.path.expandvars(os.path.join(splitline[1],splitline[3])))]
293 splitline = line.split()
294 if foundHeaderPart1
and len(splitline) == 4
and splitline[0] ==
"from" and splitline[2] ==
"import":
295 for search_path
in search_paths:
296 if os.path.exists(os.path.join(search_path,splitline[1]+
".py")):
297 self.
_filename = os.path.join(search_path,splitline[1]+
".py")
300 foundHeaderPart2 =
True 304 """ dump python configuration """ 305 logging.debug(__name__ +
": dumpPython")
312 """ configuration history """ 313 logging.debug(__name__ +
": history")
320 """ Get label of an object """ 322 if hasattr(object,
"label_")
and (
not hasattr(object,
"hasLabel_")
or object.hasLabel_()):
323 text =
str(object.label_())
327 if hasattr(object,
"_name"):
328 text =
str(object._name)
330 if hasattr(object,
"type_"):
331 text =
str(object.type_())
337 """ Get children of an object """ 338 if hasattr(object,
"_configChildren"):
339 return tuple(object._configChildren)
344 return isinstance(object, (ConfigFolder, list, cms.Path, cms.EndPath, cms.Sequence))
359 """ Get motherRelations of an object """ 360 if object
in self._motherRelationsDict.keys():
369 """ Get daughterRelations of an object """ 370 if object
in self._daughterRelationsDict.keys():
379 """ Get type of an object """ 380 return object.__class__.__name__
383 """ Get classname of an object """ 385 if hasattr(object,
"type_"):
386 text = object.type_()
390 """ Get full filename """ 394 if text ==
"" or text.find(
"FWCore/ParameterSet") >= 0
or text.find(
"/build/") >= 0:
395 if self.
label(object)
in file_dict:
396 text = file_dict[self.
label(object)]
399 root = os.path.splitext(text)[0]
405 """ Get linenumber """ 407 if hasattr(object,
"_filename"):
408 if object._filename.find(
"FWCore/ParameterSet") < 0
and object._filename.find(
"ConfigEditor") < 0:
409 if hasattr(object,
"_lineNumber"):
410 text =
str(object._lineNumber)
415 text = os.path.splitext(os.path.basename(self.
fullFilename(object)))[0]
419 match_compiled = re.match(
r'(?:^|.*?/)CMSSW[0-9_]*/python/((?:\w*/)*\w*)\.py$',self.
fullFilename(object))
421 return match_compiled.group(1).
replace(
'/',
'.')
423 match_norm = re.match(
r'(?:^|.*?/)(\w*)/(\w*)/(?:test|python)/((?:\w*/)*)(\w*)\.py$',self.
fullFilename(object))
425 return '%s.%s.%s%s' % (match_norm.group(1),match_norm.group(2),match_norm.group(3).
replace(
'/',
'.'),match_norm.group(4))
429 match_compiled = re.match(
r'(?:^|.*?/)CMSSW[0-9_]*/python/((?:\w*/){2})((?:\w*/)*)(\w*\.py)$',self.
fullFilename(object))
431 return '%spython/%s%s' % (match_compiled.group(1),match_compiled.group(2),match_compiled.group(3))
432 match_norm = re.match(
r'(?:^|.*?/)(\w*/\w*/(?:test|python)/(?:\w*/)*\w*\.py)$',self.
fullFilename(object))
434 return match_norm.group(1)
438 """ Get Package of an object file """ 441 if len(shortdirname) > 1:
442 text = shortdirname[1]
446 """ Get parameters of an object """ 448 if hasattr(object,
"parameters_"):
449 this_parameters = object.parameters_().
items()
450 elif hasattr(object,
"_seq"):
451 if hasattr(object._seq,
"dumpSequencePython"):
452 this_parameters = [(
'sequence', object._seq.dumpSequencePython())]
454 this_parameters = [(
'sequence',
'WARNING: object was removed from a sequence.')]
455 if hasattr(object,
"tarlabel_"):
456 this_parameters += [(
'tarlabel', object.tarlabel_())]
457 return this_parameters
460 """ Add alls inputtags of value to a list """ 461 if isinstance(value, cms.VInputTag):
462 for i
in range(len(value)):
463 if isinstance(value[i], str):
464 self.
_addInputTag(cms.InputTag(value[i]), this_key+
"["+
str(i)+
"]", this_inputtags)
467 elif isinstance(value, list):
470 if hasattr(value,
"parameters_"):
472 if isinstance(value, cms.InputTag):
473 pythonValue = value.value()
474 this_inputtags += [(
str(this_key), value.value())]
477 """ Make list of inputtags from parameter dict """ 479 for key, value
in this_parameters:
485 return this_inputtags
488 """ Make list of inputtags from parameter dict """ 490 v = self._inputTagsDict.get(object)
499 """ Get list of all config objects that are used as input """ 500 if not object
in self._usesDict.keys():
502 for key, value
in self.
inputTags(object):
505 if module
not in uses:
514 """ Make list of all mother sequences """ 515 if not object
in self._foundInDict.keys():
518 for daughter
in self.
children(entry):
519 if self.
label(object) == self.
label(daughter)
and len(self.
children(entry)) > 0
and not self.
label(entry)
in foundin:
520 foundin += [self.
label(entry)]
528 """ Find config objects that use this as input """ 529 if not object
in self._usedByDict.keys():
532 for uses
in self.
uses(entry):
533 if self.
label(object) == uses
and not self.
label(entry)
in usedby:
534 usedby += [self.
label(entry)]
544 if name !=
"" and not isinstance(object, typ.PSet):
546 partyp=
str(type(object)).
split(
"'")[1].
replace(
"FWCore.ParameterSet.Types",
"cms")
547 if isinstance(object, cms.InputTag):
548 inputtagValue=object.pythonValue()
549 for i
in range(3-len(inputtagValue.split(
","))):
550 inputtagValue+=
', ""' 551 properties += [(
"String", name,
"cms.InputTag("+inputtagValue+
")", partyp, readonly)]
552 elif isinstance(object, cms.bool):
553 properties += [(
"Boolean", name, object.value(), partyp, readonly)]
554 elif isinstance(object, (cms.int32, cms.uint32, cms.int64, cms.uint64)):
555 properties += [(
"Integer", name, object.value(), partyp, readonly)]
556 elif isinstance(object, cms.double):
557 properties += [(
"Double", name, object.value(), partyp, readonly)]
558 elif hasattr(object,
"pythonValue"):
559 properties += [(
"String", name,
str(object.pythonValue()).
strip(
"\"'"), partyp, readonly)]
560 elif hasattr(object,
"value"):
561 properties += [(
"MultilineString", name,
str(object.value()), partyp, readonly)]
563 properties += [(
"MultilineString", name,
str(object), partyp, readonly)]
567 if isinstance(object, ConfigFolder):
571 params.sort(
lambda x, y: cmp(x[0].lower(), y[0].lower()))
572 for key, value
in params:
581 """ Make list of all properties """ 584 properties += [(
"Category",
"Object info",
"")]
585 if self.
label(object) !=
"":
586 properties += [(
"String",
"label", self.
label(object),
None,
True)]
587 if self.
type(object) !=
"":
588 text = self.
type(object)
590 text +=
" <" + self.
classname(object) +
">" 591 properties += [(
"String",
"type", text,
None,
True)]
596 properties += [(
"String",
"file", text,
None,
True)]
598 properties += [(
"String",
"package", self.
package(object),
None,
True)]
600 properties += [(
"String",
"full filename", self.
fullFilename(object),
None,
True)]
604 for entry
in foundIn:
608 properties += [(
"String",
"in sequence", text,
"This module/sequence is used the listed sequences",
True)]
609 uses=self.
uses(object)
610 usedBy=self.
usedBy(object)
611 if len(uses) + len(usedBy) > 0:
612 properties += [(
"Category",
"Connections",
"")]
619 properties += [(
"MultilineString",
"uses", text,
"This module/sequence depends on the output of the listes modules/seuquences",
True)]
627 properties += [(
"MultilineString",
"used by", text,
"The listed modules/sequences depend on the output of this module/sequence",
True)]
629 properties += [(
"Category",
"Parameters",
"")]
631 return tuple(properties)
634 """ Sets a property with given name to value. 636 if hasattr(object,
"_seq")
and name==
"sequence":
637 return "Modification of sequences not supported yet." 641 if isinstance(value,str)
and\
642 not value[0]==
"[" and\
643 not value[0:4]==
"cms.":
644 exec(
"object." + name +
"='''" + value +
"'''")
646 exec(
"object." + name +
"=" +
str(value))
647 except Exception
as e:
648 error=
"Cannot set parameter "+name+
" (see logfile for details):\n"+
str(e)
649 logging.warning(__name__ +
": setProperty: Cannot set parameter "+name+
": "+
exception_traceback())
658 for key, value
in self.
inputTags(object):
662 product = elements[1]
666 process = elements[2]
669 if not module
in allLabels:
670 if not (
"*",module,product,process)
in content:
671 content += [(
"*",module,product,process)]
672 if "*_"+module+
"_"+product+
"_"+process
in content_objects.keys():
673 content_objects[
"*_"+module+
"_"+product+
"_"+process]+=
","+self.
label(object)
675 content_objects[
"*_"+module+
"_"+product+
"_"+process]=self.
label(object)
676 return (content,content_objects)
680 if self.
type(object)
in [
"EDProducer",
"EDFilter",
"EDAnalyzer"]]
684 inputModules = [object
for object
in self.
_allObjects\
685 if self.
type(object) ==
"Source"]
686 if len(inputModules) > 0
and hasattr(inputModules[0],
"inputCommands"):
687 return inputModules[0].inputCommands
692 outputModules = [object
for object
in self.
_allObjects\
693 if self.
type(object) ==
"OutputModule"]
694 if len(outputModules) > 0
and hasattr(outputModules[0],
"outputCommands"):
695 return outputModules[0].outputCommands
701 if len(outputCommands)>0
and outputCommands[0]!=
"keep *":
702 for object
in content:
705 for object
in content:
707 for o
in outputCommands:
709 command, filter = [ x
for x
in o.split(
" ")
if x]
710 if len(filter.split(
"_")) > 1:
711 module = filter.split(
"_")[1]
712 product = filter.split(
"_")[2]
713 process = filter.split(
"_")[3]
718 for object
in content:
720 match = module.strip(
"*")
in object[1]
722 match = module == object[1]
724 match = match
and product.strip(
"*")
in object[2]
726 match = match
and product == object[2]
728 match = match
and process.strip(
"*")
in object[3]
730 match = match
and process == object[3]
732 keep[object] = command ==
"keep" 733 return [object
for object
in content
if keep[object]]
def nonSequenceChildren(self, object)
def topLevelObjects(self)
def parameters(self, object)
def _readHeaderInfo(self)
def motherRelations(self, object)
def children(self, object)
def exception_traceback()
def replace(string, replacements)
def package(self, object)
def _addInputTag(self, value, this_key, this_inputtags)
def fullFilename(self, object)
def lineNumber(self, object)
def open(self, filename=None)
def classname(self, object)
def pypackage(self, object)
def foundIn(self, object)
def isReplaceConfig(self)
def _readInputTagsRecursive(self, this_parameters, start_key="")
def daughterRelations(self, object)
def cancelOperations(self)
def setProperty(self, object, name, value, categoryName)
def _scheduleRecursive(self, object)
def setIsReplaceConfig(self)
def __init__(self, label, parent=None, parameters=None)
static std::string join(char **cmd)
def inputTags(self, object)
def readConnections(self, objects, toNeighbors=False)
def recursePSetProperties(self, name, object, readonly=None)
def properties(self, object)
def outputEventContent(self)
def _readRecursive(self, mother, pth)
def setProcess(self, process)
def inputEventContent(self)
def applyCommands(self, content, outputCommands)
def allChildren(self, object)
def isContainer(self, object)
def filename(self, object)