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.26 by ewv, Tue Feb 24 16:19:25 2009 UTC vs.
Revision 1.36 by ewv, Thu Apr 8 18:25:57 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 + 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 <        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
30 <        if self.pset.endswith('py'):
31 <            handle = open(self.pset, 'r')
32 <            try:   # Nested form for Python < 2.5
33 <                try:
34 <                    self.cfo = imp.load_source("pycfg", self.pset, handle)
35 <                    self.cmsProcess = self.cfo.process
36 <                except Exception, ex:
37 <                    msg = "Your config file is not valid python: %s" % str(ex)
38 <                    raise CrabException(msg)
39 <            finally:
40 <                handle.close()
41 <        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)
47 <                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
48 <                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 +            if self.cfg.data.MessageLogger.cerr.FwkReport.reportEvery.value() < 100:
45 +                self.cfg.data.MessageLogger.cerr.FwkReport.reportEvery = cms.untracked.int32(100)
46 +        except AttributeError:
47 +            pass
48  
49      def maxEvent(self, maxEv):
50          """
# Line 65 | Line 62 | class PsetManipulator:
62  
63      def psetWriter(self, name):
64          """
65 <        Write out modified CMSSW.cfg
65 >        Write out modified CMSSW.py
66          """
67  
68 <        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
69 <        outFile = open(common.work_space.jobDir()+name,"w")
70 <        if name.endswith('py'):
71 <            outFile.write("import FWCore.ParameterSet.Config as cms\n")
72 <            try:
73 <                outFile.write(self.cmsProcess.dumpPython())
74 <            except Exception, ex:
75 <                msg =  "Your cfg file is not valid, %s\n" % str(ex)
76 <                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
77 <                msg += "  may help you understand the problem."
81 <                raise CrabException(msg)
82 <
83 <        else:
84 <            outFile.write(self.cfg.data.dumpConfig())
68 >        pklFileName = common.work_space.jobDir() + name + ".pkl"
69 >        pklFile = open(pklFileName, "wb")
70 >        myPickle = pickle.Pickler(pklFile)
71 >        myPickle.dump(self.cmsProcess)
72 >        pklFile.close()
73 >
74 >        outFile = open(common.work_space.jobDir()+name, "w")
75 >        outFile.write("import FWCore.ParameterSet.Config as cms\n")
76 >        outFile.write("import pickle\n")
77 >        outFile.write("process = pickle.load(open('%s', 'rb'))\n" % (name + ".pkl"))
78          outFile.close()
79  
80 +
81          return
82  
83      def getTFileService(self):
# Line 98 | Line 92 | class PsetManipulator:
92  
93      def getPoolOutputModule(self):
94          """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
95 <        if not self.cfg.data.outputModules:
96 <            return None
97 <        poolOutputModule = self.cfg.data.outputModules
98 <        for out in poolOutputModule:
99 <            return poolOutputModule[out].fileName.value()
95 >        outputFinder = PoolOutputFinder()
96 >        for p  in self.cfg.data.endpaths.itervalues():
97 >            p.visit(outputFinder)
98 >        return outputFinder.getList()
99 >
100 >    def getBadFilesSetting(self):
101 >        setting = False
102 >        try:
103 >            if self.cfg.data.source.skipBadFiles.value():
104 >                setting = True
105 >        except AttributeError:
106 >            pass # Either no source or no setting of skipBadFiles
107 >        return setting
108 >
109 > class PoolOutputFinder(object):
110 >
111 >    def __init__(self):
112 >        self._poolList = []
113 >    def enter(self,visitee):
114 >        if isinstance(visitee,OutputModule) and visitee.type_() == "PoolOutputModule":
115 >            filename=visitee.fileName.value().split(":")[-1]
116 >            self._poolList.append(filename)
117 >    def leave(self,visitee):
118 >        pass
119  
120 +    def getList(self):
121 +        return self._poolList

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines