Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:45:59

0001 from __future__ import print_function
0002 from __future__ import absolute_import
0003 import sys
0004 import os
0005 import subprocess
0006 from .TkAlExceptions import AllInOneError
0007 
0008 # script which needs to be sourced for use of crab
0009 crabSourceScript = '/afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh'
0010 
0011 # source the environment variables needed for crab
0012 sourceStr = ( 'cd $CMSSW_BASE/src;'
0013               'source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh;'
0014               'eval `scramv1 runtime -sh`;'
0015               'source ' + crabSourceScript + ' && env' )
0016 sourceCmd = ['bash', '-c', sourceStr ]
0017 sourceProc = subprocess.Popen(sourceCmd, stdout = subprocess.PIPE)
0018 for line in sourceProc.stdout:
0019     (key, _, value) = line.partition("=")
0020     os.environ[key] = value.replace("\n","")
0021 sourceProc.communicate()
0022 
0023 # source variables from crab wrapper script
0024 crabFile = open('/'.join([os.environ["CRABPYTHON"],'crab']))
0025 theLines = crabFile.readlines()
0026 theLine = []
0027 for line in theLines:
0028     if ( line[0] == '#' ) or \
0029            ( line == '  python $CRABPYTHON/crab.py $*\n' ):
0030         continue
0031     theLine.append( line )
0032 tempFilePath = "tempCrab"
0033 tempFile = open( tempFilePath, "w" )
0034 tempFile.write( ''.join(theLine) )
0035 tempFile.close()
0036 crabStr = ('source tempCrab && env' )
0037 crabCmd = ['bash', '-c', crabStr ]
0038 crabProc = subprocess.Popen(crabCmd, stdout = subprocess.PIPE)
0039 for line in crabProc.stdout:
0040     (key, _, value) = line.partition("=")
0041     os.environ[key] = value.replace("\n","")
0042 crabProc.communicate()
0043 os.remove( tempFilePath )
0044 
0045 # add sourced paths to search path of python
0046 sys.path.extend( os.environ["PYTHONPATH"].split( ':' ) )
0047 
0048 import crab
0049 import crab_exceptions
0050 
0051 class CrabWrapper(object):
0052     def run( self, options ):
0053         theCrab = crab.Crab()
0054         try:
0055             theCrab.initialize_( options )
0056             theCrab.run()
0057         except crab_exceptions.CrabException as e:
0058             raise AllInOneError( str( e ) )
0059         del theCrab
0060 
0061 
0062 if __name__ == "__main__":
0063     theCrab = CrabWrapper()
0064     theCrabOptions = {"-create":"",
0065                       "-cfg":"TkAlOfflineValidation.shiftPlots.crab.cfg"}
0066     theCrab.run( theCrabOptions )
0067     
0068     theCrabOptions = {"-submit":""}
0069     theCrab.run( theCrabOptions )
0070     
0071     theCrabOptions = {"-status":""}
0072     theCrab.run( theCrabOptions )
0073 
0074     theCrabOptions = {"-getoutput":""}
0075     try:
0076         theCrab.run( theCrabOptions )
0077     except AllInOneError as e:
0078         print("crab: ", e)