CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Attributes
CrabHelper.CrabHelper Class Reference
Inheritance diagram for CrabHelper.CrabHelper:
DTWorkflow.DTWorkflow DTT0WireWorkflow.DTT0WireWorkflow DTTtrigWorkflow.DTttrigWorkflow DTVdriftWorkflow.DTvdriftWorkflow

Public Member Functions

def __init__ (self)
 
def cert_info (self)
 
def check_crabtask (self)
 
def crab (self)
 
def crab_config_filename (self)
 
def crab_config_filepath (self)
 
def crab_taskname (self)
 
def create_crab_config (self)
 
def fill_options_from_crab_config (self)
 
def submit_crab_task (self)
 
def voms_proxy_create (self, passphrase=None)
 
def voms_proxy_time_left (self)
 
def write_crabConfig (self)
 

Public Attributes

 crab_config
 
 crabFunctions
 

Private Attributes

 _cert_info
 
 _crab
 

Detailed Description

Definition at line 12 of file CrabHelper.py.

Constructor & Destructor Documentation

◆ __init__()

def CrabHelper.CrabHelper.__init__ (   self)

Definition at line 14 of file CrabHelper.py.

14  def __init__(self):
15  # perform imports only when creating instance. This allows to use the classmethods e.g.for
16  # CLI construction before crab is sourced.
17  self.crabFunctions = import_module('CalibMuon.DTCalibration.Workflow.Crabtools.crabFunctions')
18  # cached member variables
19  self._crab = None
20  self._cert_info = None
21 

Member Function Documentation

◆ cert_info()

def CrabHelper.CrabHelper.cert_info (   self)

Definition at line 213 of file CrabHelper.py.

213  def cert_info(self):
214  if not self._cert_info:
215  if not self.voms_proxy_time_left() > 0:
216  warn_msg = "No valid proxy, a default proxy without a specific"
217  warn_msg = "VOGroup will be used"
218  self.voms_proxy_create()
219  log.warning(warn_msg)
220  self._cert_info = self.crabFunctions.CertInfo()
221  return self._cert_info
222 

References CrabHelper.CrabHelper._cert_info, CrabHelper.CrabHelper.crabFunctions, CrabHelper.CrabHelper.voms_proxy_create(), and CrabHelper.CrabHelper.voms_proxy_time_left().

Referenced by CrabHelper.CrabHelper.crab().

◆ check_crabtask()

def CrabHelper.CrabHelper.check_crabtask (   self)

Definition at line 50 of file CrabHelper.py.

50  def check_crabtask(self):
51  print(self.crab_config_filepath)
52  task = self.crabFunctions.CrabTask(crab_config = self.crab_config_filepath,
53  initUpdate = False)
54  if self.options.no_exec:
55  log.info("Nothing to check in no-exec mode")
56  return True
57  for n_check in range(self.options.max_checks):
58  task.update()
59  if task.state in ( "COMPLETED"):
60  print("Crab task complete. Getting output locally")
61  output_path = os.path.join( self.local_path, "unmerged_results" )
62  self.get_output_files(task, output_path)
63  return True
64  if task.state in ("SUBMITFAILED", "FAILED"):
65  print("Crab task failed")
66  return False
67  possible_job_states = ["nUnsubmitted",
68  "nIdle",
69  "nRunning",
70  "nTransferring",
71  "nCooloff",
72  "nFailed",
73  "nFinished",
74  "nComplete" ]
75 
76  jobinfos = ""
77  for jobstate in possible_job_states:
78  njobs_in_state = getattr(task, jobstate)
79  if njobs_in_state > 0:
80  jobinfos+="%s: %d " % (jobstate[1:], njobs_in_state)
81 
82  #clear line for reuse
83  sys.stdout.write("\r")
84  sys.stdout.write("".join([" " for i in range(tools.getTerminalSize()[0])]))
85  sys.stdout.write("\r")
86  prompt_text = "Check (%d/%d). Task state: %s (%s). Press q and enter to stop checks: " % (n_check,
87  self.options.max_checks, task.state, jobinfos)
88  user_input = tools.stdinWait(prompt_text, "", self.options.check_interval)
89  if user_input in ("q","Q"):
90  return False
91  print("Task not completed after %d checks (%d minutes)" % ( self.options.max_checks,
92  int( self.options.check_interval / 60. )))
93  return False
94 

