11 import FWCore.ParameterSet.Config
as cms
12 import FWCore.ParameterSet.Modules
as mod
13 import FWCore.ParameterSet.Types
as typ
19 def __init__(self, label, parent=None, parameters=None):
24 parent._configChildren += [self]
25 if parameters !=
None:
36 logging.debug(__name__ +
": __init__")
70 """ Read cms objects recursively from path """ 72 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)):
74 entry._configChildren=[]
77 if not pth
in mother._configChildren:
78 mother._configChildren += [pth]
84 if isinstance(pth, list):
87 if hasattr(sqt,
"_SequenceCollection"):
89 if isinstance(pth, (sqt._ModuleSequenceType)):
90 if isinstance(pth._seq, (sqt._SequenceCollection)):
91 for o
in pth._seq._collection:
95 elif isinstance(pth, sqt._UnarySequenceOperator):
101 if isinstance(o, sqt._Sequenceable):
105 """ Read connection between objects """ 107 checkedObjects = set()
113 compareObjectList+=[(obj,o)
for o
in self.
_allObjects]
114 compareObjectList+=[(o,obj)
for o
in self.
_allObjects]
116 compareObjectList=[(o1,o2)
for o1
in objects
for o2
in objects]
117 for connection
in compareObjectList:
121 if (
not connection
in checkedObjects)
and (
not self._connections.has_key(connection)):
122 checkedObjects.add(connection)
123 for key, value
in self.
inputTags(connection[1]):
130 if module == self.
label(connection[0]):
133 if self._connections.has_key(connection):
135 if not self._motherRelationsDict.has_key(connection[1]):
138 if not self._daughterRelationsDict.has_key(connection[0]):
150 result.sort(
lambda x, y: cmp(self.
label(x).lower(), self.
label(y).lower()))
154 """ Open config file and read it. 156 logging.debug(__name__ +
": open")
159 global imported_configs
164 for i
in imported_configs.iterkeys():
165 if i
in sys.modules.keys():
167 sys.path.insert(0, os.path.dirname(self.
_filename))
168 common_imports = sys.modules.copy()
175 imported_configs = sys.modules.copy() 176 for i
in common_imports.iterkeys():
177 del imported_configs[i]
180 for j
in imported_configs.itervalues():
183 if entry[0] !=
"_" and entry !=
"cms":
185 for k
in imported_configs.itervalues():
186 if hasattr(k, entry):
188 if len(setk) < len(setj)
and setk < setj:
192 if hasattr(j,
"__file__"):
194 file_dict[entry] = filen
206 o=getattr(self.
_file, entry)
207 if entry[0] !=
"_" and entry !=
"cms" and hasattr(o,
"label_"):
208 getattr(self.
_file, entry).setLabel(entry)
209 text = os.path.splitext(os.path.basename(file_dict[o.label_()]))[0]
218 for obj
in reversed(self.
children(object)):
226 self._file.process=process
228 parameters = {
"name": self.
process().process}
229 process_folder =
ConfigFolder(
"process",
None, parameters)
235 folder_list += [(
"source", [self.
process().source])]
236 if self.
process().schedule !=
None:
237 folder_list += [(
"paths", self.
process().schedule)]
239 folder_list += [(
"paths", self.
process().paths.itervalues())]
240 folder_list += [(
"endpaths", self.
process().endpaths.itervalues())]
245 folder_list += [(
"essources", self.
_sort_list(self.
process().es_sources.values()))]
246 folder_list += [(
"esproducers", self.
_sort_list(self.
process().es_producers.values()))]
247 folder_list += [(
"esprefers", self.
_sort_list(self.
process().es_prefers.values()))]
249 for foldername, entry
in folder_list:
252 folders[foldername]=folder
256 print "Creating schedule...",
259 self._scheduledObjects.reverse()
262 if str(obj)
in names:
264 scheduled_folder =
ConfigFolder(
"scheduled", folders[
"paths"])
266 folders[
"paths"]._configChildren.remove(scheduled_folder)
267 folders[
"paths"]._configChildren.insert(0,scheduled_folder)
274 if hasattr(self.
_file,
"process"):
275 return self._file.process
280 foundHeaderPart1 =
False 281 foundHeaderPart2 =
False 283 search_paths=[os.path.abspath(os.path.dirname(self.
_filename))]
284 while theFile
and not (foundHeaderPart1
and foundHeaderPart2)
and lines > 0:
285 line = theFile.readline()
287 if "Generated by ConfigEditor" in line:
288 foundHeaderPart1 =
True 289 splitline = line.split(
"'")
290 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":
291 search_paths+=[os.path.abspath(os.path.expandvars(os.path.join(splitline[1],splitline[3])))]
292 splitline = line.split()
293 if foundHeaderPart1
and len(splitline) == 4
and splitline[0] ==
"from" and splitline[2] ==
"import":
294 for search_path
in search_paths:
295 if os.path.exists(os.path.join(search_path,splitline[1]+
".py")):
296 self.
_filename = os.path.join(search_path,splitline[1]+
".py")
299 foundHeaderPart2 =
True 303 """ dump python configuration """ 304 logging.debug(__name__ +
": dumpPython")
311 """ configuration history """ 312 logging.debug(__name__ +
": history")
319 """ Get label of an object """ 321 if hasattr(object,
"label_")
and (
not hasattr(object,
"hasLabel_")
or object.hasLabel_()):
322 text =
str(object.label_())
326 if hasattr(object,
"_name"):
327 text =
str(object._name)
329 if hasattr(object,
"type_"):
330 text =
str(object.type_())
336 """ Get children of an object """ 337 if hasattr(object,
"_configChildren"):
338 return tuple(object._configChildren)
343 return isinstance(object, (ConfigFolder, list, cms.Path, cms.EndPath, cms.Sequence))
358 """ Get motherRelations of an object """ 359 if object
in self._motherRelationsDict.keys():
368 """ Get daughterRelations of an object """ 369 if object
in self._daughterRelationsDict.keys():
378 """ Get type of an object """ 379 return object.__class__.__name__
382 """ Get classname of an object """ 384 if hasattr(object,
"type_"):
385 text = object.type_()
389 """ Get full filename """ 393 if text ==
"" or text.find(
"FWCore/ParameterSet") >= 0
or text.find(
"/build/") >= 0:
394 if self.
label(object)
in file_dict:
395 text = file_dict[self.
label(object)]
398 root = os.path.splitext(text)[0]
404 """ Get linenumber """ 406 if hasattr(object,
"_filename"):
407 if object._filename.find(
"FWCore/ParameterSet") < 0
and object._filename.find(
"ConfigEditor") < 0:
408 if hasattr(object,
"_lineNumber"):
409 text =
str(object._lineNumber)
414 text = os.path.splitext(os.path.basename(self.
fullFilename(object)))[0]
418 match_compiled = re.match(
r'(?:^|.*?/)CMSSW[0-9_]*/python/((?:\w*/)*\w*)\.py$',self.
fullFilename(object))
420 return match_compiled.group(1).
replace(
'/',
'.')
422 match_norm = re.match(
r'(?:^|.*?/)(\w*)/(\w*)/(?:test|python)/((?:\w*/)*)(\w*)\.py$',self.
fullFilename(object))
424 return '%s.%s.%s%s' % (match_norm.group(1),match_norm.group(2),match_norm.group(3).
replace(
'/',
'.'),match_norm.group(4))
428 match_compiled = re.match(
r'(?:^|.*?/)CMSSW[0-9_]*/python/((?:\w*/){2})((?:\w*/)*)(\w*\.py)$',self.
fullFilename(object))
430 return '%spython/%s%s' % (match_compiled.group(1),match_compiled.group(2),match_compiled.group(3))
431 match_norm = re.match(
r'(?:^|.*?/)(\w*/\w*/(?:test|python)/(?:\w*/)*\w*\.py)$',self.
fullFilename(object))
433 return match_norm.group(1)
437 """ Get Package of an object file """ 440 if len(shortdirname) > 1:
441 text = shortdirname[1]
445 """ Get parameters of an object """ 447 if hasattr(object,
"parameters_"):
448 this_parameters = object.parameters_().
items()
449 elif hasattr(object,
"_seq"):
450 if hasattr(object._seq,
"dumpSequencePython"):
451 this_parameters = [(
'sequence', object._seq.dumpSequencePython())]
453 this_parameters = [(
'sequence',
'WARNING: object was removed from a sequence.')]
454 if hasattr(object,
"tarlabel_"):
455 this_parameters += [(
'tarlabel', object.tarlabel_())]
456 return this_parameters
459 """ Add alls inputtags of value to a list """ 460 if isinstance(value, cms.VInputTag):
461 for i
in range(len(value)):
462 if type(value[i])==str:
463 self.
_addInputTag(cms.InputTag(value[i]), this_key+
"["+
str(i)+
"]", this_inputtags)
466 elif isinstance(value, list):
469 if hasattr(value,
"parameters_"):
471 if isinstance(value, cms.InputTag):
472 pythonValue = value.value()
473 this_inputtags += [(
str(this_key), value.value())]
476 """ Make list of inputtags from parameter dict """ 478 for key, value
in this_parameters:
484 return this_inputtags
487 """ Make list of inputtags from parameter dict """ 489 v = self._inputTagsDict.get(object)
498 """ Get list of all config objects that are used as input """ 499 if not object
in self._usesDict.keys():
501 for key, value
in self.
inputTags(object):
504 if module
not in uses:
513 """ Make list of all mother sequences """ 514 if not object
in self._foundInDict.keys():
517 for daughter
in self.
children(entry):
518 if self.
label(object) == self.
label(daughter)
and len(self.
children(entry)) > 0
and not self.
label(entry)
in foundin:
519 foundin += [self.
label(entry)]
527 """ Find config objects that use this as input """ 528 if not object
in self._usedByDict.keys():
531 for uses
in self.
uses(entry):
532 if self.
label(object) == uses
and not self.
label(entry)
in usedby:
533 usedby += [self.
label(entry)]
543 if name !=
"" and not isinstance(object, typ.PSet):
545 partyp=
str(type(object)).
split(
"'")[1].
replace(
"FWCore.ParameterSet.Types",
"cms")
546 if isinstance(object, cms.InputTag):
547 inputtagValue=object.pythonValue()
548 for i
in range(3-len(inputtagValue.split(
","))):
549 inputtagValue+=
', ""' 550 properties += [(
"String", name,
"cms.InputTag("+inputtagValue+
")", partyp, readonly)]
551 elif isinstance(object, cms.bool):
552 properties += [(
"Boolean", name, object.value(), partyp, readonly)]
553 elif isinstance(object, (cms.int32, cms.uint32, cms.int64, cms.uint64)):
554 properties += [(
"Integer", name, object.value(), partyp, readonly)]
555 elif isinstance(object, cms.double):
556 properties += [(
"Double", name, object.value(), partyp, readonly)]
557 elif hasattr(object,
"pythonValue"):
558 properties += [(
"String", name,
str(object.pythonValue()).
strip(
"\"'"), partyp, readonly)]
559 elif hasattr(object,
"value"):
560 properties += [(
"MultilineString", name,
str(object.value()), partyp, readonly)]
562 properties += [(
"MultilineString", name,
str(object), partyp, readonly)]
566 if isinstance(object, ConfigFolder):
570 params.sort(
lambda x, y: cmp(x[0].lower(), y[0].lower()))
571 for key, value
in params:
580 """ Make list of all properties """ 583 properties += [(
"Category",
"Object info",
"")]
584 if self.
label(object) !=
"":
585 properties += [(
"String",
"label", self.
label(object),
None,
True)]
586 if self.
type(object) !=
"":
587 text = self.
type(object)
589 text +=
" <" + self.
classname(object) +
">" 590 properties += [(
"String",
"type", text,
None,
True)]
595 properties += [(
"String",
"file", text,
None,
True)]
597 properties += [(
"String",
"package", self.
package(object),
None,
True)]
599 properties += [(
"String",
"full filename", self.
fullFilename(object),
None,
True)]
603 for entry
in foundIn:
607 properties += [(
"String",
"in sequence", text,
"This module/sequence is used the listed sequences",
True)]
608 uses=self.
uses(object)
609 usedBy=self.
usedBy(object)
610 if len(uses) + len(usedBy) > 0:
611 properties += [(
"Category",
"Connections",
"")]
618 properties += [(
"MultilineString",
"uses", text,
"This module/sequence depends on the output of the listes modules/seuquences",
True)]
626 properties += [(
"MultilineString",
"used by", text,
"The listed modules/sequences depend on the output of this module/sequence",
True)]
628 properties += [(
"Category",
"Parameters",
"")]
630 return tuple(properties)
633 """ Sets a property with given name to value. 635 if hasattr(object,
"_seq")
and name==
"sequence":
636 return "Modification of sequences not supported yet." 640 if isinstance(value,str)
and\
641 not value[0]==
"[" and\
642 not value[0:4]==
"cms.":
643 exec(
"object." + name +
"='''" + value +
"'''")
645 exec(
"object." + name +
"=" +
str(value))
646 except Exception
as e:
647 error=
"Cannot set parameter "+name+
" (see logfile for details):\n"+
str(e)
648 logging.warning(__name__ +
": setProperty: Cannot set parameter "+name+
": "+
exception_traceback())
657 for key, value
in self.
inputTags(object):
661 product = elements[1]
665 process = elements[2]
668 if not module
in allLabels:
669 if not (
"*",module,product,process)
in content:
670 content += [(
"*",module,product,process)]
671 if "*_"+module+
"_"+product+
"_"+process
in content_objects.keys():
672 content_objects[
"*_"+module+
"_"+product+
"_"+process]+=
","+self.
label(object)
674 content_objects[
"*_"+module+
"_"+product+
"_"+process]=self.
label(object)
675 return (content,content_objects)
679 if self.
type(object)
in [
"EDProducer",
"EDFilter",
"EDAnalyzer"]]
683 inputModules = [object
for object
in self.
_allObjects\
684 if self.
type(object) ==
"Source"]
685 if len(inputModules) > 0
and hasattr(inputModules[0],
"inputCommands"):
686 return inputModules[0].inputCommands
691 outputModules = [object
for object
in self.
_allObjects\
692 if self.
type(object) ==
"OutputModule"]
693 if len(outputModules) > 0
and hasattr(outputModules[0],
"outputCommands"):
694 return outputModules[0].outputCommands
700 if len(outputCommands)>0
and outputCommands[0]!=
"keep *":
701 for object
in content:
704 for object
in content:
706 for o
in outputCommands:
708 command, filter = [ x
for x
in o.split(
" ")
if x]
709 if len(filter.split(
"_")) > 1:
710 module = filter.split(
"_")[1]
711 product = filter.split(
"_")[2]
712 process = filter.split(
"_")[3]
717 for object
in content:
719 match = module.strip(
"*")
in object[1]
721 match = module == object[1]
723 match = match
and product.strip(
"*")
in object[2]
725 match = match
and product == object[2]
727 match = match
and process.strip(
"*")
in object[3]
729 match = match
and process == object[3]
731 keep[object] = command ==
"keep" 732 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)