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

Fix #5911: sirepo.resource.render_resource() #6088

Merged
merged 16 commits into from
Aug 14, 2023
2 changes: 2 additions & 0 deletions sirepo/package_data/resource_test_data/README.txt.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This file tests sirepo.resource.render (see tests/resource_test.py)
var = "{{ var }}"
18 changes: 6 additions & 12 deletions sirepo/pkcli/nersc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _Sequential(PKDict):
"""Run a sequential job by mocking the input to job_cmd"""

JOB_CMD_FILE = "sequential_job_cmd.json"
RESOURCE_DIR = "nersc_test/"
RESOURCE_DIR = "nersc_test"
RESULT_FILE = "sequential_result.json"
RUN_DIR = "sirepo_run_dir"
RUN_FILE = "sequential_run.sh"
Expand Down Expand Up @@ -71,11 +71,6 @@ def execute(self):
if self.result_parsed.state != "completed":
raise RuntimeError(f"unexpected result state={self.result_parsed.state}")

def _file_path(self, filename):
return sirepo.resource.file_path(
self.RESOURCE_DIR + filename + pykern.pkjinja.RESOURCE_SUFFIX
)

def _job_cmd_file(self):
if self.pkunit_deviance:
return pykern.pkio.py_path(self.pkunit_deviance)
Expand All @@ -93,14 +88,13 @@ def __str__(self):
return res

def _render_resource(self, filename):
res = self.run_dir.join(filename)
pykern.pkjinja.render_file(
self._file_path(filename),
PKDict(
return sirepo.resource.render_jinja(
self.RESOURCE_DIR,
filename,
target_dir=self.run_dir,
j2_ctx=PKDict(
job_cmd_file=self.get("job_cmd_file"),
run_dir=self.run_dir,
user=self.user,
),
output=res,
)
return res
22 changes: 22 additions & 0 deletions sirepo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import importlib
import os
import pykern.pkio
import pykern.pkjinja
import sirepo.const
import sirepo.feature_config
import sirepo.util
Expand Down Expand Up @@ -44,6 +45,27 @@ def glob_paths(*paths):
)


def render_jinja(*paths, target_dir=None, j2_ctx=None):
"""Render a resource template file with Jinja into target_dir.

Args:
paths (str): Path components of resource file without pykern.pkjinja.RESOURCE_SUFFIX
target_dir (py.path): target directory for rendered file
j2_ctx (PKDict): parameters to jinja file

Returns:
py.path: output path which is target_dir.join(paths[-1])
"""
f = paths[-1]
res = target_dir.join(f)
pykern.pkjinja.render_file(
file_path(*paths[:-1], f + pykern.pkjinja.RESOURCE_SUFFIX),
j2_ctx,
output=res,
)
return res


def root_modules():
"""Get all root modules in package_path

Expand Down
21 changes: 21 additions & 0 deletions tests/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ def test_static_files():
"myapp-schema not in list={}",
x,
)


def test_render_resource():
from sirepo import resource
from pykern.pkcollections import PKDict
from pykern import pkunit
from pykern import pkio

pkunit.pkre(
'var = "x"',
pkio.read_text(
resource.render_jinja(
"resource_test_data",
"README.txt",
target_dir=pkunit.work_dir(),
j2_ctx=PKDict(
var="x",
),
)
),
)