Skip to content

Commit

Permalink
Custom py script for jinja2 rendering
Browse files Browse the repository at this point in the history
As alternative to currently j2cli or such feature-full/heavy tool,
small custom python script is enough for rendering jinja2 templates.

Closes #77
  • Loading branch information
queria committed Jun 14, 2024
1 parent 3807a4a commit b167cda
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion generate-docker-compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ while read line; do
export distro=$(echo "$line" | awk -F, '{ print $4 }')
touch "docker-compose/docker-compose-${shortname}.yml"
echo "Generating docker-compose-${shortname}.yml (${gamename})"
j2 -f env docker-compose.yml.j2 >"docker-compose/docker-compose-${shortname}.yml"
./j2render.py docker-compose.yml.j2 >"docker-compose/docker-compose-${shortname}.yml"
echo -n "{" >>"shortnamearray.json"
echo -n "\"shortname\":" >>"shortnamearray.json"
echo -n "\"${shortname}\"" >>"shortnamearray.json"
Expand Down
2 changes: 1 addition & 1 deletion generate-dockerfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ while read line; do
export distro=$(echo "$line" | awk -F, '{ print $4 }')
touch "dockerfiles/Dockerfile.${shortname}"
echo "Generating Dockerfile.${shortname} (${gamename})"
j2 -f env Dockerfile.j2 >"dockerfiles/Dockerfile.${shortname}"
./j2render.py Dockerfile.j2 >"dockerfiles/Dockerfile.${shortname}"
echo -n "{" >>"shortnamearray.json"
echo -n "\"shortname\":" >>"shortnamearray.json"
echo -n "\"${shortname}\"" >>"shortnamearray.json"
Expand Down
21 changes: 21 additions & 0 deletions j2render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python

import os
import sys

from jinja2 import Environment, FileSystemLoader, StrictUndefined

env = Environment(
loader=FileSystemLoader('./'),
undefined=StrictUndefined)

variables = dict(os.environ)

try:
template_path = sys.argv[1]
except IndexError:
print("ERROR: provide path to the template file", file=sys.stderr)
sys.exit(1)

tpl = env.get_template(template_path)
print(tpl.render(**variables))

0 comments on commit b167cda

Please sign in to comment.