1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00
systemd-stable/tools/meson-render-jinja2.py
Zbigniew Jędrzejewski-Szmek 5f035b13de meson-render-jinja2: use ast.literal_eval()
Imports are sorted in the usual fashion: stdlib first.

literal_eval() parses string/numbers/lists/sets/dicts, and nothing else, while
eval will execute any python code. Using literal_eval() is generally more
correct, because it avoids the risk of side effects from the parsed expression.
In this case, we generate the parsed strings ourselves, so it's very unlikely
to have anything unexpected in the expressions. But let's do the correct thing
anyway.
2021-11-05 14:56:32 +01:00

31 lines
767 B
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
import ast
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, undefined=jinja2.StrictUndefined)
return template.render(defines)
if __name__ == '__main__':
defines = parse_config_h(sys.argv[1])
print(render(sys.argv[2], defines))