CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/Alignment/OfflineValidation/python/TkAlAllInOneTool/crabWrapper.py

Go to the documentation of this file.
00001 import sys
00002 import os
00003 import subprocess
00004 from TkAlExceptions import AllInOneError
00005 
00006 # script which needs to be sourced for use of crab
00007 crabSourceScript = '/afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh'
00008 
00009 # source the environment variables needed for crab
00010 sourceStr = ( 'cd $CMSSW_BASE/src;'
00011               'source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh;'
00012               'eval `scramv1 runtime -sh`;'
00013               'source ' + crabSourceScript + ' && env' )
00014 sourceCmd = ['bash', '-c', sourceStr ]
00015 sourceProc = subprocess.Popen(sourceCmd, stdout = subprocess.PIPE)
00016 for line in sourceProc.stdout:
00017     (key, _, value) = line.partition("=")
00018     os.environ[key] = value.replace("\n","")
00019 sourceProc.communicate()
00020 
00021 # source variables from crab wrapper script
00022 crabFile = open('/'.join([os.environ["CRABPYTHON"],'crab']))
00023 theLines = crabFile.readlines()
00024 theLine = []
00025 for line in theLines:
00026     if ( line[0] == '#' ) or \
00027            ( line == '  python $CRABPYTHON/crab.py $*\n' ):
00028         continue
00029     theLine.append( line )
00030 tempFilePath = "tempCrab"
00031 tempFile = open( tempFilePath, "w" )
00032 tempFile.write( ''.join(theLine) )
00033 tempFile.close()
00034 crabStr = ('source tempCrab && env' )
00035 crabCmd = ['bash', '-c', crabStr ]
00036 crabProc = subprocess.Popen(crabCmd, stdout = subprocess.PIPE)
00037 for line in crabProc.stdout:
00038     (key, _, value) = line.partition("=")
00039     os.environ[key] = value.replace("\n","")
00040 crabProc.communicate()
00041 os.remove( tempFilePath )
00042 
00043 # add sourced paths to search path of python
00044 sys.path.extend( os.environ["PYTHONPATH"].split( ':' ) )
00045 
00046 import crab
00047 import crab_exceptions
00048 
00049 class CrabWrapper:
00050     def __init__( self ):
00051         pass
00052 
00053     def run( self, options ):
00054         theCrab = crab.Crab()
00055         try:
00056             theCrab.initialize_( options )
00057             theCrab.run()
00058         except crab_exceptions.CrabException, e:
00059             raise AllInOneError( str( e ) )
00060         del theCrab
00061 
00062 
00063 if __name__ == "__main__":
00064     theCrab = CrabWrapper()
00065     theCrabOptions = {"-create":"",
00066                       "-cfg":"TkAlOfflineValidation.shiftPlots.crab.cfg"}
00067     theCrab.run( theCrabOptions )
00068     
00069     theCrabOptions = {"-submit":""}
00070     theCrab.run( theCrabOptions )
00071     
00072     theCrabOptions = {"-status":""}
00073     theCrab.run( theCrabOptions )
00074 
00075     theCrabOptions = {"-getoutput":""}
00076     try:
00077         theCrab.run( theCrabOptions )
00078     except AllInOneError, e:
00079         print "crab: ", e