<commit_before># -*- coding: utf-8 -*-
import os
import re
import shutil

import salt.exceptions as exc


def _update(name, skipped=None, updateall=True):  # noqa

    ret = {
        'name': name,
        'changes': {},
        'result': False,
        'comment': ''
    }

    update_list = [] if updateall else [name]
    skipped = skipped or []

    runcli = __salt__['jenkins.runcli']  # noqa
    test = __opts__['test']  # noqa

    try:
        stdout = runcli('list-plugins')
    except exc.CommandExecutionError as e:
        ret['comment'] = "Failed to list plugins: %r" % (e.message,)
        return ret

    # match with ex.: 'maven-plugin  Maven plugin  2.7.1 (2.8)'
    RE_UPDATE = '(\w.+?)\s.*\s(\d+.*) \((.*)\)'
    for l in stdout.strip().split('\n'):

        m = re.match(RE_UPDATE, l)
        # no need to update
        if not m:
            continue

        name, current, update = m.groups()
        # no need to update
        if update_list and name not in update_list:
            continue
        # skipped
        if name in skipped:
            continue

        if not test:
            try:
                runcli('install-plugin', name)
            except exc.CommandExecutionError as e:
                ret['comment'] = "Failed to instal plugins: %s" % (e.message,)
                return ret

        ret['changes'][name] = {
            'old': current,
            'new': update,
        }

    return ret


(
    IS_INSTALLED,
    NOT_AVAILABLE
) = range(2)


def _info(name):

    # get info
    runcli = __salt__['jenkins.runcli']  # noqa
    stdout = runcli('list-plugins {0}'.format(name))

    # check info
    RE_INSTALL = '(\w.+?)\s.*\s(\d+.*)'
    m = re.match(RE_INSTALL, stdout)
    if not m:
        return NOT_AVAILABLE, 'Invalid info for {0}: {1}'.format(name, stdout)

    __, version = m.groups()
    return IS_INSTALLED, version


def installed(name):
    """Ensures jenkins plugins are present.

    name
        The name of one specific plugin to ensure.
    """
    ret = _update(name, updateall=False)

    runcli = __salt__['jenkins.runcli']  # noqa
    test = __opts__['test']  # noqa

    # just updated
    if name in ret['changes']:
        ret['result'] = None if test else True
        return ret<commit_msg>Handle new jenkins-cli exit code on list-plugins<commit_after># -*- coding: utf-8 -*-
import os
import re
import shutil

import salt.exceptions as exc


def _update(name, skipped=None, updateall=True):  # noqa

    ret = {
        'name': name,
        'changes': {},
        'result': False,
        'comment': ''
    }

    update_list = [] if updateall else [name]
    skipped = skipped or []

    runcli = __salt__['jenkins.runcli']  # noqa
    test = __opts__['test']  # noqa

    try:
        stdout = runcli('list-plugins')
    except exc.CommandExecutionError as e:
        ret['comment'] = "Failed to list plugins: %r" % (e.message,)
        return ret

    # match with ex.: 'maven-plugin  Maven plugin  2.7.1 (2.8)'
    RE_UPDATE = '(\w.+?)\s.*\s(\d+.*) \((.*)\)'
    for l in stdout.strip().split('\n'):

        m = re.match(RE_UPDATE, l)
        # no need to update
        if not m:
            continue

        name, current, update = m.groups()
        # no need to update
        if update_list and name not in update_list:
            continue
        # skipped
        if name in skipped:
            continue

        if not test:
            try:
                runcli('install-plugin', name)
            except exc.CommandExecutionError as e:
                ret['comment'] = "Failed to instal plugins: %s" % (e.message,)
                return ret

        ret['changes'][name] = {
            'old': current,
            'new': update,
        }

    return ret


(
    IS_INSTALLED,
    NOT_AVAILABLE
) = range(2)


def _info(name):

    # get info
    runcli = __salt__['jenkins.runcli']  # noqa
    try:
        stdout = runcli('list-plugins {0}'.format(name))
    except exc.CommandExecutionError as e:
        if 'ERROR: No plugin with the name' in e.message:
            return NOT_AVAILABLE, 'Error in listing {}'.format(name)
        else:
            raise

    # check info
    RE_INSTALL = '(\w.+?)\s.*\s(\d+.*)'
    m = re.match(RE_INSTALL, stdout)
    if not m:
        return NOT_AVAILABLE, 'Invalid info for {0}: {1}'.format(name, stdout)

    __, version = m.groups()
    return IS_INSTALLED, version


def installed(name):
    """Ensures jenkins plugins are present.

    name
        The name of one specific plugin to ensure.
    """
    ret = _update(name, updateall=False)

    runcli = __salt__['jenkins.runcli']  # noqa
    test = __opts__['test']  # noqa

    # just updated
    if name in ret['changes']:
        ret['result'] = None if test else True
        return ret
{'fraction_non_alphanumeric': 0.13120567375886524, 'fraction_numerical': 0.004171881518564873, 'mean_word_length': 1.9562268803945746, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3712633', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1502, 'n_words': 433}