Skip to content

Commit 2ff80ce

Browse files
donaldhkuba-moo
authored andcommitted
tools/net/ynl: add support for --family and --list-families
Add a --family option to ynl to specify the spec by family name instead of file path, with support for searching in-tree and system install location and a --list-families option to show the available families. ./tools/net/ynl/pyynl/cli.py --family rt_addr --dump getaddr Signed-off-by: Donald Hunter <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 7c125d5 commit 2ff80ce

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

tools/net/ynl/pyynl/cli.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@
33

44
import argparse
55
import json
6+
import os
67
import pathlib
78
import pprint
89
import sys
910

1011
sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
1112
from lib import YnlFamily, Netlink, NlError
1213

14+
sys_schema_dir='/usr/share/ynl'
15+
relative_schema_dir='../../../../Documentation/netlink'
16+
17+
def schema_dir():
18+
script_dir = os.path.dirname(os.path.abspath(__file__))
19+
schema_dir = os.path.abspath(f"{script_dir}/{relative_schema_dir}")
20+
if not os.path.isdir(schema_dir):
21+
schema_dir = sys_schema_dir
22+
if not os.path.isdir(schema_dir):
23+
raise Exception(f"Schema directory {schema_dir} does not exist")
24+
return schema_dir
25+
26+
def spec_dir():
27+
spec_dir = schema_dir() + '/specs'
28+
if not os.path.isdir(spec_dir):
29+
raise Exception(f"Spec directory {spec_dir} does not exist")
30+
return spec_dir
31+
1332

1433
class YnlEncoder(json.JSONEncoder):
1534
def default(self, obj):
@@ -32,7 +51,14 @@ def main():
3251

3352
parser = argparse.ArgumentParser(description=description,
3453
epilog=epilog)
35-
parser.add_argument('--spec', dest='spec', type=str, required=True)
54+
spec_group = parser.add_mutually_exclusive_group(required=True)
55+
spec_group.add_argument('--family', dest='family', type=str,
56+
help='name of the netlink FAMILY')
57+
spec_group.add_argument('--list-families', action='store_true',
58+
help='list all netlink families supported by YNL (has spec)')
59+
spec_group.add_argument('--spec', dest='spec', type=str,
60+
help='choose the family by SPEC file path')
61+
3662
parser.add_argument('--schema', dest='schema', type=str)
3763
parser.add_argument('--no-schema', action='store_true')
3864
parser.add_argument('--json', dest='json_text', type=str)
@@ -70,14 +96,29 @@ def output(msg):
7096
else:
7197
pprint.PrettyPrinter().pprint(msg)
7298

99+
if args.list_families:
100+
for filename in sorted(os.listdir(spec_dir())):
101+
if filename.endswith('.yaml'):
102+
print(filename.removesuffix('.yaml'))
103+
return
104+
73105
if args.no_schema:
74106
args.schema = ''
75107

76108
attrs = {}
77109
if args.json_text:
78110
attrs = json.loads(args.json_text)
79111

80-
ynl = YnlFamily(args.spec, args.schema, args.process_unknown,
112+
if args.family:
113+
spec = f"{spec_dir()}/{args.family}.yaml"
114+
if args.schema is None and spec.startswith(sys_schema_dir):
115+
args.schema = '' # disable schema validation when installed
116+
else:
117+
spec = args.spec
118+
if not os.path.isfile(spec):
119+
raise Exception(f"Spec file {spec} does not exist")
120+
121+
ynl = YnlFamily(spec, args.schema, args.process_unknown,
81122
recv_size=args.dbg_small_recv)
82123
if args.dbg_small_recv:
83124
ynl.set_recv_dbg(True)

0 commit comments

Comments
 (0)