1# Copyright 2010 Google Inc. All Rights Reserved. 2# 3 4import getpass 5import os 6 7from automation.common.state_machine import BasicStateMachine 8 9STATUS_NOT_EXECUTED = 'NOT_EXECUTED' 10STATUS_EXECUTING = 'EXECUTING' 11STATUS_SUCCEEDED = 'SUCCEEDED' 12STATUS_FAILED = 'FAILED' 13 14 15class JobGroupStateMachine(BasicStateMachine): 16 state_machine = { 17 STATUS_NOT_EXECUTED: [STATUS_EXECUTING], 18 STATUS_EXECUTING: [STATUS_SUCCEEDED, STATUS_FAILED] 19 } 20 21 final_states = [STATUS_SUCCEEDED, STATUS_FAILED] 22 23 24class JobGroup(object): 25 HOMEDIR_PREFIX = os.path.join('/home', getpass.getuser(), 'www', 'automation') 26 27 def __init__(self, 28 label, 29 jobs=None, 30 cleanup_on_completion=True, 31 cleanup_on_failure=False, 32 description=''): 33 self._state = JobGroupStateMachine(STATUS_NOT_EXECUTED) 34 self.id = 0 35 self.label = label 36 self.jobs = [] 37 self.cleanup_on_completion = cleanup_on_completion 38 self.cleanup_on_failure = cleanup_on_failure 39 self.description = description 40 41 if jobs: 42 for job in jobs: 43 self.AddJob(job) 44 45 def _StateGet(self): 46 return self._state 47 48 def _StateSet(self, new_state): 49 self._state.Change(new_state) 50 51 status = property(_StateGet, _StateSet) 52 53 @property 54 def home_dir(self): 55 return os.path.join(self.HOMEDIR_PREFIX, 'job-group-%d' % self.id) 56 57 @property 58 def time_submitted(self): 59 try: 60 return self.status.timeline[1].time_started 61 except IndexError: 62 return None 63 64 def __repr__(self): 65 return '{%s: %s}' % (self.__class__.__name__, self.id) 66 67 def __str__(self): 68 return '\n'.join(['Job-Group:', 'ID: %s' % self.id] + [str( 69 job) for job in self.jobs]) 70 71 def AddJob(self, job): 72 self.jobs.append(job) 73 job.group = self 74