diff options
author | Paul Eggleton <paul.eggleton@linux.microsoft.com> | 2020-06-14 21:48:42 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-06-16 23:39:28 +0100 |
commit | 8f7c45c183e3d9ec15feb507cd923dea85b34f47 (patch) | |
tree | b5f8eef7ade749d665390880ac944a980d159df0 | |
parent | 5d1e4b1c55ad05620a1c18782beb7cceccd38a0c (diff) | |
download | poky-8f7c45c183e3d9ec15feb507cd923dea85b34f47.tar.gz |
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>
-rwxr-xr-x | scripts/contrib/graph-tool | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/scripts/contrib/graph-tool b/scripts/contrib/graph-tool index 6d2e68b82e..9402e617e9 100755 --- a/scripts/contrib/graph-tool +++ b/scripts/contrib/graph-tool | |||
@@ -11,6 +11,13 @@ | |||
11 | # | 11 | # |
12 | 12 | ||
13 | import sys | 13 | import sys |
14 | import os | ||
15 | import argparse | ||
16 | |||
17 | scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib')) | ||
18 | sys.path.insert(0, scripts_lib_path) | ||
19 | import argparse_oe | ||
20 | |||
14 | 21 | ||
15 | def get_path_networkx(dotfile, fromnode, tonode): | 22 | def get_path_networkx(dotfile, fromnode, tonode): |
16 | try: | 23 | try: |
@@ -34,47 +41,35 @@ def get_path_networkx(dotfile, fromnode, tonode): | |||
34 | return networkx.all_simple_paths(graph, source=fromnode, target=tonode) | 41 | return networkx.all_simple_paths(graph, source=fromnode, target=tonode) |
35 | 42 | ||
36 | 43 | ||
37 | def find_paths(args, usage): | 44 | def find_paths(args): |
38 | if len(args) < 3: | ||
39 | usage() | ||
40 | sys.exit(1) | ||
41 | |||
42 | fromnode = args[1] | ||
43 | tonode = args[2] | ||
44 | |||
45 | path = None | 45 | path = None |
46 | for path in get_path_networkx(args[0], fromnode, tonode): | 46 | for path in get_path_networkx(args.dotfile, args.fromnode, args.tonode): |
47 | print(" -> ".join(map(str, path))) | 47 | print(" -> ".join(map(str, path))) |
48 | if not path: | 48 | if not path: |
49 | print("ERROR: no path from %s to %s in graph" % (fromnode, tonode)) | 49 | print("ERROR: no path from %s to %s in graph" % (args.fromnode, args.tonode)) |
50 | sys.exit(1) | 50 | return 1 |
51 | |||
51 | 52 | ||
52 | def main(): | 53 | def main(): |
53 | import optparse | 54 | parser = argparse_oe.ArgumentParser(description='Small utility for working with .dot graph files') |
54 | parser = optparse.OptionParser( | ||
55 | usage = '''%prog [options] <command> <arguments> | ||
56 | 55 | ||
57 | Available commands: | 56 | subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') |
58 | find-paths <dotfile> <from> <to> | 57 | subparsers.required = True |
59 | Find all of the paths between two nodes in a dot graph''') | ||
60 | 58 | ||
61 | #parser.add_option("-d", "--debug", | 59 | parser_find_paths = subparsers.add_parser('find-paths', |
62 | # help = "Report all SRCREV values, not just ones where AUTOREV has been used", | 60 | help='Find all of the paths between two nodes in a dot graph', |
63 | # action="store_true", dest="debug", default=False) | 61 | description='Finds all of the paths between two nodes in a dot graph') |
62 | parser_find_paths.add_argument('dotfile', help='.dot graph to search in') | ||
63 | parser_find_paths.add_argument('fromnode', help='starting node name') | ||
64 | parser_find_paths.add_argument('tonode', help='ending node name') | ||
65 | parser_find_paths.set_defaults(func=find_paths) | ||
64 | 66 | ||
65 | options, args = parser.parse_args(sys.argv) | 67 | args = parser.parse_args() |
66 | args = args[1:] | ||
67 | 68 | ||
68 | if len(args) < 1: | 69 | ret = args.func(args) |
69 | parser.print_help() | 70 | return ret |
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 | 71 | ||
78 | 72 | ||
79 | if __name__ == "__main__": | 73 | if __name__ == "__main__": |
80 | main() | 74 | ret = main() |
75 | sys.exit(ret) | ||