graph-tool 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # SPDX-License-Identifier: GPL-2.0-only
  10. #
  11. import sys
  12. def get_path_networkx(dotfile, fromnode, tonode):
  13. try:
  14. import networkx
  15. except ImportError:
  16. print('ERROR: Please install the networkx python module')
  17. sys.exit(1)
  18. graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile))
  19. def node_missing(node):
  20. import difflib
  21. close_matches = difflib.get_close_matches(node, graph.nodes(), cutoff=0.7)
  22. if close_matches:
  23. print('ERROR: no node "%s" in graph. Close matches:\n %s' % (node, '\n '.join(close_matches)))
  24. sys.exit(1)
  25. if not fromnode in graph:
  26. node_missing(fromnode)
  27. if not tonode in graph:
  28. node_missing(tonode)
  29. return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
  30. def find_paths(args, usage):
  31. if len(args) < 3:
  32. usage()
  33. sys.exit(1)
  34. fromnode = args[1]
  35. tonode = args[2]
  36. path = None
  37. for path in get_path_networkx(args[0], fromnode, tonode):
  38. print(" -> ".join(map(str, path)))
  39. if not path:
  40. print("ERROR: no path from %s to %s in graph" % (fromnode, tonode))
  41. sys.exit(1)
  42. def main():
  43. import optparse
  44. parser = optparse.OptionParser(
  45. usage = '''%prog [options] <command> <arguments>
  46. Available commands:
  47. find-paths <dotfile> <from> <to>
  48. Find all of the paths between two nodes in a dot graph''')
  49. #parser.add_option("-d", "--debug",
  50. # help = "Report all SRCREV values, not just ones where AUTOREV has been used",
  51. # action="store_true", dest="debug", default=False)
  52. options, args = parser.parse_args(sys.argv)
  53. args = args[1:]
  54. if len(args) < 1:
  55. parser.print_help()
  56. sys.exit(1)
  57. if args[0] == "find-paths":
  58. find_paths(args[1:], parser.print_help)
  59. else:
  60. parser.print_help()
  61. sys.exit(1)
  62. if __name__ == "__main__":
  63. main()