From 276219ef826cb8b884d49695f4a3955da478d118 Mon Sep 17 00:00:00 2001 From: "C. Carouge" Date: Wed, 3 May 2023 11:13:56 +1000 Subject: [PATCH 1/2] Fix #63: MPI option only apply to default build --- benchcab/build_cable.py | 80 ++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/benchcab/build_cable.py b/benchcab/build_cable.py index 6ddedc94..d0e2bb96 100755 --- a/benchcab/build_cable.py +++ b/benchcab/build_cable.py @@ -139,46 +139,54 @@ def clean_if_needed(): if error == 1: raise ("Error cleaning previous compilation") - -def prepare_build( - build_script: str, branch_name: str, modules: list, root_dir=CWD -) -> Path: - """Prepare the build script depending if a build script was specified - in the configuration or not.""" - - if build_script: - build_script_path = Path(root_dir, SRC_DIR, branch_name, build_script) - - if not build_script_path.is_file(): - raise RuntimeError(f"The build script specified in the config.yaml file, {build_script_path}, " - "is not a valid file.", - ) - - else: - default_script_path = Path( - root_dir, SRC_DIR, branch_name, "offline", "build3.sh" - ) - if not default_script_path.is_file(): - raise RuntimeError(f"The default build script, {default_script_path}, could not be found." - "Do you need to specify a different build script with the " - "'build_script' option in config.yaml?", - ) - - build_script_path = adjust_build_script(default_script_path, modules) - - return build_script_path - - -def build_cable(config_build_script: str, branch_name: str, modules: list): - build_script_path = prepare_build(config_build_script, branch_name, modules) - - # move to the directory containing the build script and compile +def default_build(branch_name: str, modules: list, root_dir=CWD): + """Build CABLE using the default script. + This loads the modules specified in the configuration file.""" + + default_script_path = Path( + root_dir, SRC_DIR, branch_name, "offline", "build3.sh" + ) + if not default_script_path.is_file(): + raise RuntimeError(f"The default build script, {default_script_path}, could not be found." + "Do you need to specify a different build script with the " + "'build_script' option in config.yaml?", + ) + + build_script_path = adjust_build_script(default_script_path, modules) + os.chdir(build_script_path.parent) clean_if_needed() - # The following add the "mpi" option to the build if we want to compile with MPI cmd = f"{build_script_path} {'mpi'*MPI}" error = subprocess.call(cmd, shell=True) if error == 1: - raise ("Error building executable") + raise ("Error building executable with default script") + + os.chdir(CWD) + +def custom_build(config_build_script: str, branch_name: str, root_dir=CWD): + """Build CABLE with a script provided in configuration file""" + + build_script_path = Path(root_dir, SRC_DIR, branch_name, config_build_script) + + if not build_script_path.is_file(): + raise RuntimeError(f"The build script specified in the config.yaml file, {build_script_path}, " + "is not a valid file.") + + os.chdir(build_script_path.parent) + cmd = f"{build_script_path}" + error = subprocess.call(cmd, shell=True) + if error == 1: + raise ("Error building executable with custom script") os.chdir(CWD) + +def build_cable(config_build_script: str, branch_name: str, modules: list, root_dir=CWD): + + if config_build_script: + # Use provided script as is + custom_build(config_build_script, branch_name, root_dir) + + else: + # Use default script with provided module versions + default_build(branch_name, modules, root_dir) + From 4631a568a30ba9f5f9f24221b9b1b0ddebbbfc2d Mon Sep 17 00:00:00 2001 From: "C. Carouge" Date: Fri, 5 May 2023 09:56:07 +1000 Subject: [PATCH 2/2] Remove tests. --- tests/test_build.py | 91 --------------------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 tests/test_build.py diff --git a/tests/test_build.py b/tests/test_build.py deleted file mode 100644 index f59d4e61..00000000 --- a/tests/test_build.py +++ /dev/null @@ -1,91 +0,0 @@ -"""`pytest` tests for task.py""" - -from pathlib import Path - -import pytest - -from tests.common import TMP_DIR -from benchcab.build_cable import prepare_build - -branch_name = "real1" -compile_dir = "offline" - - -@pytest.fixture() -def mock_build(): - """Create a fake build directory""" - - # Setup: - build_path = Path(TMP_DIR, "src", branch_name, compile_dir) - build_path.mkdir(parents=True) - - yield build_path - - # Teardown - for fname in build_path.glob("*"): - if fname.is_file(): - fname.unlink() - else: - fname.rmdir() - build_path.rmdir() - -def mock_build_script(mock_build): - """Create fake default script for build in the build directory""" - - build_name = mock_build / "build3.sh" - - if not build_name.is_file(): - fname = open(build_name, "w") - fname.writelines(["module purge\n", "module load something\n"]) - fname.close() - -def test_prepare_build(mock_build): - - # Create default build script file - mock_build_script(mock_build) - - # Success case: uses a user-defined script - user_script = "/".join([compile_dir, "specified_build.sh"]) - modules = [] - - target_path = mock_build.parent / user_script - target_path.touch() - assert ( - prepare_build(user_script, branch_name, modules, root_dir=TMP_DIR) - == target_path - ) - - # Success case: uses the script 'build3.ksh' without modifications if specified as input - # Test added to remind maintainers of the behaviour. - user_script = "/".join([compile_dir, "build3.sh"]) - modules = [] - - target_path = mock_build.parent / user_script - target_path.touch() - assert ( - prepare_build(user_script, branch_name, modules, root_dir=TMP_DIR) - == target_path - ) - - # Success case: Default behaviour. Returns the modified build script. - user_script = "" - modules = [] - - target_path = mock_build / "my_build.ksh" - assert ( - prepare_build(user_script, branch_name, modules, root_dir=TMP_DIR) - == target_path - ) - - # Failure case: specified file does not exist - user_script = "wrong_script.sh" - with pytest.raises(RuntimeError): - prepare_build(user_script, branch_name, modules, root_dir=TMP_DIR) - - # Remove default file!!!! - (mock_build / "build3.sh").unlink(missing_ok=True) - - # Failure case: default file is not found - user_script=[] - with pytest.raises(RuntimeError): - prepare_build(user_script, branch_name, modules, root_dir=TMP_DIR)