ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PsetManipulator.py
(Generate patch)

Comparing COMP/CRAB/python/PsetManipulator.py (file contents):
Revision 1.21 by slacapra, Wed Jul 16 11:24:01 2008 UTC vs.
Revision 1.33 by ewv, Wed Nov 4 18:01:41 2009 UTC

# Line 3 | Line 3
3   import os
4   import common
5   import imp
6 + import pickle
7  
8   from crab_util import *
9   from crab_exceptions import *
9 from crab_logger import Logger
10  
11   from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
12 + from FWCore.ParameterSet.Config    import include
13   from FWCore.ParameterSet.DictTypes import SortedKeysDict
14 + from FWCore.ParameterSet.Modules   import OutputModule
15   from FWCore.ParameterSet.Modules   import Service
16   from FWCore.ParameterSet.Types     import *
17  
18   import FWCore.ParameterSet.Types   as CfgTypes
19   import FWCore.ParameterSet.Modules as CfgModules
20 + import FWCore.ParameterSet.Config  as cms
21  
22   class PsetManipulator:
23      def __init__(self, pset):
# Line 23 | Line 26 | class PsetManipulator:
26          """
27  
28          self.pset = pset
29 <        #convert Pset
30 <        from FWCore.ParameterSet.Config import include
31 <        common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset)
32 <        if self.pset.endswith('py'):
30 <            handle = open(self.pset, 'r')
31 <            try:   # Nested form for Python < 2.5
32 <                try:
33 <                    self.cfo = imp.load_source("pycfg", self.pset, handle)
34 <                    self.cmsProcess = self.cfo.process
35 <                except Exception, ex:
36 <                    msg = "Your config file is not valid python: %s" % str(ex)
37 <                    raise CrabException(msg)
38 <            finally:
39 <                handle.close()
40 <        else:
29 >
30 >        common.logger.debug("PsetManipulator::__init__: PSet file = "+self.pset)
31 >        handle = open(self.pset, 'r')
32 >        try:   # Nested form for Python < 2.5
33              try:
34 <                self.cfo = include(self.pset)
35 <                self.cmsProcess = self.cfo
34 >                self.cfo = imp.load_source("pycfg", self.pset, handle)
35 >                self.cmsProcess = self.cfo.process
36              except Exception, ex:
37 <                msg =  "Your cfg file is not valid, %s\n" % str(ex)
46 <                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
47 <                msg += "  may help you understand the problem."
37 >                msg = "Your config file is not valid python: %s" % str(ex)
38                  raise CrabException(msg)
39 +        finally:
40 +            handle.close()
41 +
42          self.cfg = CfgInterface(self.cmsProcess)
43 +        try: # Quiet the output
44 +            self.cfg.data.MessageLogger.cerr.FwkReport.reportEvery = cms.untracked.int32(100)
45 +        except AttributeError:
46 +            pass
47  
48      def maxEvent(self, maxEv):
49          """
# Line 55 | Line 52 | class PsetManipulator:
52          self.cfg.maxEvents.setMaxEventsInput(maxEv)
53          return
54  
55 <    def psetWriter(self, name):
55 >    def skipEvent(self, skipEv):
56          """
57 <        Write out modified CMSSW.cfg
57 >        Set max event in the standalone untracked module
58          """
59 <
63 <        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
64 <        outFile = open(common.work_space.jobDir()+name,"w")
65 <        if name.endswith('py'):
66 <            outFile.write("import FWCore.ParameterSet.Config as cms\n")
67 <            outFile.write(self.cmsProcess.dumpPython())
68 <        else:
69 <            outFile.write(str(self.cfg))
70 <        outFile.close()
71 <
59 >        self.cfg.inputSource.setSkipEvents(skipEv)
60          return
61  
62 <    def addCrabFJR(self,name):
62 >    def psetWriter(self, name):
63          """
64 <        _addCrabFJR_
77 <        add CRAB specific FrameworkJobReport (FJR)
78 <        if a FJR already exists in input CMSSW parameter-set, add a second one.
79 <        This code is not needed for CMSSW >= 1.5.x and is non-functional in CMSSW >= 1.7.x.
80 <        It should be removed at some point in the future.
64 >        Write out modified CMSSW.py
65          """
66  
67 <        # Check if MessageLogger service already exists in configuration. If not, add it
68 <        svcs = self.cfg.data.services
69 <        if not svcs.has_key('MessageLogger'):
70 <            self.cfg.data.add_(CfgModules.Service("MessageLogger"))
71 <
72 <        messageLogger = self.cfg.data.services['MessageLogger']
73 <
74 <        # Add fwkJobReports to Message logger if it doesn't exist
75 <        if "fwkJobReports" not in messageLogger.parameterNames_():
76 <            messageLogger.fwkJobReports = CfgTypes.untracked(CfgTypes.vstring())
77 <
78 <        # should figure out how to remove "name" if it is there.
79 <
80 <        if name not in messageLogger.fwkJobReports:
81 <            messageLogger.fwkJobReports.append(name)
67 >        pklFileName=common.work_space.jobDir()+name+".pkl"
68 >        pklFile = open(pklFileName,"w")
69 >        myPickle = pickle.Pickler(pklFile)
70 >        myPickle.dump(self.cmsProcess)
71 >        pklFile.close()
72 >        pklFile = open(pklFileName,"rb")
73 >        outFile = open(common.work_space.jobDir()+name,"w")
74 >        outFile.write("import FWCore.ParameterSet.Config as cms\n")
75 >        outFile.write("import pickle\n")
76 >        outFile.write("pickledCfg=\"\"\"")
77 >        outFile.write(pklFile.read())
78 >        outFile.write("\"\"\"\n")
79 >        outFile.write("process = pickle.loads(pickledCfg)\n")
80 >        pklFile.close()
81 >        outFile.close()
82  
83          return
84  
# Line 107 | Line 91 | class PsetManipulator:
91              fileName = getattr(tFileService,'fileName',None).value()
92              return fileName
93          return None
94 <        
94 >
95      def getPoolOutputModule(self):
96          """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
97 <        if not self.cfg.data.outputModules:
98 <            return None
99 <        poolOutputModule = self.cfg.data.outputModules
100 <        for out in poolOutputModule:
101 <            return poolOutputModule[out].fileName.value()
97 >        outputFinder = PoolOutputFinder()
98 >        for p  in self.cfg.data.endpaths.itervalues():
99 >            p.visit(outputFinder)
100 >        return outputFinder.getList()
101 >
102 >    def getBadFilesSetting(self):
103 >        setting = False
104 >        try:
105 >            if self.cfg.data.source.skipBadFiles.value():
106 >                setting = True
107 >        except AttributeError:
108 >            pass # Either no source or no setting of skipBadFiles
109 >        return setting
110 >
111 > class PoolOutputFinder(object):
112 >
113 >    def __init__(self):
114 >        self._poolList = []
115 >    def enter(self,visitee):
116 >        if isinstance(visitee,OutputModule) and visitee.type_() == "PoolOutputModule":
117 >            self._poolList.append(visitee.fileName.value())
118 >    def leave(self,visitee):
119 >        pass
120  
121 +    def getList(self):
122 +        return self._poolList

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines