1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer 3*4882a593Smuzhiyun# Copyright (C) 2006 - 2007 Richard Purdie 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 6*4882a593Smuzhiyun# 7*4882a593Smuzhiyun 8*4882a593Smuzhiyunimport bb.build 9*4882a593Smuzhiyunimport time 10*4882a593Smuzhiyun 11*4882a593Smuzhiyunclass BBUIHelper: 12*4882a593Smuzhiyun def __init__(self): 13*4882a593Smuzhiyun self.needUpdate = False 14*4882a593Smuzhiyun self.running_tasks = {} 15*4882a593Smuzhiyun # Running PIDs preserves the order tasks were executed in 16*4882a593Smuzhiyun self.running_pids = [] 17*4882a593Smuzhiyun self.failed_tasks = [] 18*4882a593Smuzhiyun self.pidmap = {} 19*4882a593Smuzhiyun self.tasknumber_current = 0 20*4882a593Smuzhiyun self.tasknumber_total = 0 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun def eventHandler(self, event): 23*4882a593Smuzhiyun # PIDs are a bad idea as they can be reused before we process all UI events. 24*4882a593Smuzhiyun # We maintain a 'fuzzy' match for TaskProgress since there is no other way to match 25*4882a593Smuzhiyun def removetid(pid, tid): 26*4882a593Smuzhiyun self.running_pids.remove(tid) 27*4882a593Smuzhiyun del self.running_tasks[tid] 28*4882a593Smuzhiyun if self.pidmap[pid] == tid: 29*4882a593Smuzhiyun del self.pidmap[pid] 30*4882a593Smuzhiyun self.needUpdate = True 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun if isinstance(event, bb.build.TaskStarted): 33*4882a593Smuzhiyun tid = event._fn + ":" + event._task 34*4882a593Smuzhiyun if event._mc != "default": 35*4882a593Smuzhiyun self.running_tasks[tid] = { 'title' : "mc:%s:%s %s" % (event._mc, event._package, event._task), 'starttime' : time.time(), 'pid' : event.pid } 36*4882a593Smuzhiyun else: 37*4882a593Smuzhiyun self.running_tasks[tid] = { 'title' : "%s %s" % (event._package, event._task), 'starttime' : time.time(), 'pid' : event.pid } 38*4882a593Smuzhiyun self.running_pids.append(tid) 39*4882a593Smuzhiyun self.pidmap[event.pid] = tid 40*4882a593Smuzhiyun self.needUpdate = True 41*4882a593Smuzhiyun elif isinstance(event, bb.build.TaskSucceeded): 42*4882a593Smuzhiyun tid = event._fn + ":" + event._task 43*4882a593Smuzhiyun removetid(event.pid, tid) 44*4882a593Smuzhiyun elif isinstance(event, bb.build.TaskFailedSilent): 45*4882a593Smuzhiyun tid = event._fn + ":" + event._task 46*4882a593Smuzhiyun removetid(event.pid, tid) 47*4882a593Smuzhiyun # Don't add to the failed tasks list since this is e.g. a setscene task failure 48*4882a593Smuzhiyun elif isinstance(event, bb.build.TaskFailed): 49*4882a593Smuzhiyun tid = event._fn + ":" + event._task 50*4882a593Smuzhiyun removetid(event.pid, tid) 51*4882a593Smuzhiyun self.failed_tasks.append( { 'title' : "%s %s" % (event._package, event._task)}) 52*4882a593Smuzhiyun elif isinstance(event, bb.runqueue.runQueueTaskStarted) or isinstance(event, bb.runqueue.sceneQueueTaskStarted): 53*4882a593Smuzhiyun self.tasknumber_current = event.stats.completed + event.stats.active + event.stats.failed 54*4882a593Smuzhiyun self.tasknumber_total = event.stats.total 55*4882a593Smuzhiyun self.setscene_current = event.stats.setscene_active + event.stats.setscene_covered + event.stats.setscene_notcovered 56*4882a593Smuzhiyun self.setscene_total = event.stats.setscene_total 57*4882a593Smuzhiyun self.needUpdate = True 58*4882a593Smuzhiyun elif isinstance(event, bb.build.TaskProgress): 59*4882a593Smuzhiyun if event.pid > 0 and event.pid in self.pidmap: 60*4882a593Smuzhiyun self.running_tasks[self.pidmap[event.pid]]['progress'] = event.progress 61*4882a593Smuzhiyun self.running_tasks[self.pidmap[event.pid]]['rate'] = event.rate 62*4882a593Smuzhiyun self.needUpdate = True 63*4882a593Smuzhiyun else: 64*4882a593Smuzhiyun return False 65*4882a593Smuzhiyun return True 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun def getTasks(self): 68*4882a593Smuzhiyun self.needUpdate = False 69*4882a593Smuzhiyun return (self.running_tasks, self.failed_tasks) 70