diff --git a/esd/spack_manager/SpackManager.py b/esd/spack_manager/SpackManager.py index d534b30a085ce41ed27a19908b71f47f9097b699..65a21d6e224cb7b3c0d4150de1fefa7adbe97b4f 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 38151c6d0741d16c7a06e64f4a2ee5bc9db2e376..5b66f5c51bb6bb3377749998c1e9377fdcd70f94 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 2ec22705240ab549ba59229a503f00be8091b02a..3dbc25f68e4bb64204b6901a356b6feaca054c9a 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 e56274096f3e597b82bacfc3d52538d267eaaed6..782d6be8f7504ad2116b761b15504290de0663e9 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