graphviz.py 2.9 KB

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