systemd/tools/meson-render-jinja2.py
Zbigniew Jędrzejewski-Szmek a324a8958b meson: adjust whitespace handling in jinja2 rendering
In 6abe882bae the renderer was made to
unconditionally append a newline to output. This works, but is ugly. A nicer
solution is to tell jinja2 to not strip the newline in the first place, via
keep_trailing_newline=True. It seems that the result is unchanged because all
our source files have exactly one trailing newline.

Also, enable lstrip_blocks=True. This would cause whitespace on the line before
an {%if block to be automatically stripped. It seems reasonable to enable that
if trim_blocks=True.

Overall, no change is expected, though I didn't test combinations of
configurations, so there might be a change in some cases. But now the rules of
rendering are more logical, e.g. we should be able to indent nested conditional
statements without getting unexpected whitespace in the output.
2023-02-21 06:41:19 +09:00

41 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
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)
if __name__ == '__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)