diff --git a/mpsd-software-environment.py b/mpsd-software-environment.py index 10be2df4b08cdced54c016040df7b77385025388..3f6b33feb0e17659c36fd8caa61e6885d37e918d 100755 --- a/mpsd-software-environment.py +++ b/mpsd-software-environment.py @@ -12,7 +12,6 @@ import time from pathlib import Path from typing import List, Tuple, Union import re -from string import Template # If 'rich' is available ("pip install rich" or "apt-get install python3-rich"), # then use coloured output, otherwise proceed as before @@ -38,14 +37,7 @@ call_date_iso = ( config_vars = { # kept inside the mpsd_release folder "cmd_log_file": "script_execution_summary.log", - # Placeholder installer log file name, placed at mpsd_microarch/logs - "installer_log_template": Template( - "${mpsd_release}_${mpsd_microarch}_${date}_APEX_${action}.log" - ), - # Placeholder build log file name, placed at mpsd_microarch/logs - "build_log_template": Template( - "${mpsd_release}_${mpsd_microarch}_${date}_BUILD_${toolchain}_${action}.log" - ), + # Metadata tags "metadata_tag_open": "!<meta>", "metadata_tag_close": "</meta>!", "spack_environments_repo": "https://gitlab.gwdg.de/mpsd-cs/spack-environments.git", @@ -58,17 +50,17 @@ def create_log_file_names( action: str, date: str = call_date_iso, toolchain: str = None, -) -> Tuple[str, str]: +) -> Union[str, None]: """Create log file names. - This function creates the log file names for the installer and + This function creates the log file names for either the installer or the build log files. - The installer log file is created - from the output of this script except - the toolchain build log that is handled by the spack_setup.sh script. - The build log `tee`'s the output of the spack_setup.sh script to the log file. - The log file names are created using the - template strings defined in config_vars. + + If a toolchain is given, then the build log file name is created. + if no toolchain is given, then the installer log file name is created. + The installer log file hosts the logs of the installer script, while + the build log file hosts the logs of the build process as generated by the + spack_setup.sh script. Parameters ---------- @@ -83,27 +75,29 @@ def create_log_file_names( only install and remove are valid for build log file. toolchain : str toolchain name (only for build log file) - returns : tuple - tuple containing installer_log_file, build_log_file, file names + + Returns + ------- + str or None + log file name + installer_log_file_name or build_log_file_name depending on the + parameters given. + If the action is not one that changes the files on disk ( info only actions) + then None is returned. """ - installer_log_file = config_vars["installer_log_template"].substitute( - mpsd_release=mpsd_release, - mpsd_microarch=mpsd_microarch, - date=date, - action=action, - ) - if toolchain and action in ["install", "remove"]: - build_log_file = config_vars["build_log_template"].substitute( - mpsd_release=mpsd_release, - mpsd_microarch=mpsd_microarch, - date=date, - action=action, - toolchain=toolchain, - ) + if toolchain: + # if toolchain is given, then we build the build_log_file_name + if action in ["install", "remove"]: + log_file_name = ( + f"{mpsd_release}_{mpsd_microarch}_{date}_BUILD_{toolchain}_{action}.log" + ) + else: + return None else: - build_log_file = None - logging.warning("Incorrect action or toolchain name.") - return installer_log_file, build_log_file + # if toolchain is not given, then we build the installer_log_file_name + log_file_name = f"{mpsd_release}_{mpsd_microarch}_{date}_APEX_{action}.log" + + return log_file_name def log_metadata(key: str, value: str) -> None: @@ -160,7 +154,7 @@ def get_installer_log_file_path(mpsd_release: str, cmd: str, script_dir: str) -> mpsd_microarch = get_native_microarchitecture() # parse logging first # decide the log_file_name - installer_log_name, build_log_name = create_log_file_names( + installer_log_name = create_log_file_names( mpsd_release=mpsd_release, mpsd_microarch=mpsd_microarch, action=cmd ) log_folder = script_dir / mpsd_release / "logs" @@ -786,7 +780,7 @@ def install_environment( os.mkdir("logs") for toolchain in toolchains: # Set the install log file name from create_log_file_names - _, build_log_file_name = create_log_file_names( + build_log_file_name = create_log_file_names( mpsd_release, mpsd_microarch, "install", toolchain=toolchain ) build_log_folder = release_base_dir / "logs" diff --git a/tests.py b/tests.py index 01be0fd7124654763f17c7ebbd9cd6c3149283d2..c284591902e312c7b7d662a9da542894443e0681 100644 --- a/tests.py +++ b/tests.py @@ -383,51 +383,45 @@ def test_metadata_logging(tmp_path): assert len(read_dict) == len(keys) -@pytest.mark.skip( - reason="This function is redundant in the next version of the script." -) def test_create_log_file_names(): """Test that the log file names are created correctly.""" - pass create_log_file_names = mod.create_log_file_names mpsd_release = "dev-23a" mpsd_microarch = "sandybridge" date = datetime.datetime.now().replace(microsecond=0).isoformat() action = "install" toolchain = "foss2021a" - # test for correct action and toolchain - installer_log_file, build_log_file = create_log_file_names( + # test build_log_file_name generation + build_log_file_name = create_log_file_names( mpsd_microarch=mpsd_microarch, mpsd_release=mpsd_release, date=date, action=action, toolchain=toolchain, ) - assert installer_log_file == f"{mpsd_release}_{mpsd_microarch}_{date}_{action}.log" assert ( - build_log_file - == f"{mpsd_release}_{mpsd_microarch}_{date}_{toolchain}_{action}.log" + build_log_file_name + == f"{mpsd_release}_{mpsd_microarch}_{date}_BUILD_{toolchain}_{action}.log" ) - # test no build log file for incorrect action - installer_log_file, build_log_file = create_log_file_names( + installer_log_file_name = create_log_file_names( mpsd_microarch=mpsd_microarch, mpsd_release=mpsd_release, date=date, - action="status", - toolchain=toolchain, + action=action, ) - assert installer_log_file == f"{mpsd_release}_{mpsd_microarch}_{date}_status.log" - assert build_log_file is None - # test no build log file for incorrect toolchain - installer_log_file, build_log_file = create_log_file_names( + assert ( + installer_log_file_name + == f"{mpsd_release}_{mpsd_microarch}_{date}_APEX_{action}.log" + ) + # test no build log file for incorrect action + build_log_file_name = create_log_file_names( mpsd_microarch=mpsd_microarch, mpsd_release=mpsd_release, date=date, - action="reinstall", - toolchain=None, + action="status", + toolchain=toolchain, ) - assert installer_log_file == f"{mpsd_release}_{mpsd_microarch}_{date}_reinstall.log" - assert build_log_file is None + assert build_log_file_name is None def test_interface(tmp_path):