annotate python/ppci/tasks.py @ 329:8f6f3ace4e78

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