annotate python/ppci/tasks.py @ 409:8fe299cd2d55 devel

Close devel branch
author Windel Bouwman
date Sat, 21 Feb 2015 12:20:10 +0100
parents 3bb7dcfe5529
children 9667d78ba79e
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
1 """
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
2 This module defines tasks and a runner for these tasks. Tasks can
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
3 have dependencies and it can be determined if they need to be run.
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
4 """
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
5
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
6 import logging
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
7
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
8 class TaskError(Exception):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
9 def __init__(self, msg):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
10 self.msg = msg
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
11
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
12
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
13 class Task:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
14 """ Task that can run, and depend on other tasks """
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
15 def __init__(self, name):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
16 self.name = name
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
17 self.completed = False
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
18 self.dependencies = set()
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
19 self.duration = 1
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
20
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
21 def run(self):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
22 raise NotImplementedError("Implement this abstract method!")
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
23
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
24 def fire(self):
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
25 """ Wrapper around run that marks the task as done """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
26 assert all(t.completed for t in self.dependencies)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
27 self.run()
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
28 self.completed = True
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
29
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
30 def add_dependency(self, task):
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
31 """ Add another task as a dependency for this task """
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
32 if task is self:
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
33 raise TaskError('Can not add dependency on task itself!')
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
34 if self in task.down_stream_tasks:
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
35 raise TaskError('Can not introduce circular task')
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
36 self.dependencies.add(task)
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
37 return task
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
38
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
39 @property
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
40 def down_stream_tasks(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
41 """ Return a set of all tasks that follow this task """
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
42 # TODO: is this upstream or downstream???
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
43 cdst = list(dep.down_stream_tasks for dep in self.dependencies)
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
44 cdst.append(self.dependencies)
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
45 return set.union(*cdst)
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
46
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
47 def __gt__(self, other):
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
48 return other in self.down_stream_tasks
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
49
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
50 def __repr__(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
51 return 'Task "{}"'.format(self.name)
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
52
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
53
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
54 class EmptyTask(Task):
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
55 """ Basic task that does nothing """
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
56 def run(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
57 pass
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
58
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
59
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
60 class TaskRunner:
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
61 """ Basic task runner that can run some tasks in sequence """
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
62 def __init__(self):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
63 self.logger = logging.getLogger('taskrunner')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
64 self.task_list = []
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
65
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
66 def add_task(self, task):
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
67 self.task_list.append(task)
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
68
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
69 @property
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
70 def total_duration(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
71 return sum(t.duration for t in self.task_list)
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
72
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
73 def run_tasks(self):
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
74 # First sort tasks:
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
75 self.task_list.sort()
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
76
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
77 # Run tasks:
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
78 passed_time = 0.0
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
79 total_time = self.total_duration
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
80 try:
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
81 for t in self.task_list:
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
82 self.report_progress(passed_time / total_time, t.name)
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
83 t.fire()
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
84 passed_time += t.duration
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
85 except TaskError as e:
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 334
diff changeset
86 self.logger.error(str(e.msg))
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
87 return 1
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
88 self.report_progress(1, 'OK')
329
8f6f3ace4e78 Added build tasks
Windel Bouwman
parents:
diff changeset
89 return 0
332
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
90
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
91 def display(self):
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
92 """ Display task how they would be run """
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
93 for task in self.task_list:
87feb8a23b4d Added task list command
Windel Bouwman
parents: 329
diff changeset
94 print(task)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
95
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
96 def report_progress(self, percentage, text):
6f4753202b9a Added more recipes
Windel Bouwman
parents: 332
diff changeset
97 self.logger.info('[{:3.1%}] {}'.format(percentage, text))