annotate python/ppci/tasks.py @ 336:d1ecc493384e

Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
author Windel Bouwman
date Wed, 19 Feb 2014 22:32:15 +0100
parents 6f4753202b9a
children 86b02c98a717
rev   line source
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
1
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
2 import logging
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
3
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
4 class TaskError(Exception):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
5 pass
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
6
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
7
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
8 class Task:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
9 """ Task that can run, and depend on other tasks """
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
10 def __init__(self, name):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
11 self.name = name
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
12 self.subtasks = []
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
13 self.completed = False
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
14 self.dependencies = []
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
15 self.duration = 1
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
16
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
17 def run(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
18 raise NotImplementedError("Implement this abstract method!")
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
19
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
20 def fire(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
21 assert all(t.completed for t in self.dependencies)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
22 self.run()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
23 self.completed = True
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
24
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
25 def addSubTask(self, tsk):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
26 self.subtasks.append(tsk)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
27 return tsk
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
28
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
29 def addDependency(self, task):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
30 self.dependencies.append(task)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
31 return task
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
32
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
33 def __repr__(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
34 return 'Task "{}"'.format(self.name)
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
35
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
36
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
37 class TaskRunner:
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
38 """ Basic task runner that can run some tasks in sequence """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
39 def __init__(self):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
40 self.logger = logging.getLogger('taskrunner')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
41 self.task_list = []
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
42
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
43 def add_task(self, task):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
44 self.task_list.append(task)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
45
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
46 @property
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
47 def total_duration(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
48 return sum(t.duration for t in self.task_list)
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
49
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
50 def run_tasks(self):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
51 passed_time = 0.0
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
52 total_time = self.total_duration
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
53 try:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
54 for t in self.task_list:
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
55 self.report_progress(passed_time / total_time, t.name)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
56 t.fire()
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
57 passed_time += t.duration
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
58 except TaskError as e:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
59 print('Error: {}'.format(e))
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
60 return 1
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
61 self.report_progress(1, 'OK')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
62 return 0
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
63
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
64 def display(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
65 """ Display task how they would be run """
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
66 for task in self.task_list:
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
67 print(task)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
68
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
69 def report_progress(self, percentage, text):
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
70 self.logger.info('[{:3.1%}] {}'.format(percentage, text))