References CrabHelper.CrabHelper.crab_config_filepath(), CrabHelper.CrabHelper.crabFunctions, DTWorkflow.DTWorkflow.get_output_files(), tools.getTerminalSize(), createfilelist.int, join(), DTWorkflow.DTWorkflow.local_path(), cmsswPreprocessor.CmsswPreprocessor.options, DTCalibrationWorker.DTCalibrationWorker.options, DTWorkflow.DTWorkflow.options, TestProcess.TestProcess.options, confdb.HLTProcess.options, VisualizationOptions.options(), production_tasks.Task.options, fireworks::OptionNode.options(), edmIntegrityCheck.IntegrityCheck.options, Config.Process.options, validateAlignments.ValidationJobMultiIOV.options, print(), FastTimerService_cff.range, and tools.stdinWait().

Referenced by DTWorkflow.DTWorkflow.check().

◆ crab()

def CrabHelper.CrabHelper.crab (   self)
Retuns a CrabController instance from cache or creates new
   on on first call 

Definition at line 202 of file CrabHelper.py.

202  def crab(self):
203  """ Retuns a CrabController instance from cache or creates new
204  on on first call """
205  if self._crab is None:
206  if self.cert_info.voGroup:
207  self._crab = self.crabFunctions.CrabController(voGroup = self.cert_info.voGroup)
208  else:
209  self._crab = self.crabFunctions.CrabController()
210  return self._crab
211 

References CrabHelper.CrabHelper._crab, CrabHelper.CrabHelper.cert_info(), and CrabHelper.CrabHelper.crabFunctions.

Referenced by DTWorkflow.DTWorkflow.get_output_files(), CrabHelper.CrabHelper.submit_crab_task(), and DTWorkflow.DTWorkflow.user().

◆ crab_config_filename()

def CrabHelper.CrabHelper.crab_config_filename (   self)

◆ crab_config_filepath()

def CrabHelper.CrabHelper.crab_config_filepath (   self)

◆ crab_taskname()

def CrabHelper.CrabHelper.crab_taskname (   self)

Definition at line 235 of file CrabHelper.py.

235  def crab_taskname(self):
236  taskname = self.options.label + "_" + self.options.workflow + "_"
237  if hasattr( self.options, "workflow_mode"):
238  taskname+= self.options.workflow_mode + "_"
239  taskname += "run_" + str(self.options.run) + "_v" + str(self.options.trial)
240  return taskname
241 

References cmsswPreprocessor.CmsswPreprocessor.options, DTCalibrationWorker.DTCalibrationWorker.options, DTWorkflow.DTWorkflow.options, TestProcess.TestProcess.options, confdb.HLTProcess.options, VisualizationOptions.options(), production_tasks.Task.options, fireworks::OptionNode.options(), edmIntegrityCheck.IntegrityCheck.options, Config.Process.options, validateAlignments.ValidationJobMultiIOV.options, and str.

Referenced by CrabHelper.CrabHelper.crab_config_filename().

◆ create_crab_config()

def CrabHelper.CrabHelper.create_crab_config (   self)
Create a crab config object dependent on the chosen command option

Definition at line 120 of file CrabHelper.py.

120  def create_crab_config(self):
121  """ Create a crab config object dependent on the chosen command option"""
122  from CalibMuon.DTCalibration.Workflow.Crabtools.crabConfigParser import CrabConfigParser
123  self.crab_config = CrabConfigParser()
124  """ Fill common options in crab config """
125 
126  self.crab_config.add_section('General')
127  if "/" in self.crab_taskname:
128  raise ValueError( 'Sample contains "/" which is not allowed' )
129  self.crab_config.set( 'General', 'requestName', self.crab_taskname )
130  self.crab_config.set( 'General', 'workArea', self.local_path)
131  if self.options.no_log:
132  self.crab_config.set( 'General', 'transferLogs', 'False' )
133  else:
134  self.crab_config.set( 'General', 'transferLogs', 'True' )
135 
136  self.crab_config.add_section('JobType')
137  self.crab_config.set( 'JobType', 'pluginName', 'Analysis' )
138  self.crab_config.set( 'JobType', 'psetName', self.pset_path )
139  self.crab_config.set( 'JobType', 'outputFiles', self.output_files)
140  if self.input_files:
141  self.crab_config.set( 'JobType', 'inputFiles', self.input_files)
142 
143  self.crab_config.add_section('Data')
144  self.crab_config.set('Data', 'inputDataset', self.options.datasetpath)
145  # set job splitting options
146  if self.options.datasettype =="MC":
147  self.crab_config.set('Data', 'splitting', 'FileBased')
148  self.crab_config.set('Data', 'unitsPerJob', str(self.options.filesPerJob) )
149  else:
150  self.crab_config.set('Data', 'splitting', 'LumiBased')
151  self.crab_config.set('Data', 'unitsPerJob', str(self.options.lumisPerJob) )
152  if self.options.runselection:
153  self.crab_config.set( "Data",
154  "runRange",
155  ",".join( self.options.runselection )
156  )
157  # set output path in compliance with crab3 structure
158  self.crab_config.set('Data', 'publication', False)
159  self.crab_config.set('Data', 'outputDatasetTag', self.remote_out_path["outputDatasetTag"])
160  self.crab_config.set('Data', 'outLFNDirBase', self.remote_out_path["outLFNDirBase"] )
161 
162  # set site section options
163  self.crab_config.add_section('Site')
164  self.crab_config.set('Site', 'storageSite', self.options.output_site)
165  self.crab_config.set('Site', 'whitelist', self.options.ce_white_list)
166  self.crab_config.set('Site', 'blacklist', self.options.ce_black_list)
167 
168  #set user section options if necessary
169 # if self.cert_info.voGroup or self.cert_info.voRole:
170 # self.crab_config.add_section('User')
171 # if self.cert_info.voGroup:
172 # self.crab_config.set('User', "voGroup", self.cert_info.voGroup)
173 # if self.cert_info.voRole:
174 # self.crab_config.set('User', "voRole", self.cert_info.voRole)
175  log.debug("Created crab config: %s " % self.crab_config_filename)
176 

