Skip to content

Commit

Permalink
Merge pull request #235 from Perfexionists/jinja-fix
Browse files Browse the repository at this point in the history
Fix jinja2 issue with editable build
  • Loading branch information
tfiedor authored Jul 17, 2024
2 parents a7de6c6 + 8f7420d commit 2aa98d6
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 17 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true

# Tests editable build using Tox for selected Python versions
build-editable:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3

- name: Setup Python, Ubuntu and Python environment
uses: ./.github/workflows/actions/setup
with:
python-version: ${{ matrix.python-version }}

- name: Execute tests for Python ${{ matrix.python-version }} using Tox
run: tox -e editable

# Tests that perun is buildable from distribution packages (this is precursor for pypi install).
# We limit the test to version 3.11 in order to have less clutter in Actions
build-from-dist:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ might require root permissions to install Perun.

It is advised to verify that Perun is running correctly in your environment as follows:

# You can run this only once: it will initialize the requirements necessary for testing
make init-test
# Runs all tests using pytest
make test

Expand Down
45 changes: 45 additions & 0 deletions perun/templates/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Functions for working with templates
"""

from __future__ import annotations

# Standard Imports
from pathlib import Path
from typing import Callable, Any, Optional, Mapping

# Third-Party Imports
from jinja2 import Environment, FileSystemLoader, Template

# Perun Imports


def get_template(
template_name: str, filters: Optional[Mapping[str, Callable[[str], str]]] = None
) -> Template:
"""Loads jinja2 template from the templates directory
Note: there are some issues with using PackageLoader of Jinja2 in combination
with meson.build; when using the editable install (make dev) it seems that it
nondeterministically sets the wrong path resulting into errors. It works fine
for classic installation (make install).
Hence, we wrapped it in FileSystemLoader with absolute paths.
:return: loaded template from perun/templates directory
"""
# Note: we keep the autoescape=false, since we kindof believe we are not trying to fuck us up
path = Path(__file__).parent
env = Environment(loader=FileSystemLoader(path))
for filter_name, filter_func in (filters or {}).items():
env.filters[filter_name] = filter_func
return env.get_template(template_name)


def get_environment(**kwargs: Any) -> Environment:
"""Returns Jinja2 environment for working with template
:return: jinja environment
"""
path = Path(__file__).parent
return Environment(loader=FileSystemLoader(path), **kwargs)
1 change: 1 addition & 0 deletions perun/templates/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ perun_templates_files = files(
'diff_view_datatables.html.jinja2',
'diff_view_report.html.jinja2',
'diff_view_sankey.html.jinja2',
'factory.py',
'filters.py',
'jquery-3.6.0.min.js',
'macros_accordion.html.jinja2',
Expand Down
5 changes: 2 additions & 3 deletions perun/utils/common/script_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

# Third-Party Imports

import jinja2

# Perun Imports
from perun.logic import config
from perun.templates import factory as templates
from perun.utils import log
from perun.utils.common import common_kit
from perun.utils.exceptions import ExternalEditorErrorException
Expand Down Expand Up @@ -71,7 +70,7 @@ def template_name_filter(template_name: str) -> bool:
log.minor_status("Target Perun development dir", status=log.path_style(perun_dev_dir))

# Initialize the jinja2 environment and load all templates for template_type set
env = jinja2.Environment(loader=jinja2.PackageLoader("perun", "templates"), autoescape=True)
env = templates.get_environment(autoescape=True)
list_of_templates = env.list_templates(filter_func=template_name_filter)

# Specify the target dir (for packages we create a new directory)
Expand Down
4 changes: 2 additions & 2 deletions perun/view_diff/datatables/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import progressbar

# Perun Imports
from perun.templates import factory as templates
from perun.utils import log
from perun.utils.common import diff_kit, traces_kit
from perun.profile.factory import Profile
Expand Down Expand Up @@ -209,9 +210,8 @@ def generate_html_report(lhs_profile: Profile, rhs_profile: Profile, **kwargs: A
("[%]", "The relative measured value (in percents overall)."),
]

env = jinja2.Environment(loader=jinja2.PackageLoader("perun", "templates"))
lhs_header, rhs_header = diff_kit.generate_headers(lhs_profile, rhs_profile)
template = env.get_template("diff_view_datatables.html.jinja2")
template = templates.get_template("diff_view_datatables.html.jinja2")
content = template.render(
lhs_tag="Baseline (base)",
lhs_columns=columns,
Expand Down
4 changes: 2 additions & 2 deletions perun/view_diff/flamegraph/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jinja2

# Perun Imports
from perun.templates import factory as templates
from perun.utils import log, mapping
from perun.utils.common import diff_kit
from perun.profile.factory import Profile
Expand Down Expand Up @@ -195,8 +196,7 @@ def generate_flamegraph_difference(
)
lhs_header, rhs_header = diff_kit.generate_headers(lhs_profile, rhs_profile)

env = jinja2.Environment(loader=jinja2.PackageLoader("perun", "templates"))
template = env.get_template("diff_view_flamegraph.html.jinja2")
template = templates.get_template("diff_view_flamegraph.html.jinja2")
content = template.render(
flamegraphs=flamegraphs,
lhs_header=lhs_header,
Expand Down
8 changes: 3 additions & 5 deletions perun/view_diff/report/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from perun.logic import config
from perun.profile import convert
from perun.profile.factory import Profile
from perun.templates import filters
from perun.templates import filters, factory as templates
from perun.utils import log, mapping
from perun.utils.common import diff_kit, common_kit
from perun.utils.structs import WebColorPalette
Expand Down Expand Up @@ -684,10 +684,8 @@ def generate_report(lhs_profile: Profile, rhs_profile: Profile, **kwargs: Any) -
log.minor_success("Sankey graphs", "generated")
lhs_header, rhs_header = diff_kit.generate_headers(lhs_profile, rhs_profile)

# Note: we keep the autoescape=false, since we kindof believe we are not trying to fuck us up
env = jinja2.Environment(loader=jinja2.PackageLoader("perun", "templates"))
env.filters["sanitize_variable_name"] = filters.sanitize_variable_name
template = env.get_template("diff_view_report.html.jinja2")
env_filters = {"sanitize_variable_name": filters.sanitize_variable_name}
template = templates.get_template("diff_view_report.html.jinja2", filters=env_filters)
content = template.render(
title="Differences of profiles (with sankey)",
lhs_tag="Baseline (base)",
Expand Down
5 changes: 2 additions & 3 deletions perun/view_diff/sankey/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import progressbar

# Perun Imports
from perun.templates import factory as templates
from perun.profile import convert
from perun.profile.factory import Profile
from perun.utils import log
Expand Down Expand Up @@ -528,9 +529,7 @@ def generate_sankey_difference(lhs_profile: Profile, rhs_profile: Profile, **kwa
]
lhs_header, rhs_header = diff_kit.generate_headers(lhs_profile, rhs_profile)

# Note: we keep the autoescape=false, since we kindof believe we are not trying to fuck us up
env = jinja2.Environment(loader=jinja2.PackageLoader("perun", "templates"))
template = env.get_template("diff_view_sankey.html.jinja2")
template = templates.get_template("diff_view_sankey.html.jinja2")
content = template.render(
title="Differences of profiles (with sankey)",
lhs_tag="Baseline (base)",
Expand Down
9 changes: 9 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ env_list =
lint
typing
docs
editable

[testenv:editable]
description = Run Perun tests in editable install
package = editable
allowlist_externals = make
extras = test
commands =
make test-ci

[testenv]
description = Run Perun tests
Expand Down

0 comments on commit 2aa98d6

Please sign in to comment.