graph-tool 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. # Simple graph query utility
  3. # useful for getting answers from .dot files produced by bitbake -g
  4. #
  5. # Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
  6. #
  7. # Copyright 2013 Intel Corporation
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import sys
  23. def get_path_networkx(dotfile, fromnode, tonode):
  24. try:
  25. import networkx
  26. except ImportError:
  27. print('ERROR: Please install the networkx python module')
  28. sys.exit(1)
  29. graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile))
  30. def node_missing(node):
  31. import difflib
  32. close_matches = difflib.get_close_matches(node, graph.nodes(), cutoff=0.7)
  33. if close_matches:
  34. print('ERROR: no node "%s" in graph. Close matches:\n %s' % (node, '\n '.join(close_matches)))
  35. sys.exit(1)
  36. if not fromnode in graph:
  37. node_missing(fromnode)
  38. if not tonode in graph:
  39. node_missing(tonode)
  40. return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
  41. def find_paths(args, usage):
  42. if len(args) < 3:
  43. usage()
  44. sys.exit(1)
  45. fromnode = args[1]
  46. tonode = args[2]
  47. path = None
  48. for path in get_path_networkx(args[0], fromnode, tonode):
  49. print(" -> ".join(map(str, path)))
  50. if not path:
  51. print("ERROR: no path from %s to %s in graph" % (fromnode, tonode))
  52. sys.exit(1)
  53. def main():
  54. import optparse
  55. parser = optparse.OptionParser(
  56. usage = '''%prog [options] <command> <arguments>
  57. Available commands:
  58. find-paths <dotfile> <from> <to>
  59. Find all of the paths between two nodes in a dot graph''')
  60. #parser.add_option("-d", "--debug",
  61. # help = "Report all SRCREV values, not just ones where AUTOREV has been used",
  62. # action="store_true", dest="debug", default=False)
  63. options, args = parser.parse_args(sys.argv)
  64. args = args[1:]
  65. if len(args) < 1:
  66. parser.print_help()
  67. sys.exit(1)
  68. if args[0] == "find-paths":
  69. find_paths(args[1:], parser.print_help)
  70. else:
  71. parser.print_help()
  72. sys.exit(1)
  73. if __name__ == "__main__":
  74. main()