mirror of
https://git.yoctoproject.org/poky
synced 2026-09-19 09:49:33 +02:00
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>
This commit is contained in:
committed by
Richard Purdie
parent
5d1e4b1c55
commit
8f7c45c183
@@ -11,6 +11,13 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
import sys
|
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):
|
def get_path_networkx(dotfile, fromnode, tonode):
|
||||||
try:
|
try:
|
||||||
@@ -34,47 +41,35 @@ def get_path_networkx(dotfile, fromnode, tonode):
|
|||||||
return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
|
return networkx.all_simple_paths(graph, source=fromnode, target=tonode)
|
||||||
|
|
||||||
|
|
||||||
def find_paths(args, usage):
|
def find_paths(args):
|
||||||
if len(args) < 3:
|
|
||||||
usage()
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
fromnode = args[1]
|
|
||||||
tonode = args[2]
|
|
||||||
|
|
||||||
path = None
|
path = None
|
||||||
for path in get_path_networkx(args[0], fromnode, tonode):
|
for path in get_path_networkx(args.dotfile, args.fromnode, args.tonode):
|
||||||
print(" -> ".join(map(str, path)))
|
print(" -> ".join(map(str, path)))
|
||||||
if not path:
|
if not path:
|
||||||
print("ERROR: no path from %s to %s in graph" % (fromnode, tonode))
|
print("ERROR: no path from %s to %s in graph" % (args.fromnode, args.tonode))
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import optparse
|
parser = argparse_oe.ArgumentParser(description='Small utility for working with .dot graph files')
|
||||||
parser = optparse.OptionParser(
|
|
||||||
usage = '''%prog [options] <command> <arguments>
|
|
||||||
|
|
||||||
Available commands:
|
subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
|
||||||
find-paths <dotfile> <from> <to>
|
subparsers.required = True
|
||||||
Find all of the paths between two nodes in a dot graph''')
|
|
||||||
|
|
||||||
#parser.add_option("-d", "--debug",
|
parser_find_paths = subparsers.add_parser('find-paths',
|
||||||
# help = "Report all SRCREV values, not just ones where AUTOREV has been used",
|
help='Find all of the paths between two nodes in a dot graph',
|
||||||
# action="store_true", dest="debug", default=False)
|
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)
|
||||||
|
|
||||||
options, args = parser.parse_args(sys.argv)
|
args = parser.parse_args()
|
||||||
args = args[1:]
|
|
||||||
|
|
||||||
if len(args) < 1:
|
ret = args.func(args)
|
||||||
parser.print_help()
|
return ret
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if args[0] == "find-paths":
|
|
||||||
find_paths(args[1:], parser.print_help)
|
|
||||||
else:
|
|
||||||
parser.print_help()
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
ret = main()
|
||||||
|
sys.exit(ret)
|
||||||
|
|||||||
Reference in New Issue
Block a user