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