Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Jinja filter to aid with version checking #599

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions osbenchmark/builder/utils/template_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from osbenchmark.exceptions import InvalidSyntax, SystemSetupError
from osbenchmark.utils import io
from osbenchmark.workload import loader


class TemplateRenderer:
Expand All @@ -11,6 +12,7 @@ def render_template_file(self, root_path, variables, file_name):

def _render_template_file(self, root_path, variables, file_name):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(root_path), autoescape=select_autoescape(['html', 'xml']))
env.filters["version_between"] = loader.version_between
template = env.get_template(io.basename(file_name))
# force a new line at the end. Jinja seems to remove it.
return template.render(variables) + "\n"
Expand All @@ -20,6 +22,7 @@ def render_template_string(self, template_string, variables):

def _render_template_string(self, template_string, variables):
env = jinja2.Environment(loader=jinja2.BaseLoader, autoescape=select_autoescape(['html', 'xml']))
env.filters["version_between"] = loader.version_between
template = env.from_string(template_string)

return template.render(variables)
Expand Down
8 changes: 8 additions & 0 deletions osbenchmark/workload/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ def read_glob_files(self, pattern):
return ",\n".join(source)


# A Jinja filter that tests if a version string lies within a specified range.
# For instance, "1.2.3" lies between "1.0.0" and "2.0.0".
def version_between(version, frm, to):
return list(map(int, version.split('.'))) >= list(map(int, frm.split('.'))) and \
list(map(int, version.split('.'))) <= list(map(int, to.split('.')))


def default_internal_template_vars(glob_helper=lambda f: [], clock=time.Clock):
"""
Dict of internal global variables used by our jinja2 renderers
Expand Down Expand Up @@ -751,6 +758,7 @@ def render_template(template_source, template_vars=None, template_internal_vars=
for env_global_key, env_global_value in template_internal_vars[macro_type].items():
getattr(env, macro_type)[env_global_key] = env_global_value

env.filters["version_between"] = version_between
template = env.from_string(template_source)
return template.render()

Expand Down
12 changes: 12 additions & 0 deletions tests/builder/utils/template_renderer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def test_successful_render(self, get_template):

self.template_renderer.render_template_file(self.root_path, self.variables, self.file_name)

def test_version_between_filter(self):
self.assertEqual(self.template_renderer.render_template_string('{{ "2.0.0" | version_between("2.0.0", "3.0.0")}}',
self.variables), "True")
self.assertEqual(self.template_renderer.render_template_string('{{ "2.2.3" | version_between("2.0.0", "3.0.0")}}',
self.variables), "True")
self.assertEqual(self.template_renderer.render_template_string('{{ "3.0.0" | version_between("2.0.0", "3.0.0")}}',
self.variables), "True")
self.assertEqual(self.template_renderer.render_template_string('{{ "1.9.0" | version_between("2.0.0", "3.0.0")}}',
self.variables), "False")
self.assertEqual(self.template_renderer.render_template_string('{{ "3.0.1" | version_between("2.0.0", "3.0.0")}}',
self.variables), "False")

@mock.patch('jinja2.Environment.get_template')
def test_template_syntax_error(self, get_template):
get_template.side_effect = TemplateSyntaxError("fake", 12)
Expand Down
Loading