Skip to content

Commit

Permalink
Fix #5972 #5911: sirepo.render_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
git-user committed Jul 7, 2023
1 parent 4ac788b commit 62e233d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sirepo/package_data/nersc_test/parameters.py.jinja
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print("Running parallel parameters.py")
print("parallel test completed")
26 changes: 11 additions & 15 deletions sirepo/pkcli/nersc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import subprocess

# TODO (gurhar1133): remove repitition in parallel() and sequential()
def parallel():
def parallel(pkunit_deviance=None):
try:
p = _Parallel(
JOB_CMD_FILE="parameters.py",
Expand All @@ -26,7 +26,7 @@ def parallel():
RUN_FILE="sbatch_script.sh",
RUN_CMD="sbatch",
)
p.prepare(None)
p.prepare(pkunit_deviance)
p.execute()
return "nersc_test.parallel PASS"
except Exception as e:
Expand Down Expand Up @@ -87,28 +87,22 @@ def execute(self):
self.result_file.write(p.stdout)
self.result_text = p.stdout

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)
return self._render_resource(self.JOB_CMD_FILE)

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


class _Sequential(_NERSCTestBase):
Expand All @@ -132,6 +126,8 @@ def __str__(self):


class _Parallel(_NERSCTestBase):
# TODO (gurhar1133): needs to check RESULT_FILE in execute()
# after calling super().execute()
pass
"""Run a parallel job mocking script that invokes 'sbatch'"""
def execute(self):
super().execute()
if self.result_text.strip() != "parallel test completed":
raise RuntimeError(f"unexpected result state={self.result_text}")
23 changes: 23 additions & 0 deletions sirepo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ def glob_paths(*paths):
)


def render_resource(filename, resource_dir, run_dir, jinja_params):
"""Render .jinja filename from resource_dir into run_dir
Args:
filename (str): .jinja filename
resource_dir (str): dir that .jinja template lives in
run_dir (py.path): target directory for rendered file
jinja_params (PKDict): parameters to jinja file
Returns:
py.path: path to rendered file
"""
from pykern import pkjinja

res = run_dir.join(filename)
pykern.pkjinja.render_file(
file_path(resource_dir + filename + pkjinja.RESOURCE_SUFFIX),
jinja_params,
output=res,
)
return res


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

0 comments on commit 62e233d

Please sign in to comment.