Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 1 | #!/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 | |
| 16 | import glob |
| 17 | import inspect |
| 18 | import os |
| 19 | import re |
| 20 | import sys |
| 21 | |
| 22 | |
| 23 | class 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 | |
| 35 | class PluginRenderer(ContentRenderer): |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 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. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 41 | path = os.path.join(self.plugin_dir(), entry.name, "**") |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 42 | for f in glob.iglob(path, recursive=True): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 43 | if not f.endswith(".c"): |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 44 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 58 | if not entry.name.startswith(".") and entry.is_dir(): |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 59 | self._render_entry(output_file, entry) |
| 60 | |
| 61 | |
| 62 | renderers = [PluginRenderer] |
| 63 | |
| 64 | |
| 65 | def 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 | |
| 76 | if __name__ == "__main__": |
| 77 | main() |