Skip to content
Snippets Groups Projects
Commit 9abbf5f4 authored by adrianciu's avatar adrianciu Committed by Adrian Ciu
Browse files

esd-spack-installation: additional methods

parent 36a42d69
No related branches found
No related tags found
2 merge requests!7Dedal Release,!5Methods for caching; CLI
...@@ -5,6 +5,7 @@ stages: ...@@ -5,6 +5,7 @@ stages:
variables: variables:
BUILD_ENV_DOCKER_IMAGE: docker-registry.ebrains.eu/esd/tmp:latest BUILD_ENV_DOCKER_IMAGE: docker-registry.ebrains.eu/esd/tmp:latest
build-wheel: build-wheel:
stage: build stage: build
tags: tags:
......
...@@ -14,13 +14,18 @@ class BashCommandException(SpackException): ...@@ -14,13 +14,18 @@ class BashCommandException(SpackException):
""" """
class NoSpackEnvironmentException(SpackException): class NoSpackEnvironmentException(BashCommandException):
""" """
To be thrown when an operation on a spack environment is executed without the environment being activated or existent To be thrown when an operation on a spack environment is executed without the environment being activated or existent
""" """
class SpackConcertizeException(SpackException): class SpackConcertizeException(BashCommandException):
""" """
To be thrown when the spack concretization step fails To be thrown when the spack concretization step fails
""" """
class SpackInstallPackagesException(BashCommandException):
"""
To be thrown when the spack fails to install spack packages
"""
import os import os
import re
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException, \
SpackInstallPackagesException
from esd.logger.logger_builder import get_logger from esd.logger.logger_builder import get_logger
from esd.model.SpackModel import SpackModel from esd.model.SpackModel import SpackModel
from esd.spack_manager.wrapper.spack_wrapper import no_spack_env
from esd.utils.utils import run_command, git_clone_repo from esd.utils.utils import run_command, git_clone_repo
...@@ -103,10 +106,10 @@ class SpackManager(ABC): ...@@ -103,10 +106,10 @@ class SpackManager(ABC):
else: else:
if self.spack_env_exists(): if self.spack_env_exists():
result = run_command("bash", "-c", result = run_command("bash", "-c",
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo list', f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo list',
check=True, check=True,
capture_output=True, text=True, logger=self.logger, capture_output=True, text=True, logger=self.logger,
debug_msg=f'Checking if repository {repo_name} was added') debug_msg=f'Checking if repository {repo_name} was added')
else: else:
self.logger.debug('No spack environment defined') self.logger.debug('No spack environment defined')
raise NoSpackEnvironmentException('No spack environment defined') raise NoSpackEnvironmentException('No spack environment defined')
...@@ -124,18 +127,35 @@ class SpackManager(ABC): ...@@ -124,18 +127,35 @@ class SpackManager(ABC):
return False return False
return True return True
@no_spack_env
def add_spack_repo(self, repo_path: Path, repo_name: str): def add_spack_repo(self, repo_path: Path, repo_name: str):
"""Add the Spack repository if it does not exist.""" """Add the Spack repository if it does not exist."""
if self.spack_env_exists(): run_command("bash", "-c",
run_command("bash", "-c", f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo add {repo_path}/{repo_name}',
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,
check=True, logger=self.logger, debug_msg=f"Added {repo_name} to spack environment {self.env.env_name}",
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_msg=f"Failed to add {repo_name} to spack environment {self.env.env_name}", exception=BashCommandException)
exception=BashCommandException)
else: @no_spack_env
self.logger.debug('No spack environment defined') def get_compiler_version(self):
raise NoSpackEnvironmentException('No spack environment defined') result = run_command("bash", "-c",
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack compiler list',
check=True, logger=self.logger,
capture_output=True, text=True,
debug_msg=f"Checking spack environment compiler version for {self.env.env_name}",
exception_msg=f"Failed to checking spack environment compiler version for {self.env.env_name}",
exception=BashCommandException)
# todo add error handling and tests
if result.stdout is None:
self.logger.debug('No gcc found for {self.env.env_name}')
return None
# Find the first occurrence of a GCC compiler using regex
match = re.search(r"gcc@([\d\.]+)", result.stdout)
gcc_version = match.group(1)
self.logger.debug(f'Found gcc for {self.env.env_name}: {gcc_version}')
return gcc_version
def get_spack_installed_version(self): def get_spack_installed_version(self):
spack_version = run_command("bash", "-c", f'source {self.spack_setup_script} && spack --version', spack_version = run_command("bash", "-c", f'source {self.spack_setup_script} && spack --version',
...@@ -147,6 +167,7 @@ class SpackManager(ABC): ...@@ -147,6 +167,7 @@ class SpackManager(ABC):
return spack_version.stdout.strip().split()[0] return spack_version.stdout.strip().split()[0]
return None return None
def install_spack(self, spack_version="v0.21.1", spack_repo='https://github.com/spack/spack'): def install_spack(self, spack_version="v0.21.1", spack_repo='https://github.com/spack/spack'):
try: try:
user = os.getlogin() user = os.getlogin()
......
import functools
from esd.error_handling.exceptions import NoSpackEnvironmentException
def no_spack_env(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if self.spack_env_exists():
return method(self, *args, **kwargs) # Call the method with 'self'
else:
self.logger.debug('No spack environment defined')
raise NoSpackEnvironmentException('No spack environment defined')
return wrapper
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment