Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-23 02:25:46

0001 #!/usr/bin/env python
0002 
0003 import FWCore.ParameterSet.Config as cms
0004 from FWCore.ParameterSet import DictTypes
0005 
0006 import sys, os, os.path
0007 
0008 # enable tracing cms.Sequences, cms.Paths and cms.EndPaths for all imported modules (thus, process.load(...), too)
0009 import tracingImport
0010 
0011 result = dict()
0012 result['procname']       = ''
0013 result['main_input']     = None
0014 result['looper']         = DictTypes.SortedKeysDict()
0015 result['psets']          = DictTypes.SortedKeysDict()
0016 result['modules']        = DictTypes.SortedKeysDict()
0017 result['es_modules']     = DictTypes.SortedKeysDict()
0018 result['es_sources']     = DictTypes.SortedKeysDict()
0019 result['es_prefers']     = DictTypes.SortedKeysDict()
0020 result['output_modules'] = list()
0021 result['sequences']      = DictTypes.SortedKeysDict()
0022 result['paths']          = DictTypes.SortedKeysDict()
0023 result['endpaths']       = DictTypes.SortedKeysDict()
0024 result['services']       = DictTypes.SortedKeysDict()
0025 result['schedule']       = ''
0026 
0027 def dumpObject(obj,key):
0028     if key in ('es_modules','es_sources','es_prefers'):
0029         classname = obj['@classname']
0030         label = obj['@label']
0031         del obj['@label']
0032         del obj['@classname']
0033         returnString = "{'@classname': %s, '@label': %s, %s" %(classname, label, str(obj).lstrip('{'))
0034         return returnString
0035     elif key in ('modules','services'):
0036         classname = obj['@classname']
0037         del obj['@label']
0038         del obj['@classname']
0039         returnString = "{'@classname': %s, %s" %(classname, str(obj).lstrip('{'))
0040         return returnString
0041     elif key in ('psets',):
0042         returnString = "('PSet', 'untracked', %s)" % str(obj)
0043         return returnString
0044     else:
0045         return str(obj)
0046 
0047 
0048 def trackedness(item):
0049   if item.isTracked():
0050     return 'tracked'
0051   else:
0052     return 'untracked'
0053 
0054 # the problem are non empty VPsets
0055 def fixup(item):
0056   if isinstance(item, bool):
0057     if item: return 'true'
0058     else: return 'false'
0059   elif isinstance(item, list):
0060       return [str(i) for i in item]
0061   elif isinstance(item, str):
0062       return '"%s"' %item
0063   else:
0064       return str(item)
0065 
0066 def prepareParameter(parameter):
0067     if isinstance(parameter, cms.VPSet):
0068         configValue = []
0069         for item in parameter:
0070             configValue.append((prepareParameter(item)[2]))
0071         return (type(parameter).__name__, trackedness(parameter), configValue )
0072     if isinstance(parameter, cms.PSet):
0073         configValue = {}
0074         for name, item in parameter.parameters_().items():
0075           configValue[name] = prepareParameter(item)
0076         return (type(parameter).__name__, trackedness(parameter), configValue )
0077     else:
0078         return (type(parameter).__name__, trackedness(parameter), fixup(parameter.value()) )
0079 
0080 def parsePSet(module):
0081   if module is None: return
0082   config = DictTypes.SortedKeysDict()
0083   for parameterName,parameter in module.parameters_().items():
0084     config[parameterName] = prepareParameter(parameter)
0085   return config
0086 
0087 def parseSource(module):
0088   if module is None: return
0089   config = DictTypes.SortedKeysDict()
0090   config['@classname'] = ('string','tracked',module.type_())
0091   for parameterName,parameter in module.parameters_().items():
0092     config[parameterName] = prepareParameter(parameter)
0093   return config
0094 
0095 def parseModule(name, module):
0096   if module is None: return
0097   config = DictTypes.SortedKeysDict()
0098   config['@classname'] = ('string','tracked',module.type_())
0099   config['@label'] = ('string','tracked',name)
0100   for parameterName,parameter in module.parameters_().items():
0101     config[parameterName] = prepareParameter(parameter)
0102   return config
0103 
0104 def parseModules(process):
0105   result['procname'] = process.process
0106  
0107   result['main_input'] = parseSource(process.source)
0108 
0109   for name,item in process.producers.items():
0110     result['modules'][name] = parseModule(name, item)
0111 
0112   for name,item in process.filters.items():
0113     result['modules'][name] = parseModule(name, item)
0114 
0115   for name,item in process.analyzers.items():
0116     result['modules'][name] = parseModule(name, item)
0117 
0118   for name,item in process.outputModules.items():
0119     result['modules'][name] = parseModule(name, item)
0120     result['output_modules'].append(name)
0121 
0122   for name,item in process.es_sources.items():
0123     result['es_sources'][name + '@'] = parseModule(name, item)
0124 
0125   for name,item in process.es_producers.items():
0126     result['es_modules'][name + '@'] = parseModule(name, item)
0127 
0128   for name,item in process.es_prefers.items():
0129     result['es_prefers'][name + '@'] = parseModule(name, item)
0130 
0131   for name,item in process.psets.items():
0132     result['psets'][name] = parsePSet(item)
0133 
0134   for name,item in process.sequences.items():
0135     result['sequences'][name] = "'" + item.dumpConfig("")[1:-2] + "'"
0136 
0137   for name,item in process.paths.items():
0138     result['paths'][name] = "'" + item.dumpConfig("")[1:-2] + "'"
0139 
0140   for name,item in process.endpaths.items():
0141     result['endpaths'][name] = "'" + item.dumpConfig("")[1:-2] + "'"
0142 
0143   for name,item in process.services.items():
0144     result['services'][name] = parseModule(name, item)
0145 
0146   # TODO still missing:
0147   #   process.vpsets
0148   #   process.looper
0149   #   process.schedule
0150  
0151   # use the ordering seen at module import time for sequence, paths and endpaths,
0152   # keeping only those effectively present in the process
0153   # (some might have been commented, removed, not added, whatever...)
0154   result['paths'].list     = [ path for path in tracingImport.original_paths     if path in result['paths'].list ]
0155   result['endpaths'].list  = [ path for path in tracingImport.original_endpaths  if path in result['endpaths'].list ]
0156   result['sequences'].list = [ path for path in tracingImport.original_sequences if path in result['sequences'].list ]
0157 
0158   # nothing to do for 'main_input' as it's a single item
0159   
0160   # sort alphabetically everything else
0161   result['modules'].list.sort()
0162   result['output_modules'].sort()
0163   result['es_sources'].list.sort()
0164   result['es_modules'].list.sort()
0165   result['es_prefers'].list.sort()
0166   result['psets'].list.sort()
0167   result['services'].list.sort()
0168 
0169 
0170 # find and load the input file
0171 sys.path.append(os.environ["PWD"])
0172 filename = sys.argv[1].rstrip('.py')
0173 theConfig = __import__(filename)
0174 
0175 try: 
0176     #'process' in theConfig.__dict__:
0177     theProcess = theConfig.process
0178 except:
0179     # what if the file is just a fragment ?
0180     # try to load it into a brand new process...
0181     theProcess = cms.Process('')
0182     try:
0183         theProcess.load(filename)
0184     except:
0185         sys.err.write('Unable to parse configuation fragment %s into a new Process\n' % sys.argv[1])
0186         sys.exit(1)
0187 
0188 # parse the configuration
0189 parseModules(theProcess)
0190 
0191 # now dump it to the screen as wanted by the HLT parser
0192 hltAcceptedOrder = ['main_input','looper', 'psets', 'modules', 'es_modules', 'es_sources', 'es_prefers', 'output_modules', 'sequences', 'paths', 'endpaths', 'services', 'schedule']
0193 
0194 print '{'
0195 print "'procname': '%s'" %result['procname']
0196 
0197 for key in hltAcceptedOrder:
0198     if key in ('output_modules', 'schedule'):
0199         print ", '%s': %s" %(key, result[key])
0200     elif key in ('main_input',):
0201         print ", '%s':  {" % key
0202         # in case no source is defined, leave an empty block in the output
0203         if result[key] is not None:
0204             print str(dumpObject(result[key], key))[1:-1]
0205         print '} # end of %s' % key
0206     else:
0207         print ", '%s':  {" % key
0208         comma = ''
0209         for name,object in result[key].items():
0210             print comma+"'%s': %s" %(name, dumpObject(object,key))
0211             comma = ', '
0212         print '} # end of %s' % key
0213 
0214 print '}'
0215