poky/scripts/contrib/graph-tool
Paul Eggleton 8f7c45c183 graph-tool: switch to argparse
argparse makes this a lot easier to extend.

(From OE-Core rev: c751ef8fdc111d1c967029cea7a3ed0f88ce851b)

Signed-off-by: Paul Eggleton <paul.eggleton@linux.microsoft.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2020-06-16 23:39:28 +01:00

76 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
# Simple graph query utility
# useful for getting answers from .dot files produced by bitbake -g
#
# Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
#
# Copyright 2013 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
import sys
import os
import argparse
scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
sys.path.insert(0, scripts_lib_path)
import argparse_oe
def get_path_networkx(dotfile, fromnode, tonode):
try:
import networkx
except ImportError:
print('ERROR: Please install the networkx python module')
sys.exit(1)
graph = networkx.DiGraph(networkx.nx_pydot.read_dot(dotfile))
def node_missing(node):
import difflib
close_matches = difflib.get_close_matches(node, graph.nodes(), cutoff=0.7)
if close_matches:
print('ERROR: no node "%s" in graph. Close matches:\n %s' % (node, '\n '.join(close_matches)))
sys.exit(1)
if not fromnode in graph:
node_missing(fromnode)
if not tonode in graph:
node_missing(tonode)
return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
def find_paths(args):
path = None
for path in get_path_networkx(args.dotfile, args.fromnode, args.tonode):
print(" -> ".join(map(str, path)))
if not path:
print("ERROR: no path from %s to %s in graph" % (args.fromnode, args.tonode))
return 1
def main():
parser = argparse_oe.ArgumentParser(description='Small utility for working with .dot graph files')
subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
subparsers.required = True
parser_find_paths = subparsers.add_parser('find-paths',
help='Find all of the paths between two nodes in a dot graph',
description='Finds all of the paths between two nodes in a dot graph')
parser_find_paths.add_argument('dotfile', help='.dot graph to search in')
parser_find_paths.add_argument('fromnode', help='starting node name')
parser_find_paths.add_argument('tonode', help='ending node name')
parser_find_paths.set_defaults(func=find_paths)
args = parser.parse_args()
ret = args.func(args)
return ret
if __name__ == "__main__":
ret = main()
sys.exit(ret)