Created
December 6, 2015 10:29
-
-
Save FilippoBiga/4eaec02b1408fff34de5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Generate a dot graph of all the packages installed | |
through Homebrew. Requires pydot. | |
Usage: python brew-graph.py <outfile.dot> | |
''' | |
import pydot | |
from subprocess import Popen, PIPE | |
def _brew(args): | |
assert type(args) is list | |
cmd = "brew %s" % ' '.join(args) | |
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) | |
return p.stdout | |
def brew_lines(args): | |
if type(args) is str: | |
args = [args] | |
return map(lambda l : l.rstrip('\n'), _brew(args).readlines()) | |
def build_graph(verbose=False): | |
def _build(node): | |
# avoid nodes which have already been processed | |
# or that represent packages which are not installed | |
val = inst.get(node, -1) | |
if val is 0: | |
inst[node] = 1 | |
for line in brew_lines(['deps', node]): | |
# do not add edges to packages which are not installed | |
if line not in inst: | |
continue | |
edge = pydot.Edge(node, line) | |
graph.add_edge(edge) | |
_build(line) | |
graph = pydot.Dot(graph_type='digraph') | |
packages = brew_lines('list') | |
inst = dict.fromkeys(packages, 0) | |
for p in inst: | |
if verbose: print p | |
_build(p) | |
return graph | |
if __name__ == '__main__': | |
import os, sys | |
assert len(sys.argv) is 2 | |
outfile = sys.argv[1] | |
assert not os.path.exists(outfile) | |
dotGraph = build_graph(verbose=True) | |
dotGraph.write(outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment