aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/examples/brew-graph
blob: dd87ffdf5d64b273e2de9884d43829c442503b2d (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
"""
$ brew install graphviz
$ brew graph | dot -Tsvg -ohomebrew.svg
$ open homebrew.svg
"""
from __future__ import with_statement

from contextlib import contextmanager
import re
from subprocess import Popen, PIPE
import sys


def run(command, print_command=False):
    "Run a command, returning the exit code and output."
    if print_command: print command
    p = Popen(command, stdout=PIPE)
    output, errput = p.communicate()
    return p.returncode, output


def _quote_id(id):
    return '"' + id.replace('"', '\"') + '"'


def format_attribs(attrib):
    if len(attrib) == 0:
        return ''

    values = ['%s="%s"' % (k, attrib[k]) for k in attrib]
    return '[' + ','.join(values) + ']'


class Output(object):
    def __init__(self, fd=sys.stdout, tabstyle="  "):
        self.fd = fd
        self.tabstyle = tabstyle
        self.tablevel = 0

    def close(self):
        self.fd = None

    def out(self, s):
        self.tabout()
        self.fd.write(s)

    def outln(self, s=None):
        if s is not None:
            self.tabout()
            self.fd.write(s)
        self.fd.write('\n')

    @contextmanager
    def indented(self):
        self.indent()
        yield self
        self.dedent()

    def indent(self):
        self.tablevel += 1

    def dedent(self):
        if self.tablevel == 0:
            raise Exception('No existing indent level.')
        self.tablevel -= 1

    def tabout(self):
        if self.tablevel:
            self.fd.write(self.tabstyle * self.tablevel)


class NodeContainer(object):
    def __init__(self):
        self.nodes = list()
        self.node_defaults = dict()
        # Stack of node attribs
        self._node_styles = list()

    def _node_style(self):
        if (len(self._node_styles) > 0):
            return self._node_styles[-1]
        else:
            return dict()

    def _push_node_style(self, attrib):
        self._node_styles.append(attrib)

    def _pop_node_style(self):
        return self._node_styles.pop()

    @contextmanager
    def node_styles(self, attrib):
        self._push_node_style(attrib)
        yield
        self._pop_node_style()

    def node(self, nodeid, label, attrib=None):
        _attrib = dict(self._node_style())
        if attrib is not None:
            _attrib.update(attrib)

        n = Node(nodeid, label, _attrib)
        self.nodes.append(n)
        return n

    def nodes_to_dot(self, out):
        if len(self.node_defaults) > 0:
            out.outln("node " + format_attribs(self.node_defaults) + ";")

        if len(self.nodes) == 0:
            return

        id_width = max([len(_quote_id(n.id)) for n in self.nodes])
        for node in self.nodes:
            node.to_dot(out, id_width)


class Node(object):
    def __init__(self, nodeid, label, attrib=None):
        self.id = nodeid
        self.label = label
        self.attrib = attrib if attrib is not None else dict()

    def as_dot(self, id_width=1):
        _attribs = dict(self.attrib)
        _attribs['label'] = self.label

        return '%-*s %s' % (id_width, _quote_id(self.id), format_attribs(_attribs))


    def to_dot(self, out, id_width=1):
        out.outln(self.as_dot(id_width))


class ClusterContainer(object):
    def __init__(self):
        self.clusters = list()

    def cluster(self, clusterid, label, attrib=None):
        c = Cluster(clusterid, label, self, attrib)
        self.clusters.append(c)
        return c


class Cluster(NodeContainer, ClusterContainer):
    def __init__(self, clusterid, label, parentcluster=None, attrib=None):
        NodeContainer.__init__(self)
        ClusterContainer.__init__(self)

        self.id = clusterid
        self.label = label
        self.attrib = attrib if attrib is not None else dict()
        self.parentcluster = parentcluster

    def cluster_id(self):
        return _quote_id("cluster_" + self.id)

    def to_dot(self, out):
        out.outln("subgraph %s {" % self.cluster_id())
        with out.indented():
            out.outln('label = "%s"' % self.label)
            for k in self.attrib:
                out.outln('%s = "%s"' % (k, self.attrib[k]))

            for cluster in self.clusters:
                cluster.to_dot(out)

            self.nodes_to_dot(out)
        out.outln("}")


class Edge(object):
    def __init__(self, source, target, attrib=None):
        if attrib is None:
            attrib = dict()

        self.source = source
        self.target = target
        self.attrib = attrib

    def to_dot(self, out):
        out.outln(self.as_dot())

    def as_dot(self):
        return " ".join((_quote_id(self.source), "->", _quote_id(self.target), format_attribs(self.attrib)))


class EdgeContainer(object):
    def __init__(self):
        self.edges = list()
        self.edge_defaults = dict()
        # Stack of edge attribs
        self._edge_styles = list()

    def _edge_style(self):
        if (len(self._edge_styles) > 0):
            return self._edge_styles[-1]
        else:
            return dict()

    def _push_edge_style(self, attrib):
        self._edge_styles.append(attrib)

    def _pop_edge_style(self):
        return self._edge_styles.pop()

    @contextmanager
    def edge_styles(self, attrib):
        self._push_edge_style(attrib)
        yield
        self._pop_edge_style()

    def link(self, source, target, attrib=None):
        _attrib = dict(self._edge_style())
        if attrib is not None:
            _attrib.update(attrib)

        e = Edge(source, target, _attrib)
        self.edges.append(e)
        return e

    def edges_to_dot(self, out):
        if len(self.edge_defaults) > 0:
            out.outln("edge " + format_attribs(self.edge_defaults) + ";")

        if len(self.edges) == 0:
            return

        for edge in self.edges:
            edge.to_dot(out)


class Graph(NodeContainer, EdgeContainer, ClusterContainer):
    """
    Contains the nodes, edges, and subgraph definitions for a graph to be
    turned into a Graphviz DOT file.
    """

    def __init__(self, label=None, attrib=None):
        NodeContainer.__init__(self)
        EdgeContainer.__init__(self)
        ClusterContainer.__init__(self)

        self.label = label if label is not None else "Default Label"
        self.attrib = attrib if attrib is not None else dict()

    def dot(self, fd=sys.stdout):
        try:
            self.o = Output(fd)
            self._dot()
        finally:
            self.o.close()

    def _dot(self):
        self.o.outln("digraph G {")

        with self.o.indented():
            self.o.outln('label = "%s"' % self.label)
            for k in self.attrib:
                self.o.outln('%s = "%s"' % (k, self.attrib[k]))

            self.nodes_to_dot(self.o)

            for cluster in self.clusters:
                self.o.outln()
                cluster.to_dot(self.o)

            self.o.outln()
            self.edges_to_dot(self.o)

        self.o.outln("}")


def main():
    code, output = run(["brew", "deps", "--all"])
    output = output.strip()
    depgraph = list()

    for f in output.split("\n"):
        stuff = f.split(":",2)
        name = stuff[0]
        deps = stuff[1].strip()
        if not deps:
            deps = list()
        else:
            deps = deps.split(" ")
        depgraph.append((name, deps))

    hb = Graph("Homebrew Dependencies", attrib={'labelloc':'b', 'rankdir':'LR', 'ranksep':'5'})

    used = set()
    for f in depgraph:
        for d in f[1]:
            used.add(f[0])
            used.add(d)

    for f in depgraph:
        if f[0] not in used:
            continue
        n = hb.node(f[0], f[0])
        for d in f[1]:
            hb.link(d, f[0])

    hb.dot()


if __name__ == "__main__":
    main()