diff --git a/mpsd-software-environment.py b/mpsd-software-environment.py index 3f6b33feb0e17659c36fd8caa61e6885d37e918d..d247f207aabc06d083f2e59f17ddf1b9a2bba0fb 100755 --- a/mpsd-software-environment.py +++ b/mpsd-software-environment.py @@ -147,7 +147,7 @@ def read_metadata_from_logfile(logfile: Union[str, Path]) -> dict: } -def get_installer_log_file_path(mpsd_release: str, cmd: str, script_dir: str) -> str: +def get_installer_log_file_path(mpsd_release: str, cmd: str, root_dir: str) -> str: """Get installer log file path.""" # Get machine configs os.environ.get("MPSD_OS", "UNKNOWN_OS") @@ -157,7 +157,7 @@ def get_installer_log_file_path(mpsd_release: str, cmd: str, script_dir: str) -> installer_log_name = create_log_file_names( mpsd_release=mpsd_release, mpsd_microarch=mpsd_microarch, action=cmd ) - log_folder = script_dir / mpsd_release / "logs" + log_folder = root_dir / mpsd_release / "logs" # if the log_folder dosent exist, dont log this message if # the command is a info-only command if cmd not in ["status", "available"]: @@ -421,7 +421,7 @@ def run(*args, counter=[0], **kwargs): def setup_log_cmd( - mpsd_release: str, script_dir: str, msg: str = None, **kwargs + mpsd_release: str, root_dir: str, msg: str = None, **kwargs ) -> None: """ Log the command used to build the toolchains. @@ -435,7 +435,7 @@ def setup_log_cmd( ---------- - mpsd_release : str The name of the release to install toolchains for. - - script_dir : str + - root_dir : str The path to the directory where the scripts are located. - msg : str, optional An optional message to log in the command log file. @@ -450,7 +450,7 @@ def setup_log_cmd( ------- - None """ - release_base_dir = script_dir / mpsd_release + release_base_dir = root_dir / mpsd_release # Write to the log file with the following format # -------------------------------------------------- @@ -472,7 +472,7 @@ def setup_log_cmd( # call statement: cmd_line = " ".join(sys.argv) # script branch and commit hash - with os_chdir(script_dir): + with os_chdir(root_dir): script_branch = ( run( ["git", "rev-parse", "--abbrev-ref", "HEAD"], @@ -507,7 +507,7 @@ def setup_log_cmd( ) -def create_dir_structure(mpsd_release: str, script_dir: Path) -> None: +def create_dir_structure(mpsd_release: str, root_dir: Path) -> None: """ Create the directory structure and clone spack environments repo. @@ -518,14 +518,14 @@ def create_dir_structure(mpsd_release: str, script_dir: Path) -> None: Parameters ---------- - mpsd_release: A string representing the MPSD release version. - - script_dir: A Path object representing the path to the scripts directory. + - root_dir: A Path object representing the path to the scripts directory. Returns ------- - None """ # Create the directory structure for the release - release_base_dir = script_dir / mpsd_release + release_base_dir = root_dir / mpsd_release release_base_dir.mkdir(parents=True, exist_ok=True) with os_chdir(release_base_dir): @@ -553,7 +553,7 @@ def create_dir_structure(mpsd_release: str, script_dir: Path) -> None: run(["git", "pull"], check=True) -def get_release_info(mpsd_release: str, script_dir: Path) -> Tuple[str, str, List[str]]: +def get_release_info(mpsd_release: str, root_dir: Path) -> Tuple[str, str, List[str]]: """ Get information about the specified release. @@ -564,7 +564,7 @@ def get_release_info(mpsd_release: str, script_dir: Path) -> Tuple[str, str, Lis ---------- mpsd_release : str The name of the release to get information for. - script_dir : pathlib.Path + root_dir : pathlib.Path The base directory where releases are stored. Returns @@ -582,7 +582,7 @@ def get_release_info(mpsd_release: str, script_dir: Path) -> Tuple[str, str, Lis If the release directory does not exist. Run `create_dir_structure()` first. """ # Get the info for release - release_base_dir = script_dir / mpsd_release + release_base_dir = root_dir / mpsd_release if not os.path.exists(release_base_dir): raise FileNotFoundError( "Release directory does not exist. Run create_dir_structure() first." @@ -608,7 +608,7 @@ def get_release_info(mpsd_release: str, script_dir: Path) -> Tuple[str, str, Lis return spe_branch, spe_commit_hash, available_toolchains -def prepare_environment(mpsd_release: str, script_dir: Path) -> List[str]: +def prepare_environment(mpsd_release: str, root_dir: Path) -> List[str]: """ Create the directory structure for the given MPSD release. @@ -622,7 +622,7 @@ def prepare_environment(mpsd_release: str, script_dir: Path) -> List[str]: ---------- mpsd_release : str The name of the MPSD release to prepare the environment for. - script_dir : pathlib.Path + root_dir : pathlib.Path The base directory to create the release folder and clone the spack-environments repository into. @@ -631,12 +631,12 @@ def prepare_environment(mpsd_release: str, script_dir: Path) -> List[str]: available_toolchains : list A list of available toolchains for the given MPSD release. """ - create_dir_structure(mpsd_release, script_dir) + create_dir_structure(mpsd_release, root_dir) spe_branch, spe_commit_hash, available_toolchains = get_release_info( - mpsd_release, script_dir + mpsd_release, root_dir ) setup_log_cmd( - mpsd_release, script_dir, spe_branch=spe_branch, spe_commit_hash=spe_commit_hash + mpsd_release, root_dir, spe_branch=spe_branch, spe_commit_hash=spe_commit_hash ) return available_toolchains @@ -699,7 +699,7 @@ def get_native_microarchitecture(): def install_environment( mpsd_release: str, toolchains: List[str], - script_dir: Path, + root_dir: Path, force_reinstall: bool = False, enable_build_cache: bool = False, ) -> None: @@ -715,7 +715,7 @@ def install_environment( toolchains : list of str A list of strings representing the toolchains to install (e.g., "foss2021a-mpi", "global_generic", "ALL"). - script_dir : pathlib.Path + root_dir : pathlib.Path A Path object representing the path to the directory where the release and toolchains will be installed. force_reinstall : bool, optional @@ -736,11 +736,11 @@ def install_environment( """ logging.info( f"Installing release {mpsd_release} with toolchains {toolchains} " - f"to {script_dir}" + f"to {root_dir}" ) # Set required variables - release_base_dir = script_dir / mpsd_release + release_base_dir = root_dir / mpsd_release mpsd_microarch = get_native_microarchitecture() toolchain_dir = release_base_dir / mpsd_microarch toolchain_dir.mkdir(parents=True, exist_ok=True) @@ -750,7 +750,7 @@ def install_environment( install_flags.append("-b") # run the prepare_environment function - available_toolchains = prepare_environment(mpsd_release, script_dir) + available_toolchains = prepare_environment(mpsd_release, root_dir) # Ensure that the requested toolchains are available in the release if toolchains == "ALL": toolchains = available_toolchains @@ -794,12 +794,12 @@ def install_environment( # log the command setup_log_cmd( mpsd_release, - script_dir, + root_dir, msg=f"installing {toolchain} and logging at {build_log_path}", ) setup_log_cmd( mpsd_release, - script_dir, + root_dir, msg=( f"CMD: bash {spack_setup_script} {' '.join(install_flags)} " f"{toolchain}" @@ -909,24 +909,24 @@ def main(): args = parser.parse_args() # target dir is the place where this script exists. the - script_dir = Path(os.path.dirname(os.path.realpath(__file__))) + root_dir = Path(os.path.dirname(os.path.realpath(__file__))) set_up_logging( args.loglevel, - get_installer_log_file_path(args.release, args.action, script_dir), + get_installer_log_file_path(args.release, args.action, root_dir), ) # Check the command and run related function if args.action == "remove": - remove_environment(args.release, args.toolchains, script_dir) + remove_environment(args.release, args.toolchains, root_dir) elif args.action == "start-new": - start_new_environment(args.from_release, args.to_release, script_dir) + start_new_environment(args.from_release, args.to_release, root_dir) elif args.action == "install": install_environment( - args.release, args.toolchains, script_dir, False, args.enable_build_cache + args.release, args.toolchains, root_dir, False, args.enable_build_cache ) elif args.action == "prepare": - prepare_environment(args.release, script_dir) + prepare_environment(args.release, root_dir) if __name__ == "__main__": diff --git a/tests.py b/tests.py index c284591902e312c7b7d662a9da542894443e0681..e7cee4ab24483e78a671bd51e750512e4879420a 100644 --- a/tests.py +++ b/tests.py @@ -112,23 +112,23 @@ def test_prepare_environment(tmp_path): prepare_env is run when cmd is not specified, we can test cmd='prepare' and cmd=None to check both cases """ - script_dir = tmp_path / "mpsd_opt" / "linux_debian_11" + root_dir = tmp_path / "mpsd_opt" / "linux_debian_11" spack_environments = "spack-environments" mpsd_release_to_test = "dev-23a" - release_base_dir = script_dir / mpsd_release_to_test + release_base_dir = root_dir / mpsd_release_to_test # check that the test directory does not exist - assert not script_dir.exists() + assert not root_dir.exists() # prepare_environment expects to be executed in git repository # (mpsd-software-environments). It queries the commit on which we are to # log that information. For this to work, we need to execute the command # within a directory tree that has a git repository at the same or high # level. Let's create one: - create_mock_git_repository(script_dir) + create_mock_git_repository(root_dir) # now call the function we want to test result = mod.prepare_environment( - mpsd_release=mpsd_release_to_test, script_dir=script_dir + mpsd_release=mpsd_release_to_test, root_dir=root_dir ) # check if the directory now is created @@ -155,7 +155,7 @@ def test_prepare_environment(tmp_path): # Expect an Exception when wrong mpsd_release is provided with pytest.raises(Exception): result = mod.prepare_environment( - mpsd_release="wrong-mpsd-release", script_dir=(script_dir) + mpsd_release="wrong-mpsd-release", root_dir=(root_dir) ) @@ -166,17 +166,17 @@ def test_setup_log_cmd(tmp_path): """ cmd_log_file = mod.config_vars["cmd_log_file"] - script_dir = tmp_path / "test_prepare_env" + root_dir = tmp_path / "test_prepare_env" mpsd_release_to_test = "dev-23a" - release_base_dir = script_dir / mpsd_release_to_test + release_base_dir = root_dir / mpsd_release_to_test if os.path.exists(release_base_dir / cmd_log_file): initial_bytes = os.path.getsize(cmd_log_file) else: initial_bytes = 0 # run the prepare_env functionality - create_mock_git_repository(target_directory=script_dir, create_directory=True) - mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir)) + create_mock_git_repository(target_directory=root_dir, create_directory=True) + mod.prepare_environment(mpsd_release=mpsd_release_to_test, root_dir=(root_dir)) # check that logs/install-software-environment.log is updated assert os.path.exists(release_base_dir / cmd_log_file) @@ -195,7 +195,7 @@ def test_install_environment_wrong_toolchain(tmp_path): mod.install_environment( mpsd_release="dev-23a", toolchains=["wrong-toolchain"], - script_dir=(tmp_path), + root_dir=(tmp_path), ) @@ -207,7 +207,7 @@ def test_install_environment_wrong_mpsd_release(tmp_path): mod.install_environment( mpsd_release="wrong-mpsd-release", toolchains=["foss2021a-mpi"], - script_dir=(tmp_path), + root_dir=(tmp_path), ) @@ -221,17 +221,17 @@ def test_install_environment_zlib(): # pytest -s # for this installation avoid tmp_path as # the length of the path becomes too long and spack complains - script_dir = Path("/tmp/test_global_generic") - if script_dir.exists(): - shutil.rmtree(script_dir) - script_dir.mkdir(exist_ok=True, parents=True) + root_dir = Path("/tmp/test_global_generic") + if root_dir.exists(): + shutil.rmtree(root_dir) + root_dir.mkdir(exist_ok=True, parents=True) mpsd_release_to_test = "dev-23a" toolchain_to_test = "global_generic" cmd_log_file = mod.config_vars["cmd_log_file"] mpsd_microarch = mod.get_native_microarchitecture() - release_base_dir = script_dir / mpsd_release_to_test - create_mock_git_repository(target_directory=script_dir, create_directory=False) - mod.prepare_environment(mpsd_release=mpsd_release_to_test, script_dir=(script_dir)) + release_base_dir = root_dir / mpsd_release_to_test + create_mock_git_repository(target_directory=root_dir, create_directory=False) + mod.prepare_environment(mpsd_release=mpsd_release_to_test, root_dir=(root_dir)) # Patch the spack environments to create a fake global_generic # create a test toolchain toolchain_src_dir = release_base_dir / "spack-environments" / "toolchains" @@ -273,12 +273,12 @@ def test_install_environment_zlib(): # install global_generic toolchain mod.set_up_logging( "WARNING", - mod.get_installer_log_file_path(mpsd_release_to_test, "install", script_dir), + mod.get_installer_log_file_path(mpsd_release_to_test, "install", root_dir), ) mod.install_environment( mpsd_release=mpsd_release_to_test, toolchains=[toolchain_to_test], - script_dir=script_dir, + root_dir=root_dir, enable_build_cache=False, ) # test that the build log is created correctly @@ -328,12 +328,12 @@ def test_install_environment_zlib(): importlib.reload(mod) mod.set_up_logging( "WARNING", - mod.get_installer_log_file_path(mpsd_release_to_test, "install", script_dir), + mod.get_installer_log_file_path(mpsd_release_to_test, "install", root_dir), ) mod.install_environment( mpsd_release=mpsd_release_to_test, toolchains=[toolchain_to_test], - script_dir=script_dir, + root_dir=root_dir, enable_build_cache=False, ) build_log = list(