summaryrefslogtreecommitdiff
path: root/projgraph
blob: c3f5757bc74e58b707ec6d4aed281d743dd6028c (plain)
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
#!/usr/bin/python3

import sys

import yaml


unknown = 0
blocked = 1
finished = 2
ready = 3
next = 4

statuses = {
    'blocked': blocked,
    'finished': finished,
    'ready': ready,
    'next': next,
}

def nodeattrs(done):
    attrs = {
        unknown: {'shape': 'diamond'},
        blocked:  {'color': '#777777', 'shape': 'rectangle'},
        finished:  {'color': '#eeeeee'},
        ready:  {'color': '#bbbbbb'},
        next: {'color': '#00cc00'},
    }

    a = dict(attrs[done])
    if 'style' not in a:
        a['style'] = 'filled'
    return ' '.join('{}="{}"'.format(key, a[key]) for key in a)


def find_unknown(tasklist):
    return [t for t in tasklist if t['status'] == unknown]


def all_deps(tasks, task, status):
    for dep_name in task.get('depends', []):
        dep = tasks[dep_name]
        if dep['status'] != status:
            return False
    return True


def any_dep(tasks, task, status):
    for dep_name in task.get('depends', []):
        dep = tasks[dep_name]
        if dep['status'] == status:
            return True
    return False


def set_status(tasks):
    tasklist = list(tasks.values())
    for task in tasklist:
        if 'status' not in task:
            task['status'] = unknown
        else:
            task['status'] = statuses[task['status']]
    unknown_tasks = find_unknown(tasklist)
    while unknown_tasks:
        for t in unknown_tasks:
            if not t.get('depends', []):
                t['status'] = ready
            else:
                if all_deps(tasks, t, finished):
                    t['status'] = ready
                else:
                    t['status'] = blocked
        unknown_tasks = find_unknown(tasklist)


obj = yaml.safe_load(sys.stdin)
ikiwiki = sys.argv[1:] == ['ikiwiki']

if ikiwiki:
    print('[[!graph src="""')
else:
    print('digraph "project" {')

tasks = obj['tasks']
set_status(tasks)
for name, task in tasks.items():
    print('{} [label="{}"]'.format(name, task['label']))
    status = task['status']
    print('{} [{}]'.format(name, nodeattrs(status)))
    for dep in task.get('depends', []):
        print('{} -> {}'.format(name, dep))

if ikiwiki:
    print('"""]]')
else:
    print('}')