329
|
1
|
|
2 class TaskError(Exception):
|
|
3 pass
|
|
4
|
|
5
|
|
6 class Task:
|
|
7 """ Task that can run, and depend on other tasks """
|
|
8 def __init__(self, name):
|
|
9 self.name = name
|
|
10 self.subtasks = []
|
|
11 self.completed = False
|
|
12 self.dependencies = []
|
|
13
|
|
14 def run(self):
|
|
15 raise NotImplementedError("Implement this abstract method!")
|
|
16
|
|
17 def fire(self):
|
|
18 assert all(t.completed for t in self.dependencies)
|
|
19 self.run()
|
|
20 self.completed = True
|
|
21
|
|
22 def addSubTask(self, tsk):
|
|
23 self.subtasks.append(tsk)
|
|
24 return tsk
|
|
25
|
|
26 def addDependency(self, task):
|
|
27 self.dependencies.append(task)
|
|
28 return task
|
|
29
|
|
30
|
|
31 class TaskRunner:
|
|
32 def __init__(self):
|
|
33 self.task_list = []
|
|
34
|
|
35 def add_task(self, task):
|
|
36 self.task_list.append(task)
|
|
37
|
|
38 def run_tasks(self):
|
|
39 try:
|
|
40 for t in self.task_list:
|
|
41 #print('Running {}'.format(t.name))
|
|
42 t.fire()
|
|
43 except TaskError as e:
|
|
44 print('Error: {}'.format(e))
|
|
45 return 1
|
|
46 return 0
|