dot.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import re
  18. def pre_order_graph(node, nodes, edges, parent):
  19. match = re.search("(.*?)\s\(id=(\d+)\)", node.val.name)
  20. if match:
  21. if parent:
  22. edges.append("node_%s -> %s;" % (match.group(2), parent))
  23. nodes.append("node_%s [label=\"%s\"];" % (match.group(2),
  24. match.group(1)))
  25. for c in node.children:
  26. pre_order_graph(c, nodes, edges, "node_%s" % (match.group(2), ))
  27. def graph_to_dot(fragments):
  28. """Parse the list of fragements to build the graph"""
  29. # get all nodes of the fragement
  30. nodes = []
  31. edges = []
  32. for f in fragments:
  33. parent = None
  34. for c in f.children:
  35. dst = re.search("dst_id=(\d+)", c.val.name)
  36. if dst:
  37. parent = "node_%s" % (dst.group(1))
  38. pre_order_graph(c, nodes, edges, parent)
  39. return """ digraph q { %s %s } """ % (
  40. " ".join(nodes),
  41. " ".join(edges)
  42. )