Source code for src.codecrepos.docker_builder

"""
Author: Ismael Seidel
Affiliation: Embedded Computing Lab (ECL), Federal University of Santa Catarina (UFSC)

Description: 
    This module provides Docker-based codec compilation capabilities, allowing
    codecs to be built inside isolated Docker containers for consistency and portability.
"""

import subprocess

DOCKER_BUILD_COMMAND_TEMPLATE = """
docker
run
--rm
-v {repository_path}:/tmp:z
{image_name} 
/bin/sh -c
"""

[docs] class DockerBuilder: """Builds codecs inside Docker containers."""
[docs] def __init__(self, image_name: str = "codecs-compilation") -> None: """Initializes DockerBuilder with the Docker image name. :param image_name: Name of the Docker image for compilation, defaults to "codecs-compilation" :type image_name: str :return: None :rtype: None """ self.image_name = image_name
[docs] def get_docker_build_command(self, repository_path: str, build_command: str) -> list: """Builds the Docker run command for executing the build inside the container. :param repository_path: Path to the repository to mount :type repository_path: str :param build_command: Shell command to run inside the container :type build_command: str :return: Command as list of arguments for subprocess :rtype: list """ command = DOCKER_BUILD_COMMAND_TEMPLATE.format( repository_path=repository_path, image_name=self.image_name ).split() command.append(f"{build_command}") return command
[docs] def get_gcc_version(self) -> str: """Gets the GCC version from the Docker container. :return: First line of gcc --version output :rtype: str """ command = DOCKER_BUILD_COMMAND_TEMPLATE.format( repository_path=".", image_name=self.image_name ).split() command.append("gcc --version") res = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) return res.stdout.split('\n')[0]