Skip to content

Commit

Permalink
Merge pull request #166 from CABLE-LSM/149-general-tidy-up-of-code
Browse files Browse the repository at this point in the history
A few small changes bundled together.
  • Loading branch information
ccarouge authored Oct 5, 2023
2 parents 162f554 + d3cd1a1 commit de33ae0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion benchcab/benchcab.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from benchcab.environment_modules import EnvironmentModules, EnvironmentModulesInterface
from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface
from benchcab.utils.pbs import render_job_script
from benchcab.utils.logging import next_path
from benchcab.utils.fs import next_path


class Benchcab:
Expand Down
23 changes: 23 additions & 0 deletions benchcab/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@ def copy2(src: Path, dest: Path, verbose=False):
if verbose:
print(f"cp -p {src} {dest}")
shutil.copy2(src, dest)


def next_path(path: Path, path_pattern: str, sep: str = "-"):
"""Finds the next free path in a sequentially named list of
files with the following pattern in the `path` directory:
path_pattern = 'file{sep}*.suf':
file-1.txt
file-2.txt
file-3.txt
"""

loc_pattern = Path(path_pattern)
new_file_index = 1
common_filename, _ = loc_pattern.stem.split(sep)

pattern_files_sorted = sorted(path.glob(path_pattern))
if pattern_files_sorted != []:
common_filename, last_file_index = pattern_files_sorted[-1].stem.split(sep)
new_file_index = int(last_file_index) + 1

return f"{common_filename}{sep}{new_file_index}{loc_pattern.suffix}"
26 changes: 0 additions & 26 deletions benchcab/utils/logging.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_logging.py → tests/test_fs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""`pytest` tests for utils/logging.py"""
"""`pytest` tests for utils/fs.py"""

from benchcab.utils.logging import next_path
from benchcab.utils.fs import next_path
from .common import MOCK_CWD


Expand Down
22 changes: 11 additions & 11 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ def test_run_build():
mock_modules = ["foo", "bar"]
(MOCK_CWD / internal.SRC_DIR / "trunk" / "offline" / ".tmp").mkdir(parents=True)

environment_vars = {
"NCDIR": f"{mock_netcdf_root}/lib/Intel",
"NCMOD": f"{mock_netcdf_root}/include/Intel",
"CFLAGS": "-O2 -fp-model precise",
"LDFLAGS": f"-L{mock_netcdf_root}/lib/Intel -O0",
"LD": "-lnetcdf -lnetcdff",
"FC": "ifort",
}

# This is required so that we can use the NETCDF_ROOT environment variable
# when running `make`, and `serial_cable` and `parallel_cable` scripts:
os.environ["NETCDF_ROOT"] = mock_netcdf_root
Expand Down Expand Up @@ -188,17 +197,8 @@ def test_run_build():
mock_subprocess = MockSubprocessWrapper()
repo = get_mock_repo(subprocess_handler=mock_subprocess)
repo.run_build(mock_modules)
assert all(
kv in mock_subprocess.env.items()
for kv in {
"NCDIR": f"{mock_netcdf_root}/lib/Intel",
"NCMOD": f"{mock_netcdf_root}/include/Intel",
"CFLAGS": "-O2 -fp-model precise",
"LDFLAGS": f"-L{mock_netcdf_root}/lib/Intel -O0",
"LD": "-lnetcdf -lnetcdff",
"FC": "ifort",
}.items()
)
for kv in environment_vars.items():
assert (kv in mock_subprocess.env.items())

# Success case: test non-verbose standard output
repo = get_mock_repo()
Expand Down

0 comments on commit de33ae0

Please sign in to comment.