From 508281df31dc3c18f2e007f4dd505160342a681a Mon Sep 17 00:00:00 2001 From: Benedetta Delfino Date: Thu, 14 Mar 2024 11:38:13 +0000 Subject: refactor: Refactored functions for Corstone installations Signed-off-by: Benedetta Delfino Change-Id: I51f845e9fc9f0a699a239c63c268565487be2aeb --- src/mlia/backend/corstone/__init__.py | 8 +- src/mlia/backend/corstone/install.py | 208 +++++++++++++++------------------ tests/test_backend_corstone_install.py | 13 +-- 3 files changed, 104 insertions(+), 125 deletions(-) diff --git a/src/mlia/backend/corstone/__init__.py b/src/mlia/backend/corstone/__init__.py index c6c2227..0911483 100644 --- a/src/mlia/backend/corstone/__init__.py +++ b/src/mlia/backend/corstone/__init__.py @@ -4,15 +4,15 @@ from mlia.backend.config import BackendConfiguration from mlia.backend.config import BackendType from mlia.backend.config import System -from mlia.backend.corstone.install import get_corstone_300_installation -from mlia.backend.corstone.install import get_corstone_310_installation +from mlia.backend.corstone.install import get_corstone_installation from mlia.backend.registry import registry from mlia.core.common import AdviceCategory # List of mutually exclusive Corstone backends ordered by priority + CORSTONE_PRIORITY = { - "Corstone-310": get_corstone_310_installation(), - "Corstone-300": get_corstone_300_installation(), + "Corstone-310": get_corstone_installation(corstone_name="corstone-310"), + "Corstone-300": get_corstone_installation(corstone_name="corstone-300"), } diff --git a/src/mlia/backend/corstone/install.py b/src/mlia/backend/corstone/install.py index ae0dd4e..5f11d5b 100644 --- a/src/mlia/backend/corstone/install.py +++ b/src/mlia/backend/corstone/install.py @@ -9,6 +9,7 @@ check. from __future__ import annotations import logging +import re import subprocess # nosec from pathlib import Path @@ -24,6 +25,76 @@ from mlia.utils.filesystem import working_directory logger = logging.getLogger(__name__) +ARM_ECOSYSTEM_FVP_URL = ( + "https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/" +) + + +class CorstoneFVP: + """Corstone FVP.""" + + def __init__( + self, archive: str, sha256_hash: str, fvp_expected_files: list + ) -> None: + """Initialize Corstone FVP.""" + self.archive: str = archive + self.sha256_hash: str = sha256_hash + self.fvp_expected_files: list = fvp_expected_files + + def get_fvp_version(self) -> str: + """Get fvp version from archive name.""" + version_pattern = r"\d+\.\d+_\d+" + match_result = re.search(version_pattern, self.archive) + if match_result: + return match_result.group() + + raise RuntimeError("Couldn't find Corstone FVP version.") + + def get_vht_expected_files(self) -> list: + """Get expected files in vht.""" + return ["VHT" + fvp.split("/")[-1][3:] for fvp in self.fvp_expected_files] + + +# pylint: disable=line-too-long +CORSTONE_FVPS: dict[str, dict[str, CorstoneFVP]] = { + "corstone-300": { + "x86": CorstoneFVP( + archive="Corstone-300/FVP_Corstone_SSE-300_11.24_13_Linux64.tgz", + sha256_hash="6ea4096ecf8a8c06d6e76e21cae494f0c7139374cb33f6bc3964d189b84539a9", + fvp_expected_files=[ + "models/Linux64_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U55", + "models/Linux64_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U65", + ], + ), + "aarch64": CorstoneFVP( + archive="Corstone-300/FVP_Corstone_SSE-300_11.24_13_Linux64_armv8l.tgz", + sha256_hash="9b43da6a688220c707cd1801baf9cf4f5fb37d6dc77587b9071347411a64fd56", + fvp_expected_files=[ + "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U55", + "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U65", + ], + ), + }, + "corstone-310": { + "x86": CorstoneFVP( + archive="Corstone-310/FVP_Corstone_SSE-310_11.24_13_Linux64.tgz", + sha256_hash="616ecc0e82067fe0684790cf99638b3496f9ead11051a58d766e8258e766c556", + fvp_expected_files=[ + "models/Linux64_GCC-9.3/FVP_Corstone_SSE-310", + "models/Linux64_GCC-9.3/FVP_Corstone_SSE-310_Ethos-U65", + ], + ), + "aarch64": CorstoneFVP( + archive="Corstone-310/FVP_Corstone_SSE-310_11.24_13_Linux64_armv8l.tgz", + sha256_hash="61be18564a7d70c8eb73736e433a65cc16ae4b59f9b028ae86d258e2c28af526", + fvp_expected_files=[ + "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-310", + "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-310_Ethos-U65", + ], + ), + }, +} + class CorstoneInstaller: """Helper class that wraps Corstone installation logic.""" @@ -73,113 +144,33 @@ class CorstoneInstaller: return dist_dir / install_dir -def get_corstone_300_installation() -> Installation: - """Get Corstone-300 installation.""" - corstone_name = "corstone-300" +def get_corstone_installation(corstone_name: str) -> Installation: + """Get Corstone installation.""" if System.CURRENT == System.LINUX_AARCH64: - url = ( - "https://developer.arm.com/-/media/Arm%20Developer%20Community/" - "Downloads/OSS/FVP/Corstone-300/" - "FVP_Corstone_SSE-300_11.24_13_Linux64_armv8l.tgz" - ) - - filename = "FVP_Corstone_SSE-300_11.24_13_Linux64_armv8l.tgz" - version = "11.24_13" - sha256_hash = "9b43da6a688220c707cd1801baf9cf4f5fb37d6dc77587b9071347411a64fd56" - expected_files = [ - "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U55", - "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U65", - ] - backend_subfolder = "models/Linux64_armv8l_GCC-9.3" - - else: - url = ( - "https://developer.arm.com/-/media/Arm%20Developer%20Community" - "/Downloads/OSS/FVP/Corstone-300/" - "FVP_Corstone_SSE-300_11.24_13_Linux64.tgz" - ) - filename = "FVP_Corstone_SSE-300_11.24_13_Linux64.tgz" - version = "11.24_13" - sha256_hash = "6ea4096ecf8a8c06d6e76e21cae494f0c7139374cb33f6bc3964d189b84539a9" - expected_files = [ - "models/Linux64_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U55", - "models/Linux64_GCC-9.3/FVP_Corstone_SSE-300_Ethos-U65", - ] - backend_subfolder = "models/Linux64_GCC-9.3" - - corstone_300 = BackendInstallation( - name=corstone_name, - description="Corstone-300 FVP", - fvp_dir_name=corstone_name.replace("-", "_"), - download_artifact=DownloadArtifact( - name="Corstone-300 FVP", - url=url, - filename=filename, - version=version, - sha256_hash=sha256_hash, - ), - supported_platforms=["Linux"], - path_checker=CompoundPathChecker( - PackagePathChecker( - expected_files=expected_files, - backend_subfolder=backend_subfolder, - settings={"profile": "default"}, - ), - StaticPathChecker( - static_backend_path=Path("/opt/VHT"), - expected_files=[ - "VHT_Corstone_SSE-300_Ethos-U55", - "VHT_Corstone_SSE-300_Ethos-U65", - ], - copy_source=False, - settings={"profile": "AVH"}, - ), - ), - backend_installer=CorstoneInstaller(name=corstone_name), - ) - - return corstone_300 + arch = "aarch64" - -def get_corstone_310_installation() -> Installation: - """Get Corstone-310 installation.""" - corstone_name = "corstone-310" - if System.CURRENT == System.LINUX_AARCH64: - url = ( - "https://developer.arm.com/-/media/Arm%20Developer%20Community" - "/Downloads/OSS/FVP/Corstone-310/" - "FVP_Corstone_SSE-310_11.24_13_Linux64_armv8l.tgz" - ) - filename = "FVP_Corstone_SSE-310_11.24_13_Linux64_armv8l.tgz" - version = "11.24_13" - sha256_hash = "61be18564a7d70c8eb73736e433a65cc16ae4b59f9b028ae86d258e2c28af526" - expected_files = [ - "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-310", - "models/Linux64_armv8l_GCC-9.3/FVP_Corstone_SSE-310_Ethos-U65", - ] - backend_subfolder = "models/Linux64_armv8l_GCC-9.3" + elif System.CURRENT == System.LINUX_AMD64: + arch = "x86" else: - url = ( - "https://developer.arm.com/-/media/Arm%20Developer%20Community" - "/Downloads/OSS/FVP/Corstone-310/" - "FVP_Corstone_SSE-310_11.24_13_Linux64.tgz" - ) - filename = "FVP_Corstone_SSE-310_11.24_13_Linux64.tgz" - version = "11.24_13" - sha256_hash = "616ecc0e82067fe0684790cf99638b3496f9ead11051a58d766e8258e766c556" - expected_files = [ - "models/Linux64_GCC-9.3/FVP_Corstone_SSE-310", - "models/Linux64_GCC-9.3/FVP_Corstone_SSE-310_Ethos-U65", - ] - backend_subfolder = "models/Linux64_GCC-9.3" - - corstone_310 = BackendInstallation( + raise RuntimeError(f"'{corstone_name}' is not compatible with this platform") + + corstone_fvp = CORSTONE_FVPS[corstone_name][arch] + archive = corstone_fvp.archive + sha256_hash = corstone_fvp.sha256_hash + url = ARM_ECOSYSTEM_FVP_URL + archive + filename = archive.split("/")[-1] + version = corstone_fvp.get_fvp_version() + expected_files_fvp = corstone_fvp.fvp_expected_files + expected_files_vht = corstone_fvp.get_vht_expected_files() + backend_subfolder = expected_files_fvp[0].split("FVP")[0] + + corstone_install = BackendInstallation( name=corstone_name, - description="Corstone-310 FVP", + description=corstone_name.capitalize() + " FVP", fvp_dir_name=corstone_name.replace("-", "_"), download_artifact=DownloadArtifact( - name="Corstone-310 FVP", + name=corstone_name.capitalize() + " FVP", url=url, filename=filename, version=version, @@ -188,16 +179,13 @@ def get_corstone_310_installation() -> Installation: supported_platforms=["Linux"], path_checker=CompoundPathChecker( PackagePathChecker( - expected_files=expected_files, + expected_files=expected_files_fvp, backend_subfolder=backend_subfolder, settings={"profile": "default"}, ), StaticPathChecker( static_backend_path=Path("/opt/VHT"), - expected_files=[ - "VHT_Corstone_SSE-310_Ethos-U55", - "VHT_Corstone_SSE-310_Ethos-U65", - ], + expected_files=expected_files_vht, copy_source=False, settings={"profile": "AVH"}, ), @@ -205,12 +193,4 @@ def get_corstone_310_installation() -> Installation: backend_installer=CorstoneInstaller(name=corstone_name), ) - return corstone_310 - - -def get_corstone_installations() -> list[Installation]: - """Get Corstone installations.""" - return [ - get_corstone_300_installation(), - get_corstone_310_installation(), - ] + return corstone_install diff --git a/tests/test_backend_corstone_install.py b/tests/test_backend_corstone_install.py index 496e378..326118d 100644 --- a/tests/test_backend_corstone_install.py +++ b/tests/test_backend_corstone_install.py @@ -11,16 +11,15 @@ from unittest.mock import MagicMock import pytest from mlia.backend.corstone.install import CorstoneInstaller -from mlia.backend.corstone.install import get_corstone_installations +from mlia.backend.corstone.install import get_corstone_installation from mlia.backend.install import Installation -def test_get_corstone_installations() -> None: - """Test function get_corstone_installations.""" - installations = get_corstone_installations() - assert len(installations) == 2 - - assert all(isinstance(item, Installation) for item in installations) +@pytest.mark.parametrize("corstone_name", ["corstone-300", "corstone-310"]) +def test_get_corstone_installation(corstone_name: str) -> None: + """Test Corstone installation""" + installation = get_corstone_installation(corstone_name) + assert isinstance(installation, Installation) @pytest.mark.parametrize( -- cgit v1.2.1