diff --git a/esd/error_handling/exceptions.py b/esd/error_handling/exceptions.py index 2acca54ea6decc66da1bd0cc827ea5b890b2780d..79f8051f5019992ab29baa74ffd7180985f7a108 100644 --- a/esd/error_handling/exceptions.py +++ b/esd/error_handling/exceptions.py @@ -11,3 +11,8 @@ class BashCommandException(SpackException): """ To be thrown when an invalid input is received. """ + +class NoSpackEnvironmentException(SpackException): + """ + To be thrown when an invalid input is received. + """ \ No newline at end of file diff --git a/esd/spack_manager/SpackManager.py b/esd/spack_manager/SpackManager.py index f7bb00b4e7ba348ba840adac81065005f65eaab7..340b1b9580b4ec4c1829b149a1276f314e3dfbfc 100644 --- a/esd/spack_manager/SpackManager.py +++ b/esd/spack_manager/SpackManager.py @@ -2,7 +2,7 @@ import os from abc import ABC, abstractmethod from pathlib import Path -from esd.error_handling.exceptions import BashCommandException +from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException from esd.logger.logger_builder import get_logger from esd.model.SpackModel import SpackModel from esd.utils.utils import run_command, git_clone_repo @@ -90,7 +90,7 @@ class SpackManager(ABC): else: self.logger.debug(f'Spack repository {repo.env_name} already added') - def spack_repo_exists(self, repo_name: str) -> bool: + def spack_repo_exists(self, repo_name: str) -> bool | None: """Check if the given Spack repository exists.""" if self.env is None: result = run_command("bash", "-c", @@ -101,11 +101,15 @@ class SpackManager(ABC): if result is None: return False else: - result = run_command("bash", "-c", + if self.spack_env_exists(): + result = run_command("bash", "-c", f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo list', check=True, capture_output=True, text=True, logger=self.logger, debug_msg=f'Checking if repository {repo_name} was added') + else: + self.logger.debug('No spack environment defined') + raise NoSpackEnvironmentException('No spack environment defined') if result is None: return False return any(line.strip().endswith(repo_name) for line in result.stdout.splitlines()) @@ -122,12 +126,16 @@ class SpackManager(ABC): def add_spack_repo(self, repo_path: Path, repo_name: str): """Add the Spack repository if it does not exist.""" - run_command("bash", "-c", - f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo add {repo_path}/{repo_name}', - check=True, logger=self.logger, - debug_msg=f"Added {repo_name} to spack environment {self.env.env_name}", - exception_msg=f"Failed to add {repo_name} to spack environment {self.env.env_name}", - exception=BashCommandException) + if self.spack_env_exists(): + run_command("bash", "-c", + f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo add {repo_path}/{repo_name}', + check=True, logger=self.logger, + debug_msg=f"Added {repo_name} to spack environment {self.env.env_name}", + exception_msg=f"Failed to add {repo_name} to spack environment {self.env.env_name}", + exception=BashCommandException) + else: + self.logger.debug('No spack environment defined') + raise NoSpackEnvironmentException('No spack environment defined') def get_spack_installed_version(self): spack_version = run_command("bash", "-c", f'source {self.spack_setup_script} && spack --version',