2 # Copyright (c) 2020. Vinci Consulting Corp. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 class ContentRenderer:
24 def __init__(self, ws_root, output_dir):
25 self.ws_root = ws_root
26 self.output_dir = output_dir
29 return os.path.join(self.ws_root, "src/plugins")
32 raise NotImplementedError
35 class PluginRenderer(ContentRenderer):
36 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.
41 path = os.path.join(self.plugin_dir(), entry.name, "**")
42 for f in glob.iglob(path, recursive=True):
43 if not f.endswith(".c"):
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))
49 output_file.write(f"* {entry.name} - {description}\n")
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):
58 if not entry.name.startswith(".") and entry.is_dir():
59 self._render_entry(output_file, entry)
62 renderers = [PluginRenderer]
66 if len(sys.argv) != 3:
67 print("You need to pass WS_ROOT and OUTPUT_DIR")
70 print("rendering dynamic includes...")
71 for renderer in renderers:
72 renderer(*sys.argv[1:]).render()
76 if __name__ == "__main__":