Skip to content

Commit

Permalink
Merge pull request #124 from CABLE-LSM/123-run-cable-exe-from-task-dir
Browse files Browse the repository at this point in the history
Run executable from task dir
  • Loading branch information
SeanBryan51 authored Aug 7, 2023
2 parents 138b877 + b5c2169 commit 79e7fa4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
14 changes: 1 addition & 13 deletions benchcab/repository.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""A module containing functions and data structures for manipulating CABLE repositories."""

import shlex
import contextlib
import os
import shutil
import stat
from pathlib import Path
Expand All @@ -11,17 +9,7 @@
from benchcab import internal
from benchcab.environment_modules import EnvironmentModulesInterface, EnvironmentModules
from benchcab.utils.subprocess import SubprocessWrapperInterface, SubprocessWrapper


@contextlib.contextmanager
def chdir(newdir: Path):
"""Context manager `cd`."""
prevdir = Path.cwd()
os.chdir(newdir.expanduser())
try:
yield
finally:
os.chdir(prevdir)
from benchcab.utils.os import chdir


class CableRepository:
Expand Down
12 changes: 7 additions & 5 deletions benchcab/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from benchcab.repository import CableRepository
from benchcab.comparison import ComparisonTask
from benchcab.utils.subprocess import SubprocessWrapperInterface, SubprocessWrapper
from benchcab.utils.os import chdir


# fmt: off
Expand Down Expand Up @@ -260,14 +261,15 @@ def run_cable(self, verbose=False):
"""
task_name = self.get_task_name()
task_dir = self.root_dir / internal.FLUXSITE_TASKS_DIR / task_name
exe_path = task_dir / internal.CABLE_EXE
nml_path = task_dir / internal.CABLE_NML
stdout_path = task_dir / internal.CABLE_STDOUT_FILENAME

try:
self.subprocess_handler.run_cmd(
f"{exe_path} {nml_path}", output_file=stdout_path, verbose=verbose
)
with chdir(task_dir):
self.subprocess_handler.run_cmd(
f"./{internal.CABLE_EXE} {internal.CABLE_NML}",
output_file=stdout_path,
verbose=verbose,
)
except CalledProcessError as exc:
print(f"Error: CABLE returned an error for task {task_name}")
raise CableError from exc
Expand Down
16 changes: 16 additions & 0 deletions benchcab/utils/os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Utility functions which wrap around the os module."""

import os
import contextlib
from pathlib import Path


@contextlib.contextmanager
def chdir(newdir: Path):
"""Context manager `cd`."""
prevdir = Path.cwd()
os.chdir(newdir.expanduser())
try:
yield
finally:
os.chdir(prevdir)
9 changes: 2 additions & 7 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,11 @@ def test_run_cable():
task = get_mock_task(subprocess_handler=mock_subprocess)
task_dir = MOCK_CWD / internal.FLUXSITE_TASKS_DIR / task.get_task_name()
task_dir.mkdir(parents=True)
exe_path = task_dir / internal.CABLE_EXE
exe_path.touch()
nml_path = task_dir / internal.CABLE_NML
nml_path.touch()

stdout_file = task_dir / internal.CABLE_STDOUT_FILENAME

# Success case: run CABLE executable in subprocess
task.run_cable()
assert f"{exe_path} {nml_path}" in mock_subprocess.commands
assert f"./{internal.CABLE_EXE} {internal.CABLE_NML}" in mock_subprocess.commands
stdout_file = task_dir / internal.CABLE_STDOUT_FILENAME
assert stdout_file.exists()

# Success case: test non-verbose output
Expand Down

0 comments on commit 79e7fa4

Please sign in to comment.