Skip to content
Snippets Groups Projects
Commit 058044f0 authored by Jakub Ružička's avatar Jakub Ružička
Browse files

templates: add pkgstyle-specific template env

pkgstyles can now define TEMPLATE_ENV dict containing variables and
functions available during templating when using that particular style
as well as TEMPLATE_ENV_DYNAMIC dict containing functionas to be
evaluated on template render to fill in dynamic template variables.

New {{ now }} dynamic variable containing current date in suitable
changelog format is now available in templates using deb or rpm
pkgstyles through this new mechanism.
parent 30f053d8
No related branches found
No related tags found
1 merge request!83refactor: flexible template selection
......@@ -12,7 +12,12 @@ and its many clones such as Ubuntu or Mint.
* core: `devscripts`
* isolated build: `pbuilder`
**template variables:**
* `now`: current date in Debian changelog format (RFC 2822)
"""
import email.utils
import glob
import os
from pathlib import Path
......@@ -44,6 +49,18 @@ DISTRO_REQUIRES = {
}
def format_date():
"""
current date and time in Debian changelog format (RFC 2822)
"""
return email.utils.formatdate(localtime=True)
TEMPLATE_ENV_DYNAMIC = {
'now': format_date,
}
RE_PKG_NAME = r'Source:\s*(\S+)'
# orbital regexp cannon to parse Build-Depends from debian/control
RE_BUILD_DEPENDS = (
......
......@@ -12,7 +12,12 @@ such as Fedora, CentOS, SUSE, RHEL.
* core: `rpm-build`
* isolated build: `mock`
**template variables:**
* `now`: current date in RPM changelog format
"""
from datetime import datetime
import glob
from pathlib import Path
import re
......@@ -46,6 +51,18 @@ DISTRO_REQUIRES = {
}
def format_date():
"""
current date and time in RPM changelog format
"""
return datetime.now().strftime('%a %b %d %Y')
TEMPLATE_ENV_DYNAMIC = {
'now': format_date,
}
RE_PKG_NAME = r'Name:\s*(\S+)'
RE_BUILD_REQUIRES = r'BuildRequires:\s*(.*)'
RE_RPMBUILD_OUT_RPM = r'Wrote:\s+(.*\.rpm)\s*'
......
......@@ -40,6 +40,24 @@ class PackageTemplate:
self.style = _pkgstyle.get_pkgstyle_for_template(self.path)
return self.style
def template_env(self, env=None):
"""
get/update template env from pkgstyle
"""
# static vars
tenv = getattr(self.pkgstyle, 'TEMPLATE_ENV', {})
# dynamic vars resolved at render time
denv = getattr(self.pkgstyle, 'TEMPLATE_ENV_DYNAMIC', {})
if denv:
denv_vars = {}
for name, fun in denv.items():
denv_vars[name] = fun()
tenv.update(denv_vars)
# custom supplied vars
if env:
tenv.update(env)
return tenv
def render(self, out_path, env,
render_filter=default_render_filter,
includes=None, excludes=None):
......@@ -72,6 +90,8 @@ class PackageTemplate:
else:
out_path.mkdir(parents=True, exist_ok=True)
env = self.template_env(env=env)
# recursively render all files
for d, _, files in shutil.walk(self.path):
rel_dir = Path(d).relative_to(self.path)
......@@ -105,6 +125,7 @@ class PackageTemplate:
render template file in memory and return its content
"""
src = self.path / name
env = self.template_env(env=env)
with src.open('r') as srcf:
t = jinja2.Template(srcf.read())
return t.render(**env) + '\n'
apkg ({{ version }}-{{ release }}) unstable; urgency=medium
* new upstream version {{ version }}
* upstream version {{ version }}
-- Jakub Ružička <jakub.ruzicka@nic.cz> Tue, 27 Oct 2020 16:20:00 +0100
-- Jakub Ružička <jakub.ruzicka@nic.cz> {{ now }}
......@@ -69,5 +69,5 @@ This package contains apkg module for Python 3.
%changelog
* Mon Mar 01 2021 Jakub Ružička <jakub.ruzicka@nic.cz> - 0.1.0-1
- initial release
* {{ now }} Jakub Ružička <jakub.ruzicka@nic.cz> - {{ version }}-{{ release }}
- upstream version {{ version }}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment