graphviz.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Using the JSON dumped by the dump-dependency-json generator,
  6. generate input suitable for graphviz to render a dependency graph of
  7. targets."""
  8. import collections
  9. import json
  10. import sys
  11. def ParseTarget(target):
  12. target, _, suffix = target.partition('#')
  13. filename, _, target = target.partition(':')
  14. return filename, target, suffix
  15. def LoadEdges(filename, targets):
  16. """Load the edges map from the dump file, and filter it to only
  17. show targets in |targets| and their depedendents."""
  18. file = open('dump.json')
  19. edges = json.load(file)
  20. file.close()
  21. # Copy out only the edges we're interested in from the full edge list.
  22. target_edges = {}
  23. to_visit = targets[:]
  24. while to_visit:
  25. src = to_visit.pop()
  26. if src in target_edges:
  27. continue
  28. target_edges[src] = edges[src]
  29. to_visit.extend(edges[src])
  30. return target_edges
  31. def WriteGraph(edges):
  32. """Print a graphviz graph to stdout.
  33. |edges| is a map of target to a list of other targets it depends on."""
  34. # Bucket targets by file.
  35. files = collections.defaultdict(list)
  36. for src, dst in edges.items():
  37. build_file, target_name, toolset = ParseTarget(src)
  38. files[build_file].append(src)
  39. print 'digraph D {'
  40. print ' fontsize=8' # Used by subgraphs.
  41. print ' node [fontsize=8]'
  42. # Output nodes by file. We must first write out each node within
  43. # its file grouping before writing out any edges that may refer
  44. # to those nodes.
  45. for filename, targets in files.items():
  46. if len(targets) == 1:
  47. # If there's only one node for this file, simplify
  48. # the display by making it a box without an internal node.
  49. target = targets[0]
  50. build_file, target_name, toolset = ParseTarget(target)
  51. print ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename,
  52. target_name)
  53. else:
  54. # Group multiple nodes together in a subgraph.
  55. print ' subgraph "cluster_%s" {' % filename
  56. print ' label = "%s"' % filename
  57. for target in targets:
  58. build_file, target_name, toolset = ParseTarget(target)
  59. print ' "%s" [label="%s"]' % (target, target_name)
  60. print ' }'
  61. # Now that we've placed all the nodes within subgraphs, output all
  62. # the edges between nodes.
  63. for src, dsts in edges.items():
  64. for dst in dsts:
  65. print ' "%s" -> "%s"' % (src, dst)
  66. print '}'
  67. def main():
  68. if len(sys.argv) < 2:
  69. print >>sys.stderr, __doc__
  70. print >>sys.stderr
  71. print >>sys.stderr, 'usage: %s target1 target2...' % (sys.argv[0])
  72. return 1
  73. edges = LoadEdges('dump.json', sys.argv[1:])
  74. WriteGraph(edges)
  75. return 0
  76. if __name__ == '__main__':
  77. sys.exit(main())