Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/net/ynl/pyynl/ynl_gen_rst.py
29271 views
1
#!/usr/bin/env python3
2
# SPDX-License-Identifier: GPL-2.0
3
# -*- coding: utf-8; mode: python -*-
4
5
"""
6
Script to auto generate the documentation for Netlink specifications.
7
8
:copyright: Copyright (C) 2023 Breno Leitao <[email protected]>
9
:license: GPL Version 2, June 1991 see linux/COPYING for details.
10
11
This script performs extensive parsing to the Linux kernel's netlink YAML
12
spec files, in an effort to avoid needing to heavily mark up the original
13
YAML file. It uses the library code from scripts/lib.
14
"""
15
16
import os.path
17
import pathlib
18
import sys
19
import argparse
20
import logging
21
22
sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
23
from lib import YnlDocGenerator # pylint: disable=C0413
24
25
def parse_arguments() -> argparse.Namespace:
26
"""Parse arguments from user"""
27
parser = argparse.ArgumentParser(description="Netlink RST generator")
28
29
parser.add_argument("-v", "--verbose", action="store_true")
30
parser.add_argument("-o", "--output", help="Output file name")
31
32
# Index and input are mutually exclusive
33
group = parser.add_mutually_exclusive_group()
34
group.add_argument("-i", "--input", help="YAML file name")
35
36
args = parser.parse_args()
37
38
if args.verbose:
39
logging.basicConfig(level=logging.DEBUG)
40
41
if args.input and not os.path.isfile(args.input):
42
logging.warning("%s is not a valid file.", args.input)
43
sys.exit(-1)
44
45
if not args.output:
46
logging.error("No output file specified.")
47
sys.exit(-1)
48
49
if os.path.isfile(args.output):
50
logging.debug("%s already exists. Overwriting it.", args.output)
51
52
return args
53
54
55
def write_to_rstfile(content: str, filename: str) -> None:
56
"""Write the generated content into an RST file"""
57
logging.debug("Saving RST file to %s", filename)
58
59
with open(filename, "w", encoding="utf-8") as rst_file:
60
rst_file.write(content)
61
62
63
def main() -> None:
64
"""Main function that reads the YAML files and generates the RST files"""
65
66
args = parse_arguments()
67
68
parser = YnlDocGenerator()
69
70
if args.input:
71
logging.debug("Parsing %s", args.input)
72
try:
73
content = parser.parse_yaml_file(os.path.join(args.input))
74
except Exception as exception:
75
logging.warning("Failed to parse %s.", args.input)
76
logging.warning(exception)
77
sys.exit(-1)
78
79
write_to_rstfile(content, args.output)
80
81
82
if __name__ == "__main__":
83
main()
84
85