<commit_before>"""Tasks for an SRPM build."""

# pylint: disable=W0614
import glob
import fnmatch
import logging
import os
import re
import shutil

# local
from osgbuild.constants import *
from osgbuild import fetch_sources
from osgbuild import utils
from osgbuild.error import *


log = logging.getLogger('osgbuild')
log.propagate = False


class SRPMBuild(object):
    """Tasks for an SPRM build and helper functions."""

    def __init__(self, package_dir, buildopts, mock_obj, koji_obj):
        self.package_dir = package_dir
        self.mock_obj = mock_obj
        self.koji_obj = koji_obj

        self.buildopts = buildopts

        self.abs_package_dir = os.path.abspath(self.package_dir)
        self.package_name = os.path.basename(self.abs_package_dir)
        if not re.match(r"\w+", self.package_name): # sanity check
            raise Error("Package directory '%s' gives invalid package name '%s'" %
                        (self.abs_package_dir, self.package_name))
        # Unless working_directory is '.', i.e. we want to put the wd's in
        # the package dir, get rid of any parent or current directory
        # components so doing "osg-build pre -w TEMP ../foobar" won't put stuff
        # in "/tmp/foobar"
        if (os.path.realpath(self.buildopts['working_directory']) !=
                os.path.realpath('.')):
            package_dir_no_parent = re.sub(r'^(\.\.?/)+', '', package_dir)
            self.working_subdir = (
                os.path.abspath(
                    os.path.join(
                        self.buildopts['working_directory'],
                        package_dir_no_parent)))
        else:
            self.working_subdir = (
                os.path.abspath(
                    os.path.join(
                        self.buildopts['working_directory'],
                        package_dir)))
        self.results_dir = os.path.join(self.working_subdir, WD_RESULTS)
        self.prebuild_dir = os.path.join(self.working_subdir, WD_PREBUILD)
        self.unpacked_dir = os.path.join(self.working_subdir, WD_UNPACKED)
        self.unpacked_tarball_dir = os.path.join(self.working_subdir, WD_UNPACKED_TARBALL)
        self.quilt_dir = os.path.join(self.working_subdir, WD_QUILT)
    # end of __init__()


    def maybe_autoclean(self):
        """Delete underscore directories if the autoclean option is set."""
        if self.buildopts['autoclean']:
            for udir in [self.results_dir,
                         self.prebuild_dir,
                         self.unpacked_dir,
                         self.unpacked_tarball_dir]:
                if os.path.exists(udir):
                    log.debug("autoclean removing " + udir)
                    shutil.rmtree(udir)


    def get_rpmbuild_defines(self, prebuild):
        """Get a list of --define arguments to pass to rpmbuild based on the
        working dir and the subdirectories specified in the WD_* constants.

        """
        rhel = self.buildopts.get('redhat_release', '5')
        defines = [
            "_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",
            "rhel " + rhel,
            "dist ." + self.buildopts.get('distro_tag', 'osg.el' + rhel),
            "osg 1",
        ]
        for dver in DVERS:
            if dver == rhel:
                defines.append("el%s 1" % dver)
            else:
                defines.append("el%s 0" % dver)
        if rhel == '5':
            defines += [
                "_source_filedigest_algorithm 1",
                "_binary_filedigest_algorithm 1",
                "_binary_payload w9.gzdio",
                "_source_payload w9.gzdio",
                "_default_patch_fuzz 2",
            ]

        if prebuild:
            defines += [
                "_topdir " + self.prebuild_dir,
                "_builddir " + "%{_topdir}",
                "_buildrootdir " + "%{_topdir}",
                "_rpmdir " + "%{_topdir}",
                "_sourcedir " + "%{_topdir}",
                "_specdir " + "%{_topdir}",
                "_srcrpmdir " + "%{_topdir}",
                "_tmppath " + "%{_topdir}"
            ]
        else:
            defines += [
                "_topdir " + self.results_dir,
                "_builddir " + "%{_topdir}/BUILD",
                "_buildrootdir " + "%{_topdir}/BUILDROOT",
                "_rpmdir " + "%{_topdir}",
                "_sourcedir " + "%{_topdir}",
                "_specdir " + "%{_topdir}",
                "_srcrpmdir " + "%{_topdir}",
                "_tmppath " + "%{_topdir}/tmp"
            ]

        return ['--define=' + d for d in defines]
    # end of get_rpmbuild_defines()


    def make_srpm(self, spec_fn):
        """Make an SRPM from a spec file. Raise OSGPrebuildError on failure"""
        cmd = (["rpmbuild", "-bs", "--nodeps"] +
               self.get_rpmbuild_defines(prebuild=True) +
               [spec_fn])
        err_msg_prefix = ("Error making SRPM from %s\n"
                          "Command used was: %s\n" %
                          (spec_fn, " ".join(cmd)))
        out, err = utils.sbacktick(cmd, nostrip=True, clocale=True, err2out=True)
        if err:
            log.error("Rpmbuild failed. Output follows: " + out)
            raise OSGPrebuildError(err_msg_prefix +
                                   "Rpmbuild return code %d" % err)

        match = re.search(r"(?ms)^Wrote: ([^\n]+.src.rpm)$", out)
        if match:
            srpm = match.group(1).strip()
            if os.path.isfile(srpm):
                return srpm
        raise OSGPrebuildError(err_msg_prefix +
                               "Unable to find resulting SRPM.")


    def prebuild_external_sources(self, destdir=None):
        """Collect sources and spec file in prebuild_dir. Return the path to<commit_msg>add log line with destination srpm path for osg-build prebuild<commit_after>"""Tasks for an SRPM build."""

# pylint: disable=W0614
import glob
import fnmatch
import logging
import os
import re
import shutil

# local
from osgbuild.constants import *
from osgbuild import fetch_sources
from osgbuild import utils
from osgbuild.error import *


log = logging.getLogger('osgbuild')
log.propagate = False


class SRPMBuild(object):
    """Tasks for an SPRM build and helper functions."""

    def __init__(self, package_dir, buildopts, mock_obj, koji_obj):
        self.package_dir = package_dir
        self.mock_obj = mock_obj
        self.koji_obj = koji_obj

        self.buildopts = buildopts

        self.abs_package_dir = os.path.abspath(self.package_dir)
        self.package_name = os.path.basename(self.abs_package_dir)
        if not re.match(r"\w+", self.package_name): # sanity check
            raise Error("Package directory '%s' gives invalid package name '%s'" %
                        (self.abs_package_dir, self.package_name))
        # Unless working_directory is '.', i.e. we want to put the wd's in
        # the package dir, get rid of any parent or current directory
        # components so doing "osg-build pre -w TEMP ../foobar" won't put stuff
        # in "/tmp/foobar"
        if (os.path.realpath(self.buildopts['working_directory']) !=
                os.path.realpath('.')):
            package_dir_no_parent = re.sub(r'^(\.\.?/)+', '', package_dir)
            self.working_subdir = (
                os.path.abspath(
                    os.path.join(
                        self.buildopts['working_directory'],
                        package_dir_no_parent)))
        else:
            self.working_subdir = (
                os.path.abspath(
                    os.path.join(
                        self.buildopts['working_directory'],
                        package_dir)))
        self.results_dir = os.path.join(self.working_subdir, WD_RESULTS)
        self.prebuild_dir = os.path.join(self.working_subdir, WD_PREBUILD)
        self.unpacked_dir = os.path.join(self.working_subdir, WD_UNPACKED)
        self.unpacked_tarball_dir = os.path.join(self.working_subdir, WD_UNPACKED_TARBALL)
        self.quilt_dir = os.path.join(self.working_subdir, WD_QUILT)
    # end of __init__()


    def maybe_autoclean(self):
        """Delete underscore directories if the autoclean option is set."""
        if self.buildopts['autoclean']:
            for udir in [self.results_dir,
                         self.prebuild_dir,
                         self.unpacked_dir,
                         self.unpacked_tarball_dir]:
                if os.path.exists(udir):
                    log.debug("autoclean removing " + udir)
                    shutil.rmtree(udir)


    def get_rpmbuild_defines(self, prebuild):
        """Get a list of --define arguments to pass to rpmbuild based on the
        working dir and the subdirectories specified in the WD_* constants.

        """
        rhel = self.buildopts.get('redhat_release', '5')
        defines = [
            "_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",
            "rhel " + rhel,
            "dist ." + self.buildopts.get('distro_tag', 'osg.el' + rhel),
            "osg 1",
        ]
        for dver in DVERS:
            if dver == rhel:
                defines.append("el%s 1" % dver)
            else:
                defines.append("el%s 0" % dver)
        if rhel == '5':
            defines += [
                "_source_filedigest_algorithm 1",
                "_binary_filedigest_algorithm 1",
                "_binary_payload w9.gzdio",
                "_source_payload w9.gzdio",
                "_default_patch_fuzz 2",
            ]

        if prebuild:
            defines += [
                "_topdir " + self.prebuild_dir,
                "_builddir " + "%{_topdir}",
                "_buildrootdir " + "%{_topdir}",
                "_rpmdir " + "%{_topdir}",
                "_sourcedir " + "%{_topdir}",
                "_specdir " + "%{_topdir}",
                "_srcrpmdir " + "%{_topdir}",
                "_tmppath " + "%{_topdir}"
            ]
        else:
            defines += [
                "_topdir " + self.results_dir,
                "_builddir " + "%{_topdir}/BUILD",
                "_buildrootdir " + "%{_topdir}/BUILDROOT",
                "_rpmdir " + "%{_topdir}",
                "_sourcedir " + "%{_topdir}",
                "_specdir " + "%{_topdir}",
                "_srcrpmdir " + "%{_topdir}",
                "_tmppath " + "%{_topdir}/tmp"
            ]

        return ['--define=' + d for d in defines]
    # end of get_rpmbuild_defines()


    def make_srpm(self, spec_fn):
        """Make an SRPM from a spec file. Raise OSGPrebuildError on failure"""
        cmd = (["rpmbuild", "-bs", "--nodeps"] +
               self.get_rpmbuild_defines(prebuild=True) +
               [spec_fn])
        err_msg_prefix = ("Error making SRPM from %s\n"
                          "Command used was: %s\n" %
                          (spec_fn, " ".join(cmd)))
        out, err = utils.sbacktick(cmd, nostrip=True, clocale=True, err2out=True)
        if err:
            log.error("Rpmbuild failed. Output follows: " + out)
            raise OSGPrebuildError(err_msg_prefix +
                                   "Rpmbuild return code %d" % err)

        match = re.search(r"(?ms)^Wrote: ([^\n]+.src.rpm)$", out)
        if match:
            srpm = match.group(1).strip()
            if os.path.isfile(srpm):
                log.debug("Created SRPM: %s", srpm)
                return srpm
        raise OSGPrebuildError(err_msg_prefix +
                               "Unable to find resulting SRPM.")


    def prebuild_external_sources(self, destdir=None):
        """Collect sources and spec file in prebuild_dir. Return the path to
{'fraction_non_alphanumeric': 0.11836804662723922, 'fraction_numerical': 0.002742778777749207, 'mean_word_length': 1.7428302773859896, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7612062', 'n_tokens_mistral': 3644, 'n_tokens_neox': 3460, 'n_words': 770}