Mercurial > lcfOS
view python/ppci/tasks.py @ 341:4d204f6f7d4e devel
Rewrite of assembler parts
author | Windel Bouwman |
---|---|
date | Fri, 28 Feb 2014 18:07:14 +0100 |
parents | 6f4753202b9a |
children | 86b02c98a717 |
line wrap: on
line source
import logging class TaskError(Exception): pass class Task: """ Task that can run, and depend on other tasks """ def __init__(self, name): self.name = name self.subtasks = [] self.completed = False self.dependencies = [] self.duration = 1 def run(self): raise NotImplementedError("Implement this abstract method!") def fire(self): assert all(t.completed for t in self.dependencies) self.run() self.completed = True def addSubTask(self, tsk): self.subtasks.append(tsk) return tsk def addDependency(self, task): self.dependencies.append(task) return task def __repr__(self): return 'Task "{}"'.format(self.name) class TaskRunner: """ Basic task runner that can run some tasks in sequence """ def __init__(self): self.logger = logging.getLogger('taskrunner') self.task_list = [] def add_task(self, task): self.task_list.append(task) @property def total_duration(self): return sum(t.duration for t in self.task_list) def run_tasks(self): passed_time = 0.0 total_time = self.total_duration try: for t in self.task_list: self.report_progress(passed_time / total_time, t.name) t.fire() passed_time += t.duration except TaskError as e: print('Error: {}'.format(e)) return 1 self.report_progress(1, 'OK') return 0 def display(self): """ Display task how they would be run """ for task in self.task_list: print(task) def report_progress(self, percentage, text): self.logger.info('[{:3.1%}] {}'.format(percentage, text))