Skip to content
Snippets Groups Projects
Commit 82b2f791 authored by Hans Fangohr's avatar Hans Fangohr
Browse files

Merge branch 'main' into more-robust-micro-architecture-detection

parents 56f9b1d4 efef6a6a
No related branches found
No related tags found
1 merge request!32if MPSD_MICROARCH is not defined, use archspec to determine
Pipeline #369945 passed
This commit is part of merge request !32. Comments created here will be created in the context of that merge request.
.vscode .vscode
**/*.pyc **/*.pyc
logs logs
dev-23a/ dev-23a/
\ No newline at end of file *.log
\ No newline at end of file
...@@ -10,7 +10,8 @@ import subprocess ...@@ -10,7 +10,8 @@ import subprocess
import sys import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import List, Tuple from typing import List, Tuple, Union
import re
# If 'rich' is available ("pip install rich" or "apt-get install python3-rich"), # If 'rich' is available ("pip install rich" or "apt-get install python3-rich"),
# then use coloured output, otherwise proceed as before # then use coloured output, otherwise proceed as before
...@@ -36,11 +37,60 @@ config_vars = { ...@@ -36,11 +37,60 @@ config_vars = {
"logs/mpsd_spack_ver_toolchains_" "logs/mpsd_spack_ver_toolchains_"
f"{datetime.datetime.now().replace(microsecond=0).isoformat()}.log" f"{datetime.datetime.now().replace(microsecond=0).isoformat()}.log"
), ),
"metadata_tag_open": "!<meta>",
"metadata_tag_close": "</meta>!",
# TODO: modify toolchains,mpsd_spack_ver when the variable is available # TODO: modify toolchains,mpsd_spack_ver when the variable is available
"spack_environments_repo": "https://gitlab.gwdg.de/mpsd-cs/spack-environments.git", "spack_environments_repo": "https://gitlab.gwdg.de/mpsd-cs/spack-environments.git",
} }
def log_metadata(key: str, value: str) -> None:
"""Log metadata to the log file.
This function logs metadata to the log file. The metadata is
enclosed in a tag, so that it can be easily found in the log file.
logging module is used to write the metadata to the log file.
Parameters
----------
key : str
key of the metadata
value : str
value of the metadata
returns : None
"""
logging.info(
f"{config_vars['metadata_tag_open']}{key}:{value}{config_vars['metadata_tag_close']}"
)
def read_metadata_from_logfile(logfile: Union[str, Path]) -> dict:
"""Read metadata from the log file.
This function reads metadata from the log file. The metadata is
enclosed in a tag, so that it can be easily found in the log file.
Parameters
----------
logfile : str or Path
log file name
returns : dict
dictionary containing the metadata
"""
with open(logfile, "r") as f:
log_text = f.read()
# check for all data that matches the regex
# metadata_tag_open {key}:{value} metadata_tag_close
# and return a dictionary with all the matches
return {
match.group(1): match.group(2)
for match in re.finditer(
f"{config_vars['metadata_tag_open']}(\w+):(\w+){config_vars['metadata_tag_close']}",
log_text,
)
}
def set_up_logging(loglevel="warning", filename=None): def set_up_logging(loglevel="warning", filename=None):
"""Set up logging. """Set up logging.
......
"""Tests for mpsd-software-environment.py.""" """Tests for mpsd-software-environment.py."""
import importlib import importlib
import logging
import os import os
import shutil import shutil
import subprocess import subprocess
from pathlib import Path from pathlib import Path
import logging
import pytest import pytest
mod = importlib.import_module("mpsd-software-environment") mod = importlib.import_module("mpsd-software-environment")
...@@ -315,6 +314,45 @@ def test_install_environment_zlib(): ...@@ -315,6 +314,45 @@ def test_install_environment_zlib():
assert "zlib" in lines assert "zlib" in lines
def test_metadata_logging(tmp_path):
"""Test that metadata is logged and read correctly."""
# Test that the metadata is logged correctly
filename = tmp_path / "test-metadata.log"
print(f"Writing to {filename}")
mod.set_up_logging(loglevel="debug", filename=filename)
# our test data
keys = ["important_key", "important_key2"]
values = ["important_value", "important_value2"]
expected_log_entries = []
for key, value in zip(keys, values):
mod.log_metadata(key, value)
open_tag = mod.config_vars["metadata_tag_open"]
close_tag = mod.config_vars["metadata_tag_close"]
expected_log = f"{open_tag}{key}:{value}{close_tag}"
expected_log_entries.append(expected_log)
logging.info(f"Add some other info (after adding {key=})")
logging.debug("Add some other info")
logging.warning("Add some other info")
# Check that relevant lines show up in the log file somewhere
with open(filename, "r") as f:
logfile_content = f.read()
for expected_log in expected_log_entries:
assert expected_log in logfile_content
# Test that the metadata is read correctly using our parser
read_dict = mod.read_metadata_from_logfile(tmp_path / "test-metadata.log")
# check all entries are in the file
for key, value in zip(keys, values):
read_dict[key] == value
# check no additional entries are there
assert len(read_dict) == len(keys)
def test_interface(tmp_path): def test_interface(tmp_path):
"""Test other things (not implemented yet).""" """Test other things (not implemented yet)."""
pass pass
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment