]> git.proxmox.com Git - mirror_frr.git/blob - yang/embedmodel.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / yang / embedmodel.py
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
7 import sys
8 import os
9 import string
10 import re
11
12 inname = sys.argv[1]
13 outname = sys.argv[2]
14
15 outdir = os.path.dirname(os.path.abspath(outname))
16 try:
17 os.makedirs(outdir)
18 except 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
25 re_name = re.compile(r"\bmodule\s+([^\s]+)\s+\{")
26 re_subname = re.compile(r"\bsubmodule\s+([^\s]+)\s+\{")
27 re_mainname = re.compile(r"\bbelongs-to\s+([^\s]+)\s+\{")
28 re_rev = re.compile(r"\brevision\s+([\d-]+)\s+\{")
29
30
31 template = """/* autogenerated by embedmodel.py. DO NOT EDIT */
32
33 #include <zebra.h>
34 #include "yang.h"
35
36 static const char model[] =
37 \t"%s";
38
39 static 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
48 static void embed_register(void) __attribute__((_CONSTRUCTOR(2000)));
49 static void embed_register(void)
50 {
51 \tyang_module_embed(&embed);
52 }
53 """
54
55 passchars = set(string.printable) - set("\\'\"%\r\n\t\x0b\x0c")
56
57
58 def 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
70 def escape(line):
71 return "".join([escapech(i) for i in line])
72
73
74 with open(inname, "r") as fd:
75 data = fd.read()
76
77 sub_name = ""
78 rev = ""
79 sub_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.
83 if "<?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"
90 else:
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
102 if name is None or rev is None:
103 raise ValueError("cannot determine YANG module name and revision")
104
105 lines = [escape(row) for row in data.split("\n")]
106 text = '\\n"\n\t"'.join(lines)
107
108 with open(outname, "w") as fd:
109 fd.write(
110 template
111 % (text, escape(name), escape(rev), escape(sub_name), escape(sub_rev), fmt)
112 )