blob: 6d2e68b82e5bf4857af1cc77333635d2d6105e08 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
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 Bishopc342db32019-05-15 21:57:59 -040010# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011#
12
13import sys
14
15def 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 Williamsc0f7c042017-02-23 20:41:17 -060022 graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 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
37def 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 Williamsc0f7c042017-02-23 20:41:17 -060044
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 Williamsc124f4f2015-09-15 14:41:29 -050049 print("ERROR: no path from %s to %s in graph" % (fromnode, tonode))
50 sys.exit(1)
51
52def main():
53 import optparse
54 parser = optparse.OptionParser(
55 usage = '''%prog [options] <command> <arguments>
56
57Available 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
79if __name__ == "__main__":
80 main()