1#!/usr/bin/env python
2# vim: ts=2 sw=2 nocindent
3
4import re
5import sys
6
7def choose_regex(regs, line):
8  for func,reg in regs:
9    m = reg.match(line)
10    if m:
11      return (func,m)
12  return (None,None)
13
14def gather(included, deps):
15  result = set()
16  for inc in included:
17    result.add(inc)
18    for d in deps:
19      if inc == d[1]:
20        result.add(d[0])
21  return result
22
23def main():
24  deps = []
25  infos = []
26  def dependency(m):
27    deps.append((m.group(1), m.group(2)))
28  def info(m):
29    infos.append((m.group(1), m.group(2)))
30
31  REGS = [
32      (dependency, re.compile(r'"(.*)"\s*->\s*"(.*)"')),
33      (info, re.compile(r'"(.*)"(\s*\[.*\])')),
34    ]
35
36  lines = sys.stdin.readlines()
37  lines = [line.strip() for line in lines]
38
39  for line in lines:
40    func,m = choose_regex(REGS, line)
41    if func:
42      func(m)
43
44  # filter
45  sys.stderr.write("argv: " + str(sys.argv) + "\n")
46  if not (len(sys.argv) == 2 and sys.argv[1] == "--all"):
47    targets = sys.argv[1:]
48
49    included = set(targets)
50    prevLen = -1
51    while prevLen != len(included):
52      prevLen = len(included)
53      included = gather(included, deps)
54
55    deps = [dep for dep in deps if dep[1] in included]
56    infos = [info for info in infos if info[0] in included]
57
58  print "digraph {"
59  print "graph [ ratio=.5 ];"
60  for dep in deps:
61    print '"%s" -> "%s"' % dep
62  for info in infos:
63    print '"%s"%s' % info
64  print "}"
65
66
67if __name__ == "__main__":
68  main()
69