Commit 9b805b92 authored by Jakub Ružička's avatar Jakub Ružička
Browse files

templates: support Jinja include

Support Jinja include directive:

    {% include 'distro/common/foo' %}

Paths are relative to project root.
parent 2cf87822
Pipeline #89478 passed with stages
in 25 minutes and 3 seconds
......@@ -48,6 +48,11 @@ class PackageTemplate:
self.style = style
self.selection = selection
self.distro_rules = None
self.setup_env()
def setup_env(self):
self.env = jinja2.Environment(
loader=jinja2.FileSystemLoader('.'))
@property
def path(self):
......@@ -142,9 +147,7 @@ class PackageTemplate:
# TODO: filtering should be exposed through config
if render_filter(src):
log.verbose("rendering file: %s -> %s", src, dst)
t = None
with src.open('r') as srcf:
t = jinja2.Template(srcf.read())
t = self.env.get_template(str(src))
with dst.open('w') as dstf:
dstf.write(t.render(**tvars) + '\n')
else:
......@@ -160,8 +163,7 @@ class PackageTemplate:
"""
src = self.path / name
tvars = self.template_vars(tvars=tvars)
with src.open('r') as srcf:
t = jinja2.Template(srcf.read())
t = self.env.get_template(str(src))
return t.render(**tvars) + '\n'
def __repr__(self):
......
......@@ -35,6 +35,9 @@ You can use all [jinja] templating features with following variables provided by
* `now`: current date and time in changelog format (provided only by pkgstyles
with changelog dates such as `deb` and `rpm`)
[include] tag is also supported and can be used to include
files relative to project root.
Example Debian `changelog`:
```jinja
......@@ -182,5 +185,6 @@ Examples:
[jinja]: https://jinja.palletsprojects.com/en/3.0.x/templates/
[include]: https://jinja.palletsprojects.com/en/3.0.x/templates/#include
[distro in templates]: distro.md#distro-in-templates
[target distro]: distro.md#target-distro
Included from shared.txt file. Name: {{ name }}
......@@ -2,6 +2,7 @@ apkg-example-templates ({{ version }}-{{ release }}~{{ distro.tiny }}) unstable;
* new upstream version {{ version }}
* distro: {{ distro }} / {{ distro.idver }} / {{ distro.tiny }}
* {% include 'distro/common/shared.txt' %}
{%- if distro.match('debian >= 11', 'ubuntu > 20.04') %}
* new Debian-based
{%- elif distro.match('deb-old') %}
......
......@@ -21,6 +21,7 @@ This is a minimal example of apkg templating features.
* {{ now }} Jakub Ružička <jakub.ruzicka@nic.cz> - {{ version }}-{{ release }}
- new upstream version {{ version }}
- distro: {{ distro }} / {{ distro.idver }} / {{ distro.tiny }}
- {% include 'distro/common/shared.txt' %}
{%- if distro.match('fedora') %}
- Fedora-specific block
{%- elif distro.match('centos <= 7', 'rhel <= 7') %}
......
......@@ -53,7 +53,8 @@ def test_template_select(proj, distro, name, style):
RE_RPM_CH = r"""\* \w{3} \w{3} \d{2} \d{4} [^-]+- ([^-]+)-(\w+)
- new upstream version (\S+)
- distro: ([^\n]+)\n?(.*)\Z"""
- distro: ([^\n]+)
- Included from shared.txt file. Name: apkg-example-templates\n?(.*)\Z"""
@pytest.mark.parametrize('distro,extra', [
......@@ -88,7 +89,8 @@ def test_template_render_rpm(proj, capsys, distro, extra):
RE_DEB_CH = r"""apkg-example-templates \(([^-]+)-([^)]+)\) unstable; .*
\* new upstream version (\S+)
\* distro: ([^\n]+)\n?(.*)
\* distro: ([^\n]+)
\* Included from shared.txt file. Name: apkg-example-templates\n?(.*)
-- .+? \w{3}, \d+ \w{3} \d{4} [\d:]{8} [+-]?\d{4}"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment