Skip to content

Commit

Permalink
Run executable from task dir
Browse files Browse the repository at this point in the history
Currently, the CABLE executable is run from the current working
directory instead of the task directory by using the absolute path to
the executable. However, we should be running the executable from the
task directory (otherwise CABLE will complain it cannot find the soil
and PFT namelist files). This was a bug that was introduced in the #99.

This change makes it so we change into the task directory before running
the CABLE executable.

Fixes #123
  • Loading branch information
SeanBryan51 committed Aug 4, 2023
1 parent 138b877 commit 2d1de0c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 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)
2 changes: 1 addition & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def test_run_cable():

# Success case: run CABLE executable in subprocess
task.run_cable()
assert f"{exe_path} {nml_path}" in mock_subprocess.commands
assert f"./{exe_path.name} {nml_path.name}" in mock_subprocess.commands
assert stdout_file.exists()

# Success case: test non-verbose output
Expand Down

0 comments on commit 2d1de0c

Please sign in to comment.