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.2 by slacapra, Thu Jun 22 16:05:18 2006 UTC vs.
Revision 1.31 by ewv, Tue Jul 21 21:19:05 2009 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 <                                                                                                                                                            
3 < import os, sys, commands
2 >
3 > import os
4   import common
5 + import imp
6 + import pickle
7 +
8   from crab_util import *
9   from crab_exceptions import *
10  
11 < # Crabpydir=commands.getoutput('which crab')
12 < # Topdir=string.replace(Crabpydir,'/python/crab','')
13 < # sys.path.append(Topdir+'/PsetCode')
11 > from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
12 > from FWCore.ParameterSet.DictTypes import SortedKeysDict
13 > from FWCore.ParameterSet.Modules   import Service
14 > from FWCore.ParameterSet.Types     import *
15 > from FWCore.ParameterSet.Modules   import OutputModule
16  
17 < from cmsconfig import cmsconfig
18 < from CfgInterface import CfgInterface
17 > import FWCore.ParameterSet.Types   as CfgTypes
18 > import FWCore.ParameterSet.Modules as CfgModules
19  
20   class PsetManipulator:
21      def __init__(self, pset):
22 <        """
23 <        Convert Pset in Python format  
19 <        and initialize  
22 >        """
23 >        Read in Pset object and initialize
24          """
25  
26          self.pset = pset
27          #convert Pset
28 <        self.pyPset = os.path.basename(pset)  
29 <        cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset
30 <        #cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pset+'py < '+ self.pset
31 <        cmd_out = runCommand(cmd)  
32 <        if cmd_out != '':
33 <            msg = 'Could not convert Pset.cfg into python Dictionary \n'
34 <            msg1= '      Did you do eval `scramv1 runtime ...` from your CMSSW working area ?'
35 <            raise CrabException(msg+msg1)
36 <            pass
37 <        
38 <        self.par = file(common.work_space.shareDir()+self.pyPset+'py').read()
39 <       # par = file(common.work_space.shareDir()+self.pset+'py').read()
40 <
41 <        # get PSet
42 <        self.cfg = CfgInterface(self.par,True)
43 <
44 <    def inputModule(self, source):
45 <        """ Clean  String FileName if there
46 <            and add  vString Filenames key
47 <        """
48 <        # set input module
49 <        inModule = self.cfg.inputSource
50 <        inModule.cleanStringFileNames() ## Add Daniele
51 <        inModule.setFileNames(source)
48 <        return
49 <  
50 <    def pythiaSeed(self,seed):
51 <        """
52 <            Set pythia seed key
53 <        """
54 <        # set seed
55 <        inModule = self.cfg.inputSource
56 <        inModule.setPythiaSeed(self.cfg,seed)
57 <        return
28 >        from FWCore.ParameterSet.Config import include
29 >        common.logger.debug("PsetManipulator::__init__: PSet file = "+self.pset)
30 >        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
31 >        if self.pset.endswith('py'):
32 >            handle = open(self.pset, 'r')
33 >            try:   # Nested form for Python < 2.5
34 >                try:
35 >                    self.cfo = imp.load_source("pycfg", self.pset, handle)
36 >                    self.cmsProcess = self.cfo.process
37 >                except Exception, ex:
38 >                    msg = "Your config file is not valid python: %s" % str(ex)
39 >                    raise CrabException(msg)
40 >            finally:
41 >                handle.close()
42 >        else:
43 >            try:
44 >                self.cfo = include(self.pset)
45 >                self.cmsProcess = self.cfo
46 >            except Exception, ex:
47 >                msg =  "Your cfg file is not valid, %s\n" % str(ex)
48 >                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
49 >                msg += "  may help you understand the problem."
50 >                raise CrabException(msg)
51 >        self.cfg = CfgInterface(self.cmsProcess)
52  
53      def maxEvent(self, maxEv):
54 <        """ """
55 <        # set input module
56 <        inModule = self.cfg.inputSource
57 <        inModule.cleanMaxEvent()  
64 <        inModule.setMaxEvents(maxEv)   ## Add Daniele
54 >        """
55 >        Set max event in the standalone untracked module
56 >        """
57 >        self.cfg.maxEvents.setMaxEventsInput(maxEv)
58          return
59  
60 <    def outputModule(self, output):
61 <
62 <        #set output module
63 <        outModule = self.cfg.outputModules['out']
64 <        outModule.setFileName('file:'+str(output))
72 <
60 >    def skipEvent(self, skipEv):
61 >        """
62 >        Set max event in the standalone untracked module
63 >        """
64 >        self.cfg.inputSource.setSkipEvents(skipEv)
65          return
66  
67      def psetWriter(self, name):
68 +        """
69 +        Write out modified CMSSW.cfg
70 +        """
71  
72 <        configObject = cmsconfig(str(self.cfg))
73 <
74 <        file1 = open(common.work_space.jobDir()+name,"w")
75 <        file1.write(str(configObject.asConfigurationString()))
76 <        file1.close()
72 >        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
73 >        outFile = open(common.work_space.jobDir()+name,"w")
74 >        if name.endswith('py'):
75 >            outFile.write("import FWCore.ParameterSet.Config as cms\n")
76 >            outFile.write("import pickle\n")
77 >            outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(self.cmsProcess))
78 >            outFile.write("process = pickle.loads(pickledCfg)\n")
79 >        else:
80 >            outFile.write(self.cfg.data.dumpConfig())
81 >        outFile.close()
82  
83          return
84 +
85 +    def getTFileService(self):
86 +        """ Get Output filename from TFileService and return it. If not existing, return None """
87 +        if not self.cfg.data.services.has_key('TFileService'):
88 +            return None
89 +        tFileService = self.cfg.data.services['TFileService']
90 +        if "fileName" in tFileService.parameterNames_():
91 +            fileName = getattr(tFileService,'fileName',None).value()
92 +            return fileName
93 +        return None
94 +
95 +    def getPoolOutputModule(self):
96 +        """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
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