annotate python/ppci/tasks.py @ 332:87feb8a23b4d

Added task list command
author Windel Bouwman
date Fri, 07 Feb 2014 12:51:55 +0100
parents 8f6f3ace4e78
children 6f4753202b9a
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
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
30 def __repr__(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
31 return 'Task "{}"'.format(self.name)
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
32
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
33
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
34 class TaskRunner:
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
35 """ Basic task runner that can run some tasks in sequence """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
36 def __init__(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
37 self.task_list = []
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
38
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
39 def add_task(self, task):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
40 self.task_list.append(task)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
41
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
42 def run_tasks(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
43 try:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
44 for t in self.task_list:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
45 #print('Running {}'.format(t.name))
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
46 t.fire()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
47 except TaskError as e:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
48 print('Error: {}'.format(e))
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
49 return 1
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
50 return 0
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
51
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
52 def display(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
53 """ Display task how they would be run """
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
54 for task in self.task_list:
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
55 print(task)