diff --git a/module_build_service/builder/CoprModuleBuilder.py b/module_build_service/builder/CoprModuleBuilder.py index 77031352..a81273ee 100644 --- a/module_build_service/builder/CoprModuleBuilder.py +++ b/module_build_service/builder/CoprModuleBuilder.py @@ -38,7 +38,8 @@ import module_build_service.utils import module_build_service.scheduler import module_build_service.scheduler.consumer -from base import GenericBuilder, build_from_scm +from base import GenericBuilder +from utils import build_from_scm from KojiModuleBuilder import KojiModuleBuilder logging.basicConfig(level=logging.DEBUG) diff --git a/module_build_service/builder/MockModuleBuilder.py b/module_build_service/builder/MockModuleBuilder.py index 82ab67d5..bbc618d6 100644 --- a/module_build_service/builder/MockModuleBuilder.py +++ b/module_build_service/builder/MockModuleBuilder.py @@ -36,7 +36,8 @@ import module_build_service.utils import module_build_service.scheduler import module_build_service.scheduler.consumer -from base import GenericBuilder, _execute_cmd, build_from_scm +from base import GenericBuilder +from utils import _execute_cmd, build_from_scm from KojiModuleBuilder import KojiModuleBuilder logging.basicConfig(level=logging.DEBUG) diff --git a/module_build_service/builder/base.py b/module_build_service/builder/base.py index 252212ca..4407ba93 100644 --- a/module_build_service/builder/base.py +++ b/module_build_service/builder/base.py @@ -30,14 +30,9 @@ import six from abc import ABCMeta, abstractmethod -import logging -import os -import koji -import tempfile -import shutil -import subprocess +from requests.exceptions import ConnectionError -from module_build_service import conf, log, db +from module_build_service import conf, log from module_build_service import pdc import module_build_service.scm import module_build_service.utils @@ -45,10 +40,6 @@ import module_build_service.scheduler import module_build_service.scheduler.consumer -from requests.exceptions import ConnectionError - -logging.basicConfig(level=logging.DEBUG) - """ Example workflows - helps to see the difference in implementations Copr workflow: @@ -313,90 +304,3 @@ class GenericBuilder(six.with_metaclass(ABCMeta)): for component builds. """ raise NotImplementedError() - - - - - - -def build_from_scm(artifact_name, source, config, build_srpm, - data = None, stdout=None, stderr=None): - """ - Builds the artifact from the SCM based source. - - :param artifact_name: Name of the artifact. - :param source: SCM URL with artifact's sources (spec file). - :param config: Config instance. - :param build_srpm: Method to call to build the RPM from the generate SRPM. - :param data: Data to be passed to the build_srpm method. - :param stdout: Python file object to which the stdout of SRPM build - command is logged. - :param stderr: Python file object to which the stderr of SRPM build - command is logged. - """ - ret = (0, koji.BUILD_STATES["FAILED"], "Cannot create SRPM", None) - td = None - - try: - log.debug('Cloning source URL: %s' % source) - # Create temp dir and clone the repo there. - td = tempfile.mkdtemp() - scm = module_build_service.scm.SCM(source) - cod = scm.checkout(td) - - # Use configured command to create SRPM out of the SCM repo. - log.debug("Creating SRPM in %s" % cod) - _execute_cmd(config.mock_build_srpm_cmd.split(" "), - stdout=stdout, stderr=stderr, cwd=cod) - - # Find out the built SRPM and build it normally. - for f in os.listdir(cod): - if f.endswith(".src.rpm"): - log.info("Created SRPM %s" % f) - source = os.path.join(cod, f) - ret = build_srpm(artifact_name, source, data) - break - except Exception as e: - log.error("Error while generating SRPM for artifact %s: %s" % ( - artifact_name, str(e))) - ret = (0, koji.BUILD_STATES["FAILED"], "Cannot create SRPM %s" % str(e), None) - finally: - try: - if td is not None: - shutil.rmtree(td) - except Exception as e: - log.warning( - "Failed to remove temporary directory {!r}: {}".format( - td, str(e))) - - return ret - - -def _execute_cmd(args, stdout = None, stderr = None, cwd = None): - """ - Executes command defined by `args`. If `stdout` or `stderr` is set to - Python file object, the stderr/stdout output is redirecter to that file. - If `cwd` is set, current working directory is set accordingly for the - executed command. - - :param args: list defining the command to execute. - :param stdout: Python file object to redirect the stdout to. - :param stderr: Python file object to redirect the stderr to. - :param cwd: string defining the current working directory for command. - :raises RuntimeError: Raised when command exits with non-zero exit code. - """ - out_log_msg = "" - if stdout: - out_log_msg += ", stdout log: %s" % stdout.name - if stderr: - out_log_msg += ", stderr log: %s" % stderr.name - - log.info("Executing command: %s%s" % (args, out_log_msg)) - proc = subprocess.Popen(args, stdout=stdout, stderr=stderr, cwd=cwd) - proc.communicate() - - if proc.returncode != 0: - err_msg = "Command '%s' returned non-zero value %d%s" % (args, proc.returncode, out_log_msg) - raise RuntimeError(err_msg) - - diff --git a/module_build_service/builder/utils.py b/module_build_service/builder/utils.py new file mode 100644 index 00000000..c44987f6 --- /dev/null +++ b/module_build_service/builder/utils.py @@ -0,0 +1,92 @@ +import os +import koji +import tempfile +import shutil +import subprocess +import logging +import module_build_service +from module_build_service import log, scm + + +logging.basicConfig(level=logging.DEBUG) + + +def build_from_scm(artifact_name, source, config, build_srpm, + data = None, stdout=None, stderr=None): + """ + Builds the artifact from the SCM based source. + + :param artifact_name: Name of the artifact. + :param source: SCM URL with artifact's sources (spec file). + :param config: Config instance. + :param build_srpm: Method to call to build the RPM from the generate SRPM. + :param data: Data to be passed to the build_srpm method. + :param stdout: Python file object to which the stdout of SRPM build + command is logged. + :param stderr: Python file object to which the stderr of SRPM build + command is logged. + """ + ret = (0, koji.BUILD_STATES["FAILED"], "Cannot create SRPM", None) + td = None + + try: + log.debug('Cloning source URL: %s' % source) + # Create temp dir and clone the repo there. + td = tempfile.mkdtemp() + scm = module_build_service.scm.SCM(source) + cod = scm.checkout(td) + + # Use configured command to create SRPM out of the SCM repo. + log.debug("Creating SRPM in %s" % cod) + _execute_cmd(config.mock_build_srpm_cmd.split(" "), + stdout=stdout, stderr=stderr, cwd=cod) + + # Find out the built SRPM and build it normally. + for f in os.listdir(cod): + if f.endswith(".src.rpm"): + log.info("Created SRPM %s" % f) + source = os.path.join(cod, f) + ret = build_srpm(artifact_name, source, data) + break + except Exception as e: + log.error("Error while generating SRPM for artifact %s: %s" % ( + artifact_name, str(e))) + ret = (0, koji.BUILD_STATES["FAILED"], "Cannot create SRPM %s" % str(e), None) + finally: + try: + if td is not None: + shutil.rmtree(td) + except Exception as e: + log.warning( + "Failed to remove temporary directory {!r}: {}".format( + td, str(e))) + + return ret + + +def _execute_cmd(args, stdout = None, stderr = None, cwd = None): + """ + Executes command defined by `args`. If `stdout` or `stderr` is set to + Python file object, the stderr/stdout output is redirecter to that file. + If `cwd` is set, current working directory is set accordingly for the + executed command. + + :param args: list defining the command to execute. + :param stdout: Python file object to redirect the stdout to. + :param stderr: Python file object to redirect the stderr to. + :param cwd: string defining the current working directory for command. + :raises RuntimeError: Raised when command exits with non-zero exit code. + """ + out_log_msg = "" + if stdout: + out_log_msg += ", stdout log: %s" % stdout.name + if stderr: + out_log_msg += ", stderr log: %s" % stderr.name + + log.info("Executing command: %s%s" % (args, out_log_msg)) + proc = subprocess.Popen(args, stdout=stdout, stderr=stderr, cwd=cwd) + proc.communicate() + + if proc.returncode != 0: + err_msg = "Command '%s' returned non-zero value %d%s" % (args, proc.returncode, out_log_msg) + raise RuntimeError(err_msg)