Referenced by CrabHelper.CrabHelper.submit_crab_task().

◆ fill_options_from_crab_config()

def CrabHelper.CrabHelper.fill_options_from_crab_config (   self)

Definition at line 189 of file CrabHelper.py.

189  def fill_options_from_crab_config(self):
190  crabtask = CrabTask( crab_config = self.crab_config_filename )
191  splitinfo = crabtask.crabConfig.Data.outputDatasetTag.split("_")
192  run, trial = splitinfo[0].split("Run")[-1], splitinfo[1].split("v")[-1]
193  if not self.options.run:
194  self.options.run = int(run)
195  self.options.trail = int(trial)
196  if not hasattr(self.options, "datasetpath"):
197  self.options.datasetpath = crabtask.crabConfig.Data.inputDataset
198  if not hasattr(self.options, "label"):
199  self.options.label = crabtask.crabConfig.General.requestName.split("_")[0]
200 

References CrabHelper.CrabHelper.crab_config_filename(), createfilelist.int, cmsswPreprocessor.CmsswPreprocessor.options, DTCalibrationWorker.DTCalibrationWorker.options, DTWorkflow.DTWorkflow.options, TestProcess.TestProcess.options, confdb.HLTProcess.options, VisualizationOptions.options(), production_tasks.Task.options, fireworks::OptionNode.options(), edmIntegrityCheck.IntegrityCheck.options, Config.Process.options, validateAlignments.ValidationJobMultiIOV.options, and submitPVValidationJobs.split().

◆ submit_crab_task()

def CrabHelper.CrabHelper.submit_crab_task (   self)

Definition at line 22 of file CrabHelper.py.

22  def submit_crab_task(self):
23  # create a crab config
24  log.info("Creating crab config")
25  self.create_crab_config()
26  #write crab config
27  full_crab_config_filename = self.write_crabConfig()
28  if self.options.no_exec:
29  log.info("Runing with option no-exec exiting")
30  return True
31  #submit crab job
32  log.info("Submitting crab job")
33  self.crab.submit(full_crab_config_filename)
34  log.info("crab job submitted. Waiting 120 seconds before first status call")
35  time.sleep( 120 )
36 
37  task = self.crabFunctions.CrabTask(crab_config = full_crab_config_filename)
38  task.update()
39  if task.state =="UNKNOWN":
40  time.sleep( 30 )
41  task.update()
42  success_states = ( 'QUEUED', 'SUBMITTED', "COMPLETED", "FINISHED")
43  if task.state in success_states:
44  log.info("Job in state %s" % task.state )
45  return True
46  else:
47  log.error("Job submission not successful, crab state:%s" % task.state)
48  raise RuntimeError("Job submission not successful, crab state:%s" % task.state)
49 

References CrabHelper.CrabHelper.crab(), CrabHelper.CrabHelper.crabFunctions, CrabHelper.CrabHelper.create_crab_config(), cmsswPreprocessor.CmsswPreprocessor.options, DTCalibrationWorker.DTCalibrationWorker.options, DTWorkflow.DTWorkflow.options, TestProcess.TestProcess.options, confdb.HLTProcess.options, VisualizationOptions.options(), production_tasks.Task.options, fireworks::OptionNode.options(), edmIntegrityCheck.IntegrityCheck.options, Config.Process.options, validateAlignments.ValidationJobMultiIOV.options, and CrabHelper.CrabHelper.write_crabConfig().

