Skip to content

Commit

Permalink
Fix #5911: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
git-user committed Jul 19, 2023
1 parent a4d2a35 commit 271693e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
8 changes: 4 additions & 4 deletions sirepo/pkcli/nersc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def __str__(self):
return res

def _render_resource(self, filename):
return sirepo.resource.render_resource(
filename,
return sirepo.resource.render(
self.RESOURCE_DIR,
self.run_dir,
PKDict(
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,
Expand Down
25 changes: 9 additions & 16 deletions sirepo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ def glob_paths(*paths):
)


def render_resource(template_name, resource_dir, run_dir, j2_ctx):
"""Render a jinja resource from a package_data/resource_dir
into run_dir
def render(*paths, target_dir=None, j2_ctx=None):
"""Render a jinja resource into target_dir
Args:
resource_template (str): .jinja template basename
resource_dir (str): package_data dir that .jinja template lives in
run_dir (py.path): target directory for rendered file
jinja_params (PKDict): parameters to jinja file
paths (str): Path components of file
target_dir (py.path): target directory for rendered file
j2_ctx (PKDict): parameters to jinja file
Returns:
py.path: path to rendered file
"""
res = run_dir.join(template_name)
res = target_dir.join(paths[-1])
p = list(paths)
p[-1] += pykern.pkjinja.RESOURCE_SUFFIX
pykern.pkjinja.render_file(
_resource_path(template_name, resource_dir),
file_path(*p),
j2_ctx,
output=res,
)
Expand Down Expand Up @@ -133,10 +133,3 @@ 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)
8 changes: 4 additions & 4 deletions tests/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_render_resource():
pkunit.pkre(
'var = "x"',
pkio.read_text(
resource.render_resource(
"README.txt",
resource.render(
"resource_test_data",
pkunit.work_dir(),
PKDict(
"README.txt",
target_dir=pkunit.work_dir(),
j2_ctx=PKDict(
var="x",
),
)
Expand Down

0 comments on commit 271693e

Please sign in to comment.