|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Strip Jinja2 constructs from YAML files to make them yamllint-safe. |
| 3 | +
|
| 4 | +This project uses Jinja2 templating ({{% %}}, {{{ }}}, {{# #}}) inside YAML |
| 5 | +files. yamllint cannot parse these constructs, so this script removes them |
| 6 | +while preserving line numbers (replaced regions become blank lines) so that |
| 7 | +yamllint error messages still point to the correct source lines. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + python3 utils/strip_jinja_for_yamllint.py FILE |
| 11 | +
|
| 12 | +The cleaned content is written to stdout. |
| 13 | +""" |
| 14 | + |
| 15 | +import re |
| 16 | +import sys |
| 17 | + |
| 18 | + |
| 19 | +def _replace_with_blanks(match): |
| 20 | + """Replace a match with the same number of newlines to preserve line numbers.""" |
| 21 | + return "\n" * match.group(0).count("\n") |
| 22 | + |
| 23 | + |
| 24 | +def strip_jinja(content): |
| 25 | + # 1. Remove whole-line Jinja block tags: {{% ... %}} on their own line(s). |
| 26 | + # Match the entire line (including leading whitespace) to avoid leaving |
| 27 | + # trailing spaces behind. |
| 28 | + content = re.sub( |
| 29 | + r"^[ \t]*\{\{%.*?%\}\}[ \t]*$", |
| 30 | + _replace_with_blanks, |
| 31 | + content, |
| 32 | + flags=re.MULTILINE | re.DOTALL, |
| 33 | + ) |
| 34 | + # Remove any remaining inline block tags (rare). |
| 35 | + content = re.sub(r"\{\{%.*?%\}\}", _replace_with_blanks, content, flags=re.DOTALL) |
| 36 | + |
| 37 | + # 2. Remove whole-line Jinja comments: {{# ... #}} |
| 38 | + content = re.sub( |
| 39 | + r"^[ \t]*\{\{#.*?#\}\}[ \t]*$", |
| 40 | + _replace_with_blanks, |
| 41 | + content, |
| 42 | + flags=re.MULTILINE | re.DOTALL, |
| 43 | + ) |
| 44 | + # Remove any remaining inline comments. |
| 45 | + content = re.sub(r"\{\{#.*?#\}\}", "", content, flags=re.DOTALL) |
| 46 | + |
| 47 | + # 3a. Standalone Jinja expressions occupying entire lines — these typically |
| 48 | + # expand to top-level YAML keys (e.g. ocil/ocil_clause macros) or |
| 49 | + # Ansible tasks, so replacing them with a placeholder string would |
| 50 | + # produce invalid YAML. Replace with a YAML-safe comment placeholder |
| 51 | + # to avoid trailing whitespace on otherwise blank lines. |
| 52 | + content = re.sub( |
| 53 | + r"^([ \t]*)\{\{\{.*?\}\}\}[ \t]*$", |
| 54 | + lambda m: m.group(1) + "# jinja" + "\n" * (m.group(0).count("\n") - 1) |
| 55 | + if m.group(0).count("\n") > 0 |
| 56 | + else m.group(1) + "# jinja", |
| 57 | + content, |
| 58 | + flags=re.MULTILINE | re.DOTALL, |
| 59 | + ) |
| 60 | + |
| 61 | + # 3b. Inline Jinja expressions embedded inside a YAML value — replace |
| 62 | + # with a short placeholder so the surrounding YAML stays valid. |
| 63 | + content = re.sub(r"\{\{\{.*?\}\}\}", "JINJA_EXPRESSION", content) |
| 64 | + |
| 65 | + return content |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + if len(sys.argv) != 2: |
| 70 | + print(f"Usage: {sys.argv[0]} FILE", file=sys.stderr) |
| 71 | + sys.exit(2) |
| 72 | + |
| 73 | + with open(sys.argv[1]) as f: |
| 74 | + sys.stdout.write(strip_jinja(f.read())) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments