<commit_before>"""
Autobuilder configuration class.
"""
import os
import string
import time
import logging
from random import SystemRandom
from dateutil.parser import parse as dateparse
from twisted.internet import defer
from twisted.python import log
from buildbot.plugins import changes, schedulers, util, worker
from buildbot.www.hooks.github import GitHubEventHandler
from buildbot.config import BuilderConfig
from autobuilder import factory, settings
from autobuilder.ec2 import MyEC2LatentWorker
from autobuilder import utils

DEFAULT_BLDTYPES = ['ci', 'no-sstate', 'snapshot', 'release', 'pr']
RNG = SystemRandom()
default_svp = {'name': '/dev/xvdf', 'size': 200,
               'type': 'standard', 'iops': None}


class Buildtype(object):
    def __init__(self, name, build_sdk=False, install_sdk=False,
                 sdk_root=None, current_symlink=False, defaulttype=False,
                 pullrequesttype=False, production_release=False,
                 disable_sstate=False, extra_config=None):
        self.name = name
        self.build_sdk = build_sdk
        self.install_sdk = install_sdk
        self.sdk_root = sdk_root
        self.current_symlink = current_symlink
        self.defaulttype = defaulttype
        self.pullrequesttype = pullrequesttype
        self.production_release = production_release
        self.disable_sstate = disable_sstate
        self.extra_config = extra_config or ''


class Repo(object):
    def __init__(self, name, uri, pollinterval=None,
                 submodules=False):
        self.name = name
        self.uri = uri
        self.pollinterval = pollinterval
        self.submodules = submodules


class TargetImageSet(object):
    def __init__(self, name, images=None, sdkimages=None):
        self.name = name
        if images is None and sdkimages is None:
            raise RuntimeError('No images or SDK images defined for %s' %
                               name)
        self.images = images
        self.sdkimages = sdkimages


class Distro(object):
    def __init__(self, name, reponame, branch, email, path,
                 dldir=None, ssmirror=None,
                 targets=None, sdkmachines=None,
                 setup_script='./setup-env', repotimer=300,
                 artifacts=None,
                 sstate_mirrorvar='SSTATE_MIRRORS = "file://.* file://%s/PATH"',
                 dl_mirrorvar=None,
                 buildtypes=None, buildnum_template='DISTRO_BUILDNUM = "-%s"',
                 release_buildname_variable='DISTRO_BUILDNAME',
                 dl_mirror=None,
                 skip_sstate_update=False,
                 clean_downloads=True,
                 weekly_type=None,
                 push_type='__default__',
                 pullrequest_type=None,
                 extra_config=None):
        self.name = name
        self.reponame = reponame
        self.branch = branch
        self.email = email
        self.artifacts_path = path
        self.dl_dir = dldir
        self.sstate_mirror = ssmirror
        self.targets = targets
        self.sdkmachines = sdkmachines
        self.setup_script = setup_script
        self.repotimer = repotimer
        self.artifacts = artifacts
        self.sstate_mirrorvar = sstate_mirrorvar
        self.dl_mirrorvar = dl_mirrorvar
        self.dl_mirror = dl_mirror
        self.skip_sstate_update = skip_sstate_update
        self.clean_downloads = clean_downloads
        self.buildnum_template = buildnum_template
        self.release_buildname_variable = release_buildname_variable
        self.buildtypes = buildtypes
        if buildtypes is None:
            self.buildtypes = [Buildtype(bt) for bt in DEFAULT_BLDTYPES]
            self.buildtypes[0].defaulttype = True
        self.btdict = {bt.name: bt for bt in self.buildtypes}
        defaultlist = [bt.name for bt in self.buildtypes if bt.defaulttype]
        if len(defaultlist) != 1:
            raise RuntimeError('Must set exactly one default build type for %s' % self.name)
        self.default_buildtype = defaultlist[0]
        if weekly_type is not None and weekly_type not in self.btdict.keys():
            raise RuntimeError('Weekly build type for %s set to unknown type: %s' % (self.name, weekly_type))
        self.weekly_type = weekly_type
        if push_type:
            self.push_type = push_type if push_type != '__default__' else self.default_buildtype
        else:
            self.push_type = None
        if pullrequest_type:
            prtypelist = [bt.name for bt in self.buildtypes if bt.pullrequesttype]
            if len(prtypelist) != 1:
                raise RuntimeError('Must set exactly one PR build type for %s' % self.name)
            self.pullrequest_type = prtypelist[0]
        else:
            self.pullrequest_type = None
        self.extra_config = extra_config or ''

    def codebases(self, repos):
        cbdict = {self.reponame: {'repository': repos[self.reponame].uri}}
        return cbdict

    def codebaseparamlist(self, repos):
        return [util.CodebaseParameter(codebase=self.reponame,
                                       repository=util.FixedParameter(name='repository',
                                                                      default=repos[self.reponame].uri),
                                       branch=util.FixedParameter(name='branch', default=self.branch))]


class AutobuilderWorker(object):
    def __init__(self, name, password, conftext=None, max_builds=1):
        self.name = name
        self.password = password
        self.conftext = conftext
        self.max_builds = max_builds
        if max_builds > 1:
            threadconf = '\n'.join(['BB_NUMBER_THREADS = "${@oe.utils.cpu_count() // %d}"' % max_builds,
                                    'PARALLEL_MAKE = "${int(@oe.utils.cpu_count() // %d}"' % max_builds]) + '\n'
            if self.conftext:
                self.conftext += threadconf
            else:
                self.conftext = threadconf<commit_msg>Fix integer division<commit_after>"""
Autobuilder configuration class.
"""
import os
import string
import time
import logging
from random import SystemRandom
from dateutil.parser import parse as dateparse
from twisted.internet import defer
from twisted.python import log
from buildbot.plugins import changes, schedulers, util, worker
from buildbot.www.hooks.github import GitHubEventHandler
from buildbot.config import BuilderConfig
from autobuilder import factory, settings
from autobuilder.ec2 import MyEC2LatentWorker
from autobuilder import utils

