graphviz.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  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(
  52. f' "{target}" [shape=box, label="{filename}\\n{target_name}"]'
  53. )
  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(f' "{target}" [label="{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(f' "{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())