Visualise macports dependencies

Sun, 15. Aug 2010

Categories: en sysadmin Tags: Graphviz MacPorts OS X Ruby

to clean up your installed macports and remove cruft you need to uninstall them in the correct order โ€“ according to their dependencies.

A graphical visualisation might help doing so:

port-deps

port-deps

Call

1$ ./port-deps2dot.rb | dot -Tpdf -o port-deps.pdf ; open port-deps.pdf

with the ruby script port-deps2dot.rb (github gist) as follows:

 1#!/usr/bin/ruby -w
 2
 3# visualize macports dependencies.
 4# pipe the result through graphviz, e.g.
 5# $ ./port-deps2dot.rb | dot -Tpdf -o port-deps.pdf ; open port-deps.pdf
 6
 7def scan_deps
 8  pat = /^([^:]+):(.+)$/
 9  name = ''
10  deps = []
11  IO.popen('port info --name --pretty --depends installed') do |f|
12    f.each_line do |l|
13      case l
14        when /^--$/
15          yield name, deps
16          name = ''
17          deps = []
18        when /^([^:]+):(.+)$/
19          if 'name' == "#$1"
20            name = "#$2".strip
21          else
22            deps.concat("#$2".split(/\s*,\s*/))
23          end
24        else
25          raise "Fall-through for '#{l}'"
26      end
27    end
28  end
29end
30
31all = {}
32
33scan_deps do |name,deps|
34  d = all[name]
35  all[name] = d = [] if d.nil?
36  deps.collect! {|i| i.strip}
37  d.concat deps
38  d.sort!
39  d.uniq!
40end
41
42head = <<END_OF_STRING
43#!/usr/bin/dot -Tpdf -o port-deps.pdf
44/*
45  See http://www.graphviz.org/Documentation.php
46*/
47digraph "port deps" {
48  rankdir=LR;
49    label="port deps";
50    node [style=filled,fillcolor=lightblue,shape=ellipse];
51    top_level [shape=point];
52END_OF_STRING
53
54puts head
55
56all.keys.sort.each do |name|
57  deps = all[name]
58  if deps.count > 0
59    deps.each {|d| puts "\t\"#{name}\" -> \"#{d}\";" }
60  else
61    puts "\t\"#{name}\";"
62  end
63end
64
65foot = <<END_OF_STRING
66}
67END_OF_STRING
68
69puts foot