CMS 3D CMS Logo

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