CMS 3D CMS Logo

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