Referenced by DTWorkflow.DTWorkflow.submit().

◆ voms_proxy_create()

def CrabHelper.CrabHelper.voms_proxy_create (   self,
  passphrase = None 
)

Definition at line 105 of file CrabHelper.py.

105  def voms_proxy_create(self, passphrase = None):
106  voms = 'cms'
107  if passphrase:
108  p = subprocess.Popen(['voms-proxy-init', '--voms', voms, '--valid', '192:00'],
109  stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
110  stdout = p.communicate(input=passphrase+'\n')[0]
111  retcode = p.returncode
112  if not retcode == 0:
113  raise ProxyError('Proxy initialization command failed: %s'%stdout)
114  else:
115  retcode = subprocess.call(['voms-proxy-init', '--voms', voms, '--valid', '192:00'])
116  if not retcode == 0:
117  raise ProxyError('Proxy initialization command failed.')
118 
119 

Referenced by CrabHelper.CrabHelper.cert_info().

◆ voms_proxy_time_left()

def CrabHelper.CrabHelper.voms_proxy_time_left (   self)

Definition at line 95 of file CrabHelper.py.

95  def voms_proxy_time_left(self):
96  process = subprocess.Popen(['voms-proxy-info', '-timeleft'],
97  stdout=subprocess.PIPE,
98  stderr=subprocess.STDOUT)
99  stdout = process.communicate()[0]
100  if process.returncode != 0:
101  return 0
102  else:
103  return int(stdout)
104 

References createfilelist.int.

Referenced by CrabHelper.CrabHelper.cert_info().

◆ write_crabConfig()

def CrabHelper.CrabHelper.write_crabConfig (   self)
Write crab config file in working dir with label option as name 

Definition at line 177 of file CrabHelper.py.

177  def write_crabConfig(self):
178  """ Write crab config file in working dir with label option as name """
179  base_path = os.path.join( self.options.working_dir,self.local_path)
180  filename = os.path.join( base_path, self.crab_config_filename)
181  if not os.path.exists(base_path):
182  os.makedirs(base_path)
183  if os.path.exists(filename):
184  raise IOError("file %s alrady exits"%(filename))
185  self.crab_config.writeCrabConfig(filename)
186  log.info( 'created crab config file %s'%filename )
187  return filename
188 

References CrabHelper.CrabHelper.crab_config, CrabHelper.CrabHelper.crab_config_filename(), DTWorkflow.DTWorkflow.local_path(), cmsswPreprocessor.CmsswPreprocessor.options, DTCalibrationWorker.DTCalibrationWorker.options, DTWorkflow.DTWorkflow.options, TestProcess.TestProcess.options, confdb.HLTProcess.options, VisualizationOptions.options(), production_tasks.Task.options, fireworks::OptionNode.options(), edmIntegrityCheck.IntegrityCheck.options, Config.Process.options, and validateAlignments.ValidationJobMultiIOV.options.

Referenced by CrabHelper.CrabHelper.submit_crab_task().

Member Data Documentation

◆ _cert_info

CrabHelper.CrabHelper._cert_info
private

Definition at line 20 of file CrabHelper.py.

Referenced by CrabHelper.CrabHelper.cert_info().

◆ _crab

CrabHelper.CrabHelper._crab
private

Definition at line 19 of file CrabHelper.py.

Referenced by CrabHelper.CrabHelper.crab().

◆ crab_config

CrabHelper.CrabHelper.crab_config

Definition at line 123 of file CrabHelper.py.

Referenced by CrabHelper.CrabHelper.write_crabConfig().

◆ crabFunctions

CrabHelper.CrabHelper.crabFunctions
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
edmPickEvents.crab
crab
Definition: edmPickEvents.py:295
CrabConfigParser
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
submit
Definition: submit.py:1
submitPVValidationJobs.split
def split(sequence, size)
Definition: submitPVValidationJobs.py:352
str
#define str(s)
Definition: TestProcessor.cc:52
tools.stdinWait
def stdinWait(text, default, time, timeoutDisplay=None, **kwargs)
Definition: tools.py:70
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
createfilelist.int
int
Definition: createfilelist.py:10
tools.getTerminalSize
def getTerminalSize()
Definition: tools.py:96
cmsHarvester.create_crab_config
def create_crab_config(self)
Definition: cmsHarvester.py:4233