| 1 |
class SimpleTaskEngine(object): |
| 2 |
def __init__(self): |
| 3 |
self._plan_cache = {} |
| 4 |
|
| 5 |
self.tasks = tasks = {} |
| 6 |
|
| 7 |
for name in dir(self): |
| 8 |
if name.startswith('task_'): |
| 9 |
task_name = name[len('task_'):] |
| 10 |
task = getattr(self, name) |
| 11 |
assert callable(task) |
| 12 |
task_deps = getattr(task, 'task_deps', []) |
| 13 |
|
| 14 |
tasks[task_name] = task, task_deps |
| 15 |
|
| 16 |
def _plan(self, goals, skip=[]): |
| 17 |
skip = [toskip for toskip in skip if toskip not in goals] |
| 18 |
|
| 19 |
key = (tuple(goals), tuple(skip)) |
| 20 |
try: |
| 21 |
return self._plan_cache[key] |
| 22 |
except KeyError: |
| 23 |
pass |
| 24 |
constraints = [] |
| 25 |
|
| 26 |
def subgoals(task_name): |
| 27 |
taskcallable, deps = self.tasks[task_name] |
| 28 |
for dep in deps: |
| 29 |
if dep.startswith('??'): |
| 30 |
dep = dep[2:] |
| 31 |
if dep not in goals: |
| 32 |
continue |
| 33 |
if dep.startswith('?'): |
| 34 |
dep = dep[1:] |
| 35 |
if dep in skip: |
| 36 |
continue |
| 37 |
yield dep |
| 38 |
|
| 39 |
seen = {} |
| 40 |
|
| 41 |
def consider(subgoal): |
| 42 |
if subgoal in seen: |
| 43 |
return |
| 44 |
else: |
| 45 |
seen[subgoal] = True |
| 46 |
constraints.append([subgoal]) |
| 47 |
deps = subgoals(subgoal) |
| 48 |
for dep in deps: |
| 49 |
constraints.append([subgoal, dep]) |
| 50 |
consider(dep) |
| 51 |
|
| 52 |
for goal in goals: |
| 53 |
consider(goal) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
plan = [] |
| 58 |
|
| 59 |
while True: |
| 60 |
cands = dict.fromkeys([constr[0] for constr in constraints if constr]) |
| 61 |
if not cands: |
| 62 |
break |
| 63 |
|
| 64 |
for cand in cands: |
| 65 |
for constr in constraints: |
| 66 |
if cand in constr[1:]: |
| 67 |
break |
| 68 |
else: |
| 69 |
break |
| 70 |
else: |
| 71 |
raise RuntimeError("circular dependecy") |
| 72 |
|
| 73 |
plan.append(cand) |
| 74 |
for constr in constraints: |
| 75 |
if constr and constr[0] == cand: |
| 76 |
del constr[0] |
| 77 |
|
| 78 |
plan.reverse() |
| 79 |
|
| 80 |
self._plan_cache[key] = plan |
| 81 |
|
| 82 |
return plan |
| 83 |
|
| 84 |
def _depending_on(self, goal): |
| 85 |
l = [] |
| 86 |
for task_name, (task, task_deps) in self.tasks.iteritems(): |
| 87 |
if goal in task_deps: |
| 88 |
l.append(task_name) |
| 89 |
return l |
| 90 |
|
| 91 |
def _depending_on_closure(self, goal): |
| 92 |
d = {} |
| 93 |
|
| 94 |
def track(goal): |
| 95 |
if goal in d: |
| 96 |
return |
| 97 |
d[goal] = True |
| 98 |
for depending in self._depending_on(goal): |
| 99 |
track(depending) |
| 100 |
track(goal) |
| 101 |
return d.keys() |
| 102 |
|
| 103 |
def _execute(self, goals, *args, **kwds): |
| 104 |
task_skip = kwds.get('task_skip', []) |
| 105 |
res = None |
| 106 |
goals = self._plan(goals, skip=task_skip) |
| 107 |
for goal in goals: |
| 108 |
taskcallable, _ = self.tasks[goal] |
| 109 |
self._event('planned', goal, taskcallable) |
| 110 |
for goal in goals: |
| 111 |
taskcallable, _ = self.tasks[goal] |
| 112 |
self._event('pre', goal, taskcallable) |
| 113 |
try: |
| 114 |
res = self._do(goal, taskcallable, *args, **kwds) |
| 115 |
except (SystemExit, KeyboardInterrupt): |
| 116 |
raise |
| 117 |
except: |
| 118 |
self._error(goal) |
| 119 |
raise |
| 120 |
self._event('post', goal, taskcallable) |
| 121 |
return res |
| 122 |
|
| 123 |
def _do(self, goal, func, *args, **kwds): |
| 124 |
return func() |
| 125 |
|
| 126 |
def _event(self, kind, goal, func): |
| 127 |
pass |
| 128 |
|
| 129 |
def _error(self, goal): |
| 130 |
pass |