File: taskengine.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (130 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class SimpleTaskEngine(object):
    def __init__(self):
        self._plan_cache = {}

        self.tasks = tasks = {}

        for name in dir(self):
            if name.startswith('task_'):
                task_name = name[len('task_'):]
                task = getattr(self, name)
                assert callable(task)
                task_deps = getattr(task, 'task_deps', [])

                tasks[task_name] = task, task_deps

    def _plan(self, goals, skip=[]):
        skip = [toskip for toskip in skip if toskip not in goals]

        key = (tuple(goals), tuple(skip))
        try:
            return self._plan_cache[key]
        except KeyError:
            pass
        constraints = []

        def subgoals(task_name):
            taskcallable, deps = self.tasks[task_name]
            for dep in deps:
                if dep.startswith('??'): # optional
                    dep = dep[2:]
                    if dep not in goals:
                        continue
                if dep.startswith('?'): # suggested
                    dep = dep[1:]
                    if dep in skip:
                        continue
                yield dep

        seen = {}

        def consider(subgoal):
            if subgoal in seen:
                return
            else:
                seen[subgoal] = True
            constraints.append([subgoal])
            deps = subgoals(subgoal)
            for dep in deps:
                constraints.append([subgoal, dep])
                consider(dep)

        for goal in goals:
            consider(goal)

        #sort

        plan = []

        while True:
            cands = dict.fromkeys([constr[0] for constr in constraints if constr])
            if not cands:
                break

            for cand in cands:
                for constr in constraints:
                    if cand in constr[1:]:
                        break
                else:
                    break
            else:
                raise RuntimeError("circular dependecy")

            plan.append(cand)
            for constr in constraints:
                if constr and constr[0] == cand:
                    del constr[0]

        plan.reverse()

        self._plan_cache[key] = plan

        return plan

    def _depending_on(self, goal):
        l = []
        for task_name, (task, task_deps) in self.tasks.iteritems():
            if goal in task_deps:
                l.append(task_name)
        return l

    def _depending_on_closure(self, goal):
        d = {}

        def track(goal):
            if goal in d:
                return
            d[goal] = True
            for depending in self._depending_on(goal):
                track(depending)
        track(goal)
        return d.keys()

    def _execute(self, goals, *args, **kwds):
        task_skip = kwds.get('task_skip', [])
        res = None
        goals = self._plan(goals, skip=task_skip)
        for goal in goals:
            taskcallable, _ = self.tasks[goal]
            self._event('planned', goal, taskcallable)
        for goal in goals:
            taskcallable, _ = self.tasks[goal]
            self._event('pre', goal, taskcallable)
            try:
                res = self._do(goal, taskcallable, *args, **kwds)
            except (SystemExit, KeyboardInterrupt):
                raise
            except:
                self._error(goal)
                raise
            self._event('post', goal, taskcallable)
        return res

    def _do(self, goal, func, *args, **kwds):
        return func()

    def _event(self, kind, goal, func):
        pass

    def _error(self, goal):
        pass