graphviz.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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(
  53. ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, target_name)
  54. )
  55. else:
  56. # Group multiple nodes together in a subgraph.
  57. print(' subgraph "cluster_%s" {' % filename)
  58. print(' label = "%s"' % filename)
  59. for target in targets:
  60. build_file, target_name, toolset = ParseTarget(target)
  61. print(' "%s" [label="%s"]' % (target, target_name))
  62. print(" }")
  63. # Now that we've placed all the nodes within subgraphs, output all
  64. # the edges between nodes.
  65. for src, dsts in edges.items():
  66. for dst in dsts:
  67. print(' "%s" -> "%s"' % (src, dst))
  68. print("}")
  69. def main():
  70. if len(sys.argv) < 2:
  71. print(__doc__, file=sys.stderr)
  72. print(file=sys.stderr)
  73. print("usage: %s target1 target2..." % (sys.argv[0]), file=sys.stderr)
  74. return 1
  75. edges = LoadEdges("dump.json", sys.argv[1:])
  76. WriteGraph(edges)
  77. return 0
  78. if __name__ == "__main__":
  79. sys.exit(main())