DEFAULT_BLDTYPES = ['ci', 'no-sstate', 'snapshot', 'release', 'pr']
RNG = SystemRandom()
default_svp = {'name': '/dev/xvdf', 'size': 200,
               'type': 'standard', 'iops': None}


class Buildtype(object):
    def __init__(self, name, build_sdk=False, install_sdk=False,
                 sdk_root=None, current_symlink=False, defaulttype=False,
                 pullrequesttype=False, production_release=False,
                 disable_sstate=False, extra_config=None):
        self.name = name
        self.build_sdk = build_sdk
        self.install_sdk = install_sdk
        self.sdk_root = sdk_root
        self.current_symlink = current_symlink
        self.defaulttype = defaulttype
        self.pullrequesttype = pullrequesttype
        self.production_release = production_release
        self.disable_sstate = disable_sstate
        self.extra_config = extra_config or ''


class Repo(object):
    def __init__(self, name, uri, pollinterval=None,
                 submodules=False):
        self.name = name
        self.uri = uri
        self.pollinterval = pollinterval
        self.submodules = submodules


class TargetImageSet(object):
    def __init__(self, name, images=None, sdkimages=None):
        self.name = name
        if images is None and sdkimages is None:
            raise RuntimeError('No images or SDK images defined for %s' %
                               name)
        self.images = images
        self.sdkimages = sdkimages


class Distro(object):
    def __init__(self, name, reponame, branch, email, path,
                 dldir=None, ssmirror=None,
                 targets=None, sdkmachines=None,
                 setup_script='./setup-env', repotimer=300,
                 artifacts=None,
                 sstate_mirrorvar='SSTATE_MIRRORS = "file://.* file://%s/PATH"',
                 dl_mirrorvar=None,
                 buildtypes=None, buildnum_template='DISTRO_BUILDNUM = "-%s"',
                 release_buildname_variable='DISTRO_BUILDNAME',
                 dl_mirror=None,
                 skip_sstate_update=False,
                 clean_downloads=True,
                 weekly_type=None,
                 push_type='__default__',
                 pullrequest_type=None,
                 extra_config=None):
        self.name = name
        self.reponame = reponame
        self.branch = branch
        self.email = email
        self.artifacts_path = path
        self.dl_dir = dldir
        self.sstate_mirror = ssmirror
        self.targets = targets
        self.sdkmachines = sdkmachines
        self.setup_script = setup_script
        self.repotimer = repotimer
        self.artifacts = artifacts
        self.sstate_mirrorvar = sstate_mirrorvar
        self.dl_mirrorvar = dl_mirrorvar
        self.dl_mirror = dl_mirror
        self.skip_sstate_update = skip_sstate_update
        self.clean_downloads = clean_downloads
        self.buildnum_template = buildnum_template
        self.release_buildname_variable = release_buildname_variable
        self.buildtypes = buildtypes
        if buildtypes is None:
            self.buildtypes = [Buildtype(bt) for bt in DEFAULT_BLDTYPES]
            self.buildtypes[0].defaulttype = True
        self.btdict = {bt.name: bt for bt in self.buildtypes}
        defaultlist = [bt.name for bt in self.buildtypes if bt.defaulttype]
        if len(defaultlist) != 1:
            raise RuntimeError('Must set exactly one default build type for %s' % self.name)
        self.default_buildtype = defaultlist[0]
        if weekly_type is not None and weekly_type not in self.btdict.keys():
            raise RuntimeError('Weekly build type for %s set to unknown type: %s' % (self.name, weekly_type))
        self.weekly_type = weekly_type
        if push_type:
            self.push_type = push_type if push_type != '__default__' else self.default_buildtype
        else:
            self.push_type = None
        if pullrequest_type:
            prtypelist = [bt.name for bt in self.buildtypes if bt.pullrequesttype]
            if len(prtypelist) != 1:
                raise RuntimeError('Must set exactly one PR build type for %s' % self.name)
            self.pullrequest_type = prtypelist[0]
        else:
            self.pullrequest_type = None
        self.extra_config = extra_config or ''

    def codebases(self, repos):
        cbdict = {self.reponame: {'repository': repos[self.reponame].uri}}
        return cbdict

    def codebaseparamlist(self, repos):
        return [util.CodebaseParameter(codebase=self.reponame,
                                       repository=util.FixedParameter(name='repository',
                                                                      default=repos[self.reponame].uri),
                                       branch=util.FixedParameter(name='branch', default=self.branch))]


class AutobuilderWorker(object):
    def __init__(self, name, password, conftext=None, max_builds=1):
        self.name = name
        self.password = password
        self.conftext = conftext
        self.max_builds = max_builds
        if max_builds > 1:
            threadconf = '\n'.join(['BB_NUMBER_THREADS = "${@oe.utils.cpu_count() // %d}"' % max_builds,
                                    'PARALLEL_MAKE = "${@oe.utils.cpu_count() // %d}"' % max_builds]) + '\n'
            if self.conftext:
                self.conftext += threadconf
            else:
                self.conftext = threadconf
{'fraction_non_alphanumeric': 0.08562359034332971, 'fraction_numerical': 0.0025060563027316015, 'mean_word_length': 2.215686274509804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '81158', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3327, 'n_words': 789}