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
16 changes: 5 additions & 11 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),
return sirepo.resource.render_resource(
filename,
self.RESOURCE_DIR,
self.run_dir,
PKDict(
job_cmd_file=self.get("job_cmd_file"),
run_dir=self.run_dir,
user=self.user,
),
output=res,
)
return res
29 changes: 29 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_resource(filename, resource_dir, run_dir, jinja_params):
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
"""Render .jinja filename from resource_dir into run_dir

Args:
filename (str): .jinja filename
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
resource_dir (str or py.path): dir that .jinja template lives in
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
run_dir (py.path): target directory for rendered file
jinja_params (PKDict): parameters to jinja file

Returns:
py.path: path to rendered file
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
"""
res = run_dir.join(filename)
pykern.pkjinja.render_file(
_resource_path(filename, resource_dir),
jinja_params,
output=res,
)
return res


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

Expand Down Expand Up @@ -110,3 +132,10 @@ def _join_paths(paths):
a = [p for p in paths if os.path.isabs(p)]
assert not a, f"absolute paths={a} in paths={paths}"
return os.path.join(*paths)


def _resource_path(filename, resource_dir):
f = filename + pykern.pkjinja.RESOURCE_SUFFIX
if type(resource_dir) == str:
return file_path(resource_dir).join(f)
return resource_dir.join(f)
2 changes: 2 additions & 0 deletions tests/resource_data/actual.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "{{ x }}"
y = "{{ y }}"
2 changes: 2 additions & 0 deletions tests/resource_data/expect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "x"
y = "y"
20 changes: 20 additions & 0 deletions tests/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
import pytest
from pykern.pkcollections import PKDict
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved


def test_static_files():
Expand All @@ -17,3 +18,22 @@ def test_static_files():
"myapp-schema not in list={}",
x,
)


def test_render_resource():
from sirepo import resource
from pykern import pkunit

d = pkunit.data_dir()
pkunit.file_eq(
d.join("expect.py"),
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
actual_path=resource.render_resource(
"actual.py",
d,
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
pkunit.work_dir(),
PKDict(
x="x",
gurhar1133 marked this conversation as resolved.
Show resolved Hide resolved
y="y",
),
),
)