CMS 3D CMS Logo

crabWrapper.py
Go to the documentation of this file.
1 import sys
2 import os
3 import subprocess
4 from TkAlExceptions import AllInOneError
5 
6 # script which needs to be sourced for use of crab
7 crabSourceScript = '/afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh'
8 
9 # source the environment variables needed for crab
10 sourceStr = ( 'cd $CMSSW_BASE/src;'
11  'source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh;'
12  'eval `scramv1 runtime -sh`;'
13  'source ' + crabSourceScript + ' && env' )
14 sourceCmd = ['bash', '-c', sourceStr ]
15 sourceProc = subprocess.Popen(sourceCmd, stdout = subprocess.PIPE)
16 for line in sourceProc.stdout:
17  (key, _, value) = line.partition("=")
18  os.environ[key] = value.replace("\n","")
19 sourceProc.communicate()
20 
21 # source variables from crab wrapper script
22 crabFile = open('/'.join([os.environ["CRABPYTHON"],'crab']))
23 theLines = crabFile.readlines()
24 theLine = []
25 for line in theLines:
26  if ( line[0] == '#' ) or \
27  ( line == ' python $CRABPYTHON/crab.py $*\n' ):
28  continue
29  theLine.append( line )
30 tempFilePath = "tempCrab"
31 tempFile = open( tempFilePath, "w" )
32 tempFile.write( ''.join(theLine) )
33 tempFile.close()
34 crabStr = ('source tempCrab && env' )
35 crabCmd = ['bash', '-c', crabStr ]
36 crabProc = subprocess.Popen(crabCmd, stdout = subprocess.PIPE)
37 for line in crabProc.stdout:
38  (key, _, value) = line.partition("=")
39  os.environ[key] = value.replace("\n","")
40 crabProc.communicate()
41 os.remove( tempFilePath )
42 
43 # add sourced paths to search path of python
44 sys.path.extend( os.environ["PYTHONPATH"].split( ':' ) )
45 
46 import crab
47 import crab_exceptions
48 
50  def run( self, options ):
51  theCrab = crab.Crab()
52  try:
53  theCrab.initialize_( options )
54  theCrab.run()
55  except crab_exceptions.CrabException as e:
56  raise AllInOneError( str( e ) )
57  del theCrab
58 
59 
60 if __name__ == "__main__":
61  theCrab = CrabWrapper()
62  theCrabOptions = {"-create":"",
63  "-cfg":"TkAlOfflineValidation.shiftPlots.crab.cfg"}
64  theCrab.run( theCrabOptions )
65 
66  theCrabOptions = {"-submit":""}
67  theCrab.run( theCrabOptions )
68 
69  theCrabOptions = {"-status":""}
70  theCrab.run( theCrabOptions )
71 
72  theCrabOptions = {"-getoutput":""}
73  try:
74  theCrab.run( theCrabOptions )
75  except AllInOneError as e:
76  print "crab: ", e
def run(self, options)
Definition: crabWrapper.py:50
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
double split
Definition: MVATrainer.cc:139