aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/cmd/brew-graph
blob: 07b3464b40c7a82c530bc430c77787ea365dfa0a (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
"""
$ brew install graphviz
$ brew graph | dot -Tsvg -ohomebrew.html
$ open homebrew.html
"""
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():
            # If the label is an HTML-like string (starts and end with '<' and '>', respectively),
            # don't put quotes around it (or GraphViz won't recognize it.)
            if self.label[0] == '<' and self.label[-1] == '>':
                out.outln('label = %s;' % self.label)
            else:
                out.outln('label = "%s"' % self.label)
            for k in self.attrib:
                if isinstance(self.attrib[k], dict):
                    out.outln('%s %s;' % (k, format_attribs(self.attrib[k])))
                else:
                    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:
                # If the value of the attrib is a dictionary, write it out in special array form
                if isinstance(self.attrib[k], dict):
                    self.o.outln('%s %s;' % (k, format_attribs(self.attrib[k])))
                else:
                    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():
    cmd = ["brew", "deps"]
    if sys.argv[1:]:
        if '--all' in sys.argv[1:]:
            show = 'all'
            cmd.extend(['--all'])
        else:
            show = 'one'
            hideOrphaned = False
            cmd.extend(sys.argv[1:])
    else:
        show = 'installed'
        cmd.extend(['--installed'])

    code, output = run(cmd)
    output = output.strip()
    depgraph = list()

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

    # We need newrank = True to make sure clusters respect rank = "source". Otherwise, we may get
    # random nodes next to "Safe to Remove" cluster, despite them not being a part of that cluster.
    hb = Graph("Homebrew Dependencies", attrib={
        'labelloc':'t', 'rankdir': 'TB' , 'ranksep':'3', 'newrank': True,
        'graph': {'fontname': 'Futura-Medium', 'fontsize': 48},
        'node': {'fontname': 'HelveticaNeue', 'fontsize': 14}
        })
    # Independent formulas (those that are not dependended on by any other formula) get placed in
    # their own subgraph so we can align them together on the left.
    if show == 'installed':
        # We use a HTML-like label to give the label a little bit of padding at the top
        sub = hb.cluster("independent", "<<font point-size=\"15\"><br/></font>Safe to Remove>",
                attrib={'rank': 'source', 'style': 'filled', 'fillcolor': '#F0F0F0', 'color': 'invis',
                    'margin': '25,1', 'graph': {'fontname': 'Helvetica-LightOblique', 'fontsize': 24}})
    else:
        sub = hb

    seen = set()
    def addNode(graph, name):
        if name not in seen:
            graph.node(name, name, attrib={'shape': 'box'})
            seen.add(name)
            return True
        return False

    independent = set()
    for f in depgraph:
        # Filter out orphan formulas when showing all, to cut down on noise
        if show == 'all' and len(f[1]) == 0:
            continue

        independent.add(f[0])
        for d in f[1]:
            independent.discard(d)
            hb.link(f[0], d)
            # Children we can add right away because we don't care where they go
            addNode(hb, d)

    # For all installed formulas, place them in the 'indep' subgraph iff they
    # are not depended on by other formulas, i.e. are root nodes.
    for d in independent:
        addNode(sub, d)

    hb.dot()


if __name__ == "__main__":
    main()