From 4e4480e89c47b52b3f4fbc1ddf07a7ce541f0839 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Mon, 30 Jan 2023 18:33:42 -0800 Subject: tools: ynl: move the cli and netlink code around Move the CLI code out of samples/ and the library part of it into tools/net/ynl/lib/. This way we can start sharing some code with the code gen. Initially I thought that code gen is too C-specific to share anything but basic stuff like calculating values for enums can easily be shared. Signed-off-by: Jakub Kicinski --- tools/net/ynl/cli.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tools/net/ynl/cli.py (limited to 'tools/net/ynl/cli.py') diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py new file mode 100755 index 000000000000..5c4eb5a68514 --- /dev/null +++ b/tools/net/ynl/cli.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: BSD-3-Clause + +import argparse +import json +import pprint +import time + +from lib import YnlFamily + + +def main(): + parser = argparse.ArgumentParser(description='YNL CLI sample') + parser.add_argument('--spec', dest='spec', type=str, required=True) + parser.add_argument('--schema', dest='schema', type=str) + parser.add_argument('--json', dest='json_text', type=str) + parser.add_argument('--do', dest='do', type=str) + parser.add_argument('--dump', dest='dump', type=str) + parser.add_argument('--sleep', dest='sleep', type=int) + parser.add_argument('--subscribe', dest='ntf', type=str) + args = parser.parse_args() + + attrs = {} + if args.json_text: + attrs = json.loads(args.json_text) + + ynl = YnlFamily(args.spec, args.schema) + + if args.ntf: + ynl.ntf_subscribe(args.ntf) + + if args.sleep: + time.sleep(args.sleep) + + if args.do or args.dump: + method = getattr(ynl, args.do if args.do else args.dump) + + reply = method(attrs, dump=bool(args.dump)) + pprint.PrettyPrinter().pprint(reply) + + if args.ntf: + ynl.check_ntf() + pprint.PrettyPrinter().pprint(ynl.async_msg_queue) + + +if __name__ == "__main__": + main() -- cgit From 8dfec0a8886880868802094967c6a769b6d15737 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Mon, 30 Jan 2023 18:33:49 -0800 Subject: tools: ynl: use operation names from spec on the CLI When I wrote the first version of the Python code I was quite excited that we can generate class methods directly from the spec. Unfortunately we need to use valid identifiers for method names (specifically no dashes are allowed). Don't reuse those names on the CLI, it's much more natural to use the operation names exactly as listed in the spec. Instead of: ./cli --do rings_get use: ./cli --do rings-get Signed-off-by: Jakub Kicinski --- tools/net/ynl/cli.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tools/net/ynl/cli.py') diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py index 5c4eb5a68514..05d1f4069ce1 100755 --- a/tools/net/ynl/cli.py +++ b/tools/net/ynl/cli.py @@ -32,10 +32,11 @@ def main(): if args.sleep: time.sleep(args.sleep) - if args.do or args.dump: - method = getattr(ynl, args.do if args.do else args.dump) - - reply = method(attrs, dump=bool(args.dump)) + if args.do: + reply = ynl.do(args.do, attrs) + pprint.PrettyPrinter().pprint(reply) + if args.dump: + reply = ynl.dump(args.dump, attrs) pprint.PrettyPrinter().pprint(reply) if args.ntf: -- cgit From 5c6674f6eb52f7968b805b25c7478b3d96b6b4f7 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Mon, 30 Jan 2023 18:33:50 -0800 Subject: tools: ynl: load jsonschema on demand The CLI script tries to validate jsonschema by default. It's seems better to validate too many times than too few. However, when copying the scripts to random servers having to install jsonschema is tedious. Load jsonschema via importlib, and let the user opt out. Signed-off-by: Jakub Kicinski --- tools/net/ynl/cli.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tools/net/ynl/cli.py') diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py index 05d1f4069ce1..e64f1478764f 100755 --- a/tools/net/ynl/cli.py +++ b/tools/net/ynl/cli.py @@ -13,6 +13,7 @@ def main(): parser = argparse.ArgumentParser(description='YNL CLI sample') parser.add_argument('--spec', dest='spec', type=str, required=True) parser.add_argument('--schema', dest='schema', type=str) + parser.add_argument('--no-schema', action='store_true') parser.add_argument('--json', dest='json_text', type=str) parser.add_argument('--do', dest='do', type=str) parser.add_argument('--dump', dest='dump', type=str) @@ -20,6 +21,9 @@ def main(): parser.add_argument('--subscribe', dest='ntf', type=str) args = parser.parse_args() + if args.no_schema: + args.schema = '' + attrs = {} if args.json_text: attrs = json.loads(args.json_text) -- cgit From 981cbcb030d919ee49ebbfc2889839c6882d9ea7 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Mon, 30 Jan 2023 18:33:54 -0800 Subject: tools: net: use python3 explicitly The scripts require Python 3 and some distros are dropping Python 2 support. Reported-by: Stanislav Fomichev Signed-off-by: Jakub Kicinski --- tools/net/ynl/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/net/ynl/cli.py') diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py index e64f1478764f..db410b74d539 100755 --- a/tools/net/ynl/cli.py +++ b/tools/net/ynl/cli.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # SPDX-License-Identifier: BSD-3-Clause import argparse -- cgit