]> git.proxmox.com Git - mirror_frr.git/blame_incremental - yang/embedmodel.py
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / yang / embedmodel.py
... / ...
CommitLineData
1#!/usr/bin/python3
2# SPDX-License-Identifier: NONE
3#
4# YANG module to C wrapper
5# written 2018 by David Lamparter, placed in Public Domain.
6
7import sys
8import os
9import string
10import re
11
12inname = sys.argv[1]
13outname = sys.argv[2]
14
15outdir = os.path.dirname(os.path.abspath(outname))
16try:
17 os.makedirs(outdir)
18except FileExistsError:
19 pass
20
21# these are regexes to avoid a compile-time/host dependency on yang-tools
22# or python-yang. Cross-compiling FRR is already somewhat involved, no need
23# to make it even harder.
24
25re_name = re.compile(r"\bmodule\s+([^\s]+)\s+\{")
26re_subname = re.compile(r"\bsubmodule\s+([^\s]+)\s+\{")
27re_mainname = re.compile(r"\bbelongs-to\s+([^\s]+)\s+\{")
28re_rev = re.compile(r"\brevision\s+([\d-]+)\s+\{")
29
30
31template = """/* autogenerated by embedmodel.py. DO NOT EDIT */
32
33#include <zebra.h>
34#include "yang.h"
35
36static const char model[] =
37\t"%s";
38
39static struct yang_module_embed embed = {
40\t.mod_name = "%s",
41\t.mod_rev = "%s",
42\t.sub_mod_name = "%s",
43\t.sub_mod_rev = "%s",
44\t.data = model,
45\t.format = %s,
46};
47
48static void embed_register(void) __attribute__((_CONSTRUCTOR(2000)));
49static void embed_register(void)
50{
51\tyang_module_embed(&embed);
52}
53"""
54
55passchars = set(string.printable) - set("\\'\"%\r\n\t\x0b\x0c")
56
57
58def escapech(char):
59 if char in passchars:
60 return char
61 if char == "\n":
62 return "\\n"
63 if char == "\t":
64 return "\\t"
65 if char in "\"\\'":
66 return "\\" + char
67 return "\\x%02x" % (ord(char))
68
69
70def escape(line):
71 return "".join([escapech(i) for i in line])
72
73
74with open(inname, "r") as fd:
75 data = fd.read()
76
77sub_name = ""
78rev = ""
79sub_rev = ""
80
81# XML support isn't actively used currently, but it's here in case the need
82# arises. It does avoid the regex'ing.
83if "<?xml" in data:
84 from xml.etree import ElementTree
85
86 xml = ElementTree.fromstring(data)
87 name = xml.get("name")
88 rev = xml.find("{urn:ietf:params:xml:ns:yang:yin:1}revision").get("date")
89 fmt = "LYS_YIN"
90else:
91 search_name = re_name.search(data)
92 if search_name:
93 name = search_name.group(1)
94 rev = re_rev.search(data).group(1)
95 else:
96 search_name = re_subname.search(data)
97 sub_name = search_name.group(1)
98 name = re_mainname.search(data).group(1)
99 sub_rev = re_rev.search(data).group(1)
100 fmt = "LYS_IN_YANG"
101
102if name is None or rev is None:
103 raise ValueError("cannot determine YANG module name and revision")
104
105lines = [escape(row) for row in data.split("\n")]
106text = '\\n"\n\t"'.join(lines)
107
108with open(outname, "w") as fd:
109 fd.write(
110 template
111 % (text, escape(name), escape(rev), escape(sub_name), escape(sub_rev), fmt)
112 )