annotate python/ppci/tasks.py @ 342:86b02c98a717 devel

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