<commit_before>import os
import logging
import errno
import re
from subprocess import check_call, STDOUT

import beanbag
import pdcupdater.handlers
import pdcupdater.services
from pdcupdater.utils import TmpDir, PushPopD

import modulemd

log = logging.getLogger(__name__)


class ModuleStateChangeHandler(pdcupdater.handlers.BaseHandler):
    """ When the state of a module changes. """

    tree_processing_states = set(('done', 'ready'))
    other_states = set(('init', 'wait', 'building'))
    relevant_states = tree_processing_states.union(other_states)
    error_states = set(('failed',))
    valid_states = relevant_states.union(error_states)

    tree_id_re = re.compile(
        r"(?P<name>[^-]+)-(?P<version>[^-]+)-"
        r"(?P<date>[0-9]{8})\.(?P<spin>[0-9]+)")

    rpm_fname_re = re.compile(
        r"(?P<name>.+)-"
        r"(?:(?P<epoch>[^:]+):)?(?P<version>[^-:]+)-(?P<release>[^-]+)."
        r"(?P<arch>[^\.]+).rpm")

    scmurl_re = re.compile(
        r"(?P<giturl>(?:(?P<scheme>git)://(?P<host>[^/]+))?"
        r"(?P<repopath>/[^\?]+))\?(?P<modpath>[^#]*)#(?P<revision>.+)")

    @property
    def topic_suffixes(self):
        return ['module.state.change']

    def can_handle(self, msg):
        if not msg['topic'].endswith('module.state.change'):
            return False

        state = msg['msg']['state']

        if state not in self.valid_states:
            log.error("Invalid module state '{}', skipping.".format(state))
            return False

        if state not in self.relevant_states:
            log.debug("Non-relevant module state '{}', skipping.".format(
                state))
            return False

        return True

    def handle(self, pdc, msg):
        body = msg['msg']
        state = body['state']

        if state not in self.relevant_states:
            log.warn("Non-relevant module state '{}', skipping.".format(
                state))
            return

        unreleased_variant = self.get_or_create_unreleased_variant(pdc, body)

        # trees are only present when a module is done building, i.e. states
        # 'done' or 'ready'
        if 'topdir' in body:
            self.handle_new_tree(pdc, body, unreleased_variant)

    def get_mmd_from_scm(self, scmurl):
        with TmpDir(prefix="pdcupdater-") as tmpdir, PushPopD(tmpdir), \
                open(os.devnull, "w") as devnull:
            m = self.scmurl_re.match(scmurl)
            if not m:
                raise RuntimeError("Can't parse SCM URL: {}".format(scmurl))
            giturl = m.group('giturl')
            repopath = m.group('repopath').rstrip("/")
            modpath = m.group('modpath')
            revision = m.group('revision')
            modname = repopath.rsplit("/", 1)[1]
            if modname.endswith(".git"):
                modname = modname[:-4]<commit_msg>Use state_name instead of state.<commit_after>import os
import logging
import errno
import re
from subprocess import check_call, STDOUT

import beanbag
import pdcupdater.handlers
import pdcupdater.services
from pdcupdater.utils import TmpDir, PushPopD

import modulemd

log = logging.getLogger(__name__)


class ModuleStateChangeHandler(pdcupdater.handlers.BaseHandler):
    """ When the state of a module changes. """

    tree_processing_states = set(('done', 'ready'))
    other_states = set(('init', 'wait', 'building'))
    relevant_states = tree_processing_states.union(other_states)
    error_states = set(('failed',))
    valid_states = relevant_states.union(error_states)

    tree_id_re = re.compile(
        r"(?P<name>[^-]+)-(?P<version>[^-]+)-"
        r"(?P<date>[0-9]{8})\.(?P<spin>[0-9]+)")

    rpm_fname_re = re.compile(
        r"(?P<name>.+)-"
        r"(?:(?P<epoch>[^:]+):)?(?P<version>[^-:]+)-(?P<release>[^-]+)."
        r"(?P<arch>[^\.]+).rpm")

    scmurl_re = re.compile(
        r"(?P<giturl>(?:(?P<scheme>git)://(?P<host>[^/]+))?"
        r"(?P<repopath>/[^\?]+))\?(?P<modpath>[^#]*)#(?P<revision>.+)")

    @property
    def topic_suffixes(self):
        return ['module.state.change']

    def can_handle(self, msg):
        if not msg['topic'].endswith('module.state.change'):
            return False

        state = msg['msg']['state_name']

        if state not in self.valid_states:
            log.error("Invalid module state '{}', skipping.".format(state))
            return False

        if state not in self.relevant_states:
            log.debug("Non-relevant module state '{}', skipping.".format(
                state))
            return False

        return True

    def handle(self, pdc, msg):
        body = msg['msg']
        state = body['state_name']

        if state not in self.relevant_states:
            log.warn("Non-relevant module state '{}', skipping.".format(
                state))
            return

        unreleased_variant = self.get_or_create_unreleased_variant(pdc, body)

        # trees are only present when a module is done building, i.e. states
        # 'done' or 'ready'
        if 'topdir' in body:
            self.handle_new_tree(pdc, body, unreleased_variant)

    def get_mmd_from_scm(self, scmurl):
        with TmpDir(prefix="pdcupdater-") as tmpdir, PushPopD(tmpdir), \
                open(os.devnull, "w") as devnull:
            m = self.scmurl_re.match(scmurl)
            if not m:
                raise RuntimeError("Can't parse SCM URL: {}".format(scmurl))
            giturl = m.group('giturl')
            repopath = m.group('repopath').rstrip("/")
            modpath = m.group('modpath')
            revision = m.group('revision')
            modname = repopath.rsplit("/", 1)[1]
            if modname.endswith(".git"):
                modname = modname[:-4]
{'fraction_non_alphanumeric': 0.1631828561391182, 'fraction_numerical': 0.002810468997013877, 'mean_word_length': 2.5278810408921935, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100213', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1815, 'n_words': 373}