Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
| 3 | # Simple graph query utility |
| 4 | # useful for getting answers from .dot files produced by bitbake -g |
| 5 | # |
| 6 | # Written by: Paul Eggleton <paul.eggleton@linux.intel.com> |
| 7 | # |
| 8 | # Copyright 2013 Intel Corporation |
| 9 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 10 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | # |
| 12 | |
| 13 | import sys |
| 14 | |
| 15 | def get_path_networkx(dotfile, fromnode, tonode): |
| 16 | try: |
| 17 | import networkx |
| 18 | except ImportError: |
| 19 | print('ERROR: Please install the networkx python module') |
| 20 | sys.exit(1) |
| 21 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 22 | graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | def node_missing(node): |
| 24 | import difflib |
| 25 | close_matches = difflib.get_close_matches(node, graph.nodes(), cutoff=0.7) |
| 26 | if close_matches: |
| 27 | print('ERROR: no node "%s" in graph. Close matches:\n %s' % (node, '\n '.join(close_matches))) |
| 28 | sys.exit(1) |
| 29 | |
| 30 | if not fromnode in graph: |
| 31 | node_missing(fromnode) |
| 32 | if not tonode in graph: |
| 33 | node_missing(tonode) |
| 34 | return networkx.all_simple_paths(graph, source=fromnode, target=tonode) |
| 35 | |
| 36 | |
| 37 | def find_paths(args, usage): |
| 38 | if len(args) < 3: |
| 39 | usage() |
| 40 | sys.exit(1) |
| 41 | |
| 42 | fromnode = args[1] |
| 43 | tonode = args[2] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | |
| 45 | path = None |
| 46 | for path in get_path_networkx(args[0], fromnode, tonode): |
| 47 | print(" -> ".join(map(str, path))) |
| 48 | if not path: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | print("ERROR: no path from %s to %s in graph" % (fromnode, tonode)) |
| 50 | sys.exit(1) |
| 51 | |
| 52 | def main(): |
| 53 | import optparse |
| 54 | parser = optparse.OptionParser( |
| 55 | usage = '''%prog [options] <command> <arguments> |
| 56 | |
| 57 | Available commands: |
| 58 | find-paths <dotfile> <from> <to> |
| 59 | Find all of the paths between two nodes in a dot graph''') |
| 60 | |
| 61 | #parser.add_option("-d", "--debug", |
| 62 | # help = "Report all SRCREV values, not just ones where AUTOREV has been used", |
| 63 | # action="store_true", dest="debug", default=False) |
| 64 | |
| 65 | options, args = parser.parse_args(sys.argv) |
| 66 | args = args[1:] |
| 67 | |
| 68 | if len(args) < 1: |
| 69 | parser.print_help() |
| 70 | sys.exit(1) |
| 71 | |
| 72 | if args[0] == "find-paths": |
| 73 | find_paths(args[1:], parser.print_help) |
| 74 | else: |
| 75 | parser.print_help() |
| 76 | sys.exit(1) |
| 77 | |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | main() |