<commit_before>import logging
import os
import shutil
import subprocess
import sys

from builtins import input
from concreate.builder import Builder
from concreate.errors import ConcreateError

logger = logging.getLogger('concreate')


class OSBSBuilder(Builder):
    """Class representing OSBS builder."""

    def check_prerequisities(self):
        try:
            subprocess.check_output(
                ['rhpkg', 'help'], stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as ex:
            raise ConcreateError("OSBS build engine needs 'rhpkg' tools installed, error: %s"
                                 % ex.output)
        except Exception as ex:
            raise ConcreateError(
                "OSBS build engine needs 'rhpkg' tools installed!", ex)

    def prepare(self, descriptor):
        """Prepares dist-git repository for OSBS build."""

        repository_key = descriptor.get('osbs', {}).get('repository', {})
        repository = repository_key.get('name')
        branch = repository_key.get('branch')

        if not (repository and branch):
            raise ConcreateError(
                "OSBS builder needs repostiory and branch provided!")

        self.dist_git_dir = os.path.join(os.path.expanduser('~/.concreate.d'),
                                         'osbs',
                                         repository)
        if not os.path.exists(os.path.dirname(self.dist_git_dir)):
            os.makedirs(os.path.dirname(self.dist_git_dir))

        self.dist_git = Git(self.dist_git_dir,
                            self.target,
                            repository,
                            branch)

        self.dist_git.prepare()
        self.dist_git.clean()

        self.artifacts = [a['name'] for a in descriptor.get('artifacts', [])]

        self.update_osbs_image_source()

    def update_osbs_image_source(self):
        with Chdir(os.path.join(self.target, 'image')):
            for obj in ["repos", "modules"]:
                if os.path.exists(obj):
                    shutil.copytree(obj, os.path.join(self.dist_git_dir, obj))
            shutil.copy("Dockerfile", os.path.join(
                self.dist_git_dir, "Dockerfile"))

        # Copy also every artifact
        for artifact in self.artifacts:
            shutil.copy(os.path.join(self.target,
                                     'image',
                                     artifact),
                        os.path.join(self.dist_git_dir,
                                     artifact))

    def update_lookaside_cache(self):
        logger.info("Updating lookaside cache...")
        cmd = ["rhpkg", "new-sources"] + self.artifacts
        logger.debug("Executing '%s'" % cmd)
        with Chdir(self.dist_git_dir):
            subprocess.check_output(cmd)
        logger.info("Update finished.")

    def build(self, build_args):
        build_cmd = ["rhpkg", "container-build"]

        if not build_args.build_osbs_release:
            build_cmd.append("--scratch")

        with Chdir(self.dist_git_dir):
            self.dist_git.add()
            self.update_lookaside_cache()

            if self.dist_git.stage_modified():
                self.dist_git.commit()
                self.dist_git.push()
            else:
                logger.info("No changes made to the code, committing skipped")<commit_msg>Fixed osbs builder, to not die if there are no artifacts specified.<commit_after>import logging
import os
import shutil
import subprocess
import sys

from builtins import input
from concreate.builder import Builder
from concreate.errors import ConcreateError

logger = logging.getLogger('concreate')


class OSBSBuilder(Builder):
    """Class representing OSBS builder."""

    def check_prerequisities(self):
        try:
            subprocess.check_output(
                ['rhpkg', 'help'], stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as ex:
            raise ConcreateError("OSBS build engine needs 'rhpkg' tools installed, error: %s"
                                 % ex.output)
        except Exception as ex:
            raise ConcreateError(
                "OSBS build engine needs 'rhpkg' tools installed!", ex)

    def prepare(self, descriptor):
        """Prepares dist-git repository for OSBS build."""

        repository_key = descriptor.get('osbs', {}).get('repository', {})
        repository = repository_key.get('name')
        branch = repository_key.get('branch')

        if not (repository and branch):
            raise ConcreateError(
                "OSBS builder needs repostiory and branch provided!")

        self.dist_git_dir = os.path.join(os.path.expanduser('~/.concreate.d'),
                                         'osbs',
                                         repository)
        if not os.path.exists(os.path.dirname(self.dist_git_dir)):
            os.makedirs(os.path.dirname(self.dist_git_dir))

        self.dist_git = Git(self.dist_git_dir,
                            self.target,
                            repository,
                            branch)

        self.dist_git.prepare()
        self.dist_git.clean()

        self.artifacts = [a['name'] for a in descriptor.get('artifacts', [])]

        self.update_osbs_image_source()

    def update_osbs_image_source(self):
        with Chdir(os.path.join(self.target, 'image')):
            for obj in ["repos", "modules"]:
                if os.path.exists(obj):
                    shutil.copytree(obj, os.path.join(self.dist_git_dir, obj))
            shutil.copy("Dockerfile", os.path.join(
                self.dist_git_dir, "Dockerfile"))

        # Copy also every artifact
        for artifact in self.artifacts:
            shutil.copy(os.path.join(self.target,
                                     'image',
                                     artifact),
                        os.path.join(self.dist_git_dir,
                                     artifact))

    def update_lookaside_cache(self):
        logger.info("Updating lookaside cache...")
        if not self.artifacts:
            return
        cmd = ["rhpkg", "new-sources"] + self.artifacts
        logger.debug("Executing '%s'" % cmd)
        with Chdir(self.dist_git_dir):
            try:
                subprocess.check_output(cmd, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as ex:
                logger.error("Cannot run '%s', ouput: '%s'" % (cmd, ex.output))
                raise ConcreateError("Cannot update sources.")

        logger.info("Update finished.")

    def build(self, build_args):
        build_cmd = ["rhpkg", "container-build"]

        if not build_args.build_osbs_release:
            build_cmd.append("--scratch")

        with Chdir(self.dist_git_dir):
            self.dist_git.add()
            self.update_lookaside_cache()

            if self.dist_git.stage_modified():
                self.dist_git.commit()
                self.dist_git.push()
            else:
                logger.info("No changes made to the code, committing skipped")
{'fraction_non_alphanumeric': 0.10430486944248413, 'fraction_numerical': 0.0, 'mean_word_length': 1.811904761904762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2729542', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1866, 'n_words': 419}