blob: a2d422b5c18cf224321090e8707654b41b3a3f10 [file] [log] [blame]
Nathan Skrzypczak9ad39c02021-08-19 11:38:06 +02001#!/usr/bin/env python3
2# Copyright (c) 2020. Vinci Consulting Corp. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import glob
17import inspect
18import os
19import re
20import sys
21
22
23class ContentRenderer:
24 def __init__(self, ws_root, output_dir):
25 self.ws_root = ws_root
26 self.output_dir = output_dir
27
28 def plugin_dir(self):
29 return os.path.join(self.ws_root, "src/plugins")
30
31 def render(self):
32 raise NotImplementedError
33
34
35class PluginRenderer(ContentRenderer):
Nathan Skrzypczak9ad39c02021-08-19 11:38:06 +020036 def _render_entry(self, output_file, entry):
37 description = "<no-description-found>"
38 # we use glob because a plugin can (ioam for now)
39 # define the plugin definition in
40 # a further subdirectory.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041 path = os.path.join(self.plugin_dir(), entry.name, "**")
Nathan Skrzypczak9ad39c02021-08-19 11:38:06 +020042 for f in glob.iglob(path, recursive=True):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 if not f.endswith(".c"):
Nathan Skrzypczak9ad39c02021-08-19 11:38:06 +020044 continue
45 with open(f, "r", encoding="utf-8") as src:
46 for match in self.regex.finditer(src.read()):
47 description = "%s" % (match.group(1))
48
49 output_file.write(f"* {entry.name} - {description}\n")
50
51 def render(self):
52 pattern = r'VLIB_PLUGIN_REGISTER\s?\(\)\s*=\s*{.*\.description\s?=\s?"([^"]*)".*};' # noqa: 501
53 self.regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
54 fname = os.path.join(self.output_dir, "plugin_list.inc")
55 with open(fname, "w") as output_file:
56 with os.scandir(self.plugin_dir()) as pdir:
57 for entry in sorted(pdir, key=lambda entry: entry.name):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020058 if not entry.name.startswith(".") and entry.is_dir():
Nathan Skrzypczak9ad39c02021-08-19 11:38:06 +020059 self._render_entry(output_file, entry)
60
61
62renderers = [PluginRenderer]
63
64
65def main():
66 if len(sys.argv) != 3:
67 print("You need to pass WS_ROOT and OUTPUT_DIR")
68 exit(1)
69
70 print("rendering dynamic includes...")
71 for renderer in renderers:
72 renderer(*sys.argv[1:]).render()
73 print("done.")
74
75
76if __name__ == "__main__":
77 main()