summaryrefslogtreecommitdiffstats
path: root/tools/meson-render-jinja2.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
commit55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch)
tree33f869f55a1b149e9b7c2b7e201867ca5dd52992 /tools/meson-render-jinja2.py
parentInitial commit. (diff)
downloadsystemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz
systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/meson-render-jinja2.py')
-rwxr-xr-xtools/meson-render-jinja2.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/meson-render-jinja2.py b/tools/meson-render-jinja2.py
new file mode 100755
index 0000000..3a3d912
--- /dev/null
+++ b/tools/meson-render-jinja2.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# pylint: disable=consider-using-with
+
+import ast
+import os
+import re
+import sys
+
+import jinja2
+
+def parse_config_h(filename):
+ # Parse config.h file generated by meson.
+ ans = {}
+ for line in open(filename):
+ m = re.match(r'#define\s+(\w+)\s+(.*)', line)
+ if not m:
+ continue
+ a, b = m.groups()
+ if b and b[0] in '0123456789"':
+ b = ast.literal_eval(b)
+ ans[a] = b
+ return ans
+
+def render(filename, defines):
+ text = open(filename).read()
+ template = jinja2.Template(text,
+ trim_blocks=True,
+ lstrip_blocks=True,
+ keep_trailing_newline=True,
+ undefined=jinja2.StrictUndefined)
+ return template.render(defines)
+
+def main():
+ defines = parse_config_h(sys.argv[1])
+ defines.update(parse_config_h(sys.argv[2]))
+ output = render(sys.argv[3], defines)
+ with open(sys.argv[4], 'w') as f:
+ f.write(output)
+ info = os.stat(sys.argv[3])
+ os.chmod(sys.argv[4], info.st_mode)
+
+if __name__ == '__main__':
+ main()