From 03b4de042810448224ab8ece7793380b3138d4c7 Mon Sep 17 00:00:00 2001 From: adrianciu <adrian.ciu@codemart.ro> Date: Fri, 7 Feb 2025 18:50:52 +0200 Subject: [PATCH] esd-spack-installation: spack install method and additional tests --- esd/spack_manager/SpackManager.py | 29 ++++++++++--------- .../factory/SpackManagerBuildCache.py | 3 -- .../factory/SpackManagerScratch.py | 3 -- esd/tests/spack_from_scratch_test.py | 14 ++++++++- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/esd/spack_manager/SpackManager.py b/esd/spack_manager/SpackManager.py index d534b30a..65a21d6e 100644 --- a/esd/spack_manager/SpackManager.py +++ b/esd/spack_manager/SpackManager.py @@ -1,5 +1,6 @@ import os import re +import subprocess from abc import ABC, abstractmethod from pathlib import Path from tabnanny import check @@ -49,10 +50,6 @@ class SpackManager(ABC): def concretize_spack_env(self, force=True): pass - @abstractmethod - def install_spack_packages(self, jobs: 3, verbose=False, debug=False): - pass - def create_fetch_spack_environment(self): if self.env.git_path: git_clone_repo(self.env.env_name, self.env.path / self.env.env_name, self.env.git_path, logger=self.logger) @@ -168,18 +165,24 @@ class SpackManager(ABC): return None @check_spack_env - def install_packages(self, jobs: int, signed=True, fresh=False): + def install_packages(self, jobs: int, signed=True, fresh=False, debug=False): signed = '' if signed else '--no-check-signature' fresh = '--fresh' if fresh else '' + debug = '--debug' if debug else '' + install_result = run_command("bash", "-c", + f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack {debug} install -v {signed} --j {jobs} {fresh}', + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + logger=self.logger, + debug_msg=f"Installing spack packages for {self.env.env_name}", + exception_msg=f"Error installing spack packages for {self.env.env_name}", + exception=SpackInstallPackagesException) with open(str(Path(os.getcwd()).resolve() / ".generate_cache.log"), "w") as log_file: - run_command("bash", "-c", - f'source {self.spack_setup_script} && spack install --env {self.env.env_name} -v {signed} --j {jobs} {fresh}', - stdout=log_file, - capture_output=True, text=True, check=True, - logger=self.logger, - debug_msg=f"Installing spack packages for {self.env.env_name}", - exception_msg=f"Error installing spack packages for {self.env.env_name}", - exception=SpackInstallPackagesException) + log_file.write(install_result.stdout) + log_file.write("\n--- STDERR ---\n") + log_file.write(install_result.stderr) + return install_result def install_spack(self, spack_version="v0.22.0", spack_repo='https://github.com/spack/spack'): try: diff --git a/esd/spack_manager/factory/SpackManagerBuildCache.py b/esd/spack_manager/factory/SpackManagerBuildCache.py index 38151c6d..5b66f5c5 100644 --- a/esd/spack_manager/factory/SpackManagerBuildCache.py +++ b/esd/spack_manager/factory/SpackManagerBuildCache.py @@ -14,6 +14,3 @@ class SpackManagerBuildCache(SpackManager): def concretize_spack_env(self, force=True): pass - - def install_spack_packages(self, jobs: 3, verbose=False, debug=False): - pass diff --git a/esd/spack_manager/factory/SpackManagerScratch.py b/esd/spack_manager/factory/SpackManagerScratch.py index 2ec22705..3dbc25f6 100644 --- a/esd/spack_manager/factory/SpackManagerScratch.py +++ b/esd/spack_manager/factory/SpackManagerScratch.py @@ -23,6 +23,3 @@ class SpackManagerScratch(SpackManager): else: self.logger.debug('No spack environment defined') raise NoSpackEnvironmentException('No spack environment defined') - - def install_spack_packages(self, jobs: 3, verbose=False, debug=False): - pass diff --git a/esd/tests/spack_from_scratch_test.py b/esd/tests/spack_from_scratch_test.py index e5627409..782d6be8 100644 --- a/esd/tests/spack_from_scratch_test.py +++ b/esd/tests/spack_from_scratch_test.py @@ -12,7 +12,6 @@ test_spack_env_git = f'https://oauth2:{SPACK_ENV_ACCESS_TOKEN}@gitlab.ebrains.eu ebrains_spack_builds_git = 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git' - def test_spack_repo_exists_1(): spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH) assert spack_manager.spack_repo_exists('ebrains-spack-builds') == False @@ -159,3 +158,16 @@ def test_spack_from_scratch_concretize_7(tmp_path): spack_manager.concretize_spack_env(force=True) concretization_file_path = spack_manager.env_path / 'spack.lock' assert file_exists_and_not_empty(concretization_file_path) == True + + +def test_spack_from_scratch_install(tmp_path): + install_dir = tmp_path + env = SpackModel('test-spack-env', install_dir, test_spack_env_git) + repo = SpackModel('ebrains-spack-builds', install_dir, ebrains_spack_builds_git) + spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo]) + spack_manager.setup_spack_env() + spack_manager.concretize_spack_env(force=True) + concretization_file_path = spack_manager.env_path / 'spack.lock' + assert file_exists_and_not_empty(concretization_file_path) == True + install_result = spack_manager.install_packages(jobs=2, signed=False, fresh=True, debug=False) + assert install_result.returncode == 0 -- GitLab