<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2013, <NAME> <<EMAIL>>
# (c) 2016, <NAME> <<EMAIL>>
#
# Based on homebrew (<NAME> <<EMAIL>>)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: homebrew_tap
author:
    - "<NAME> (@indrajitr)"
    - "<NAME> (@danieljaouen)"
short_description: Tap a Homebrew repository.
description:
    - Tap external Homebrew repositories.
version_added: "1.6"
options:
    name:
        description:
            - The GitHub user/organization repository to tap.
        required: true
        aliases: ['tap']
    url:
        description:
            - The optional git URL of the repository to tap. The URL is not
              assumed to be on GitHub, and the protocol doesn't have to be HTTP.
              Any location and protocol that git can handle is fine.
            - I(name) option may not be a list of multiple taps (but a single
              tap instead) when this option is provided.
        required: false
        version_added: "2.2"
    state:
        description:
            - state of the repository.
        choices: [ 'present', 'absent' ]
        required: false
        default: 'present'
requirements: [ homebrew ]
'''

EXAMPLES = '''
- homebrew_tap:
    name: homebrew/dupes

- homebrew_tap:
    name: homebrew/dupes
    state: absent

- homebrew_tap:
    name: homebrew/dupes,homebrew/science
    state: present

- homebrew_tap:
    name: telemachus/brew
    url: 'https://bitbucket.org/telemachus/brew'
'''

import re

from ansible.module_utils.basic import AnsibleModule


def a_valid_tap(tap):
    '''Returns True if the tap is valid.'''
    regex = re.compile(r'^([\w-]+)/(homebrew-)?([\w-]+)$')
    return regex.match(tap)


def already_tapped(module, brew_path, tap):
    '''Returns True if already tapped.'''

    rc, out, err = module.run_command([
        brew_path,
        'tap',
    ])

    taps = [tap_.strip().lower() for tap_ in out.split('\n') if tap_]
    tap_name = re.sub('homebrew-', '', tap.lower())

    return tap_name in taps


def add_tap(module, brew_path, tap, url=None):
    '''Adds a single tap.'''
    failed, changed, msg = False, False, ''

    if not a_valid_tap(tap):
        failed = True
        msg = 'not a valid tap: %s' % tap

    elif not already_tapped(module, brew_path, tap):
        if module.check_mode:
            module.exit_json(changed=True)

        rc, out, err = module.run_command([
            brew_path,
            'tap',
            tap,
            url,
        ])
        if already_tapped(module, brew_path, tap):
            changed = True
            msg = 'successfully tapped: %s' % tap
        else:
            failed = True
            msg = 'failed to tap: %s' % tap

    else:
        msg = 'already tapped: %s' % tap

    return (failed, changed, msg)


def add_taps(module, brew_path, taps):
    '''Adds one or more taps.'''
    failed, unchanged, added, msg = False, 0, 0, ''

    for tap in taps:
        (failed, changed, msg) = add_tap(module, brew_path, tap)
        if failed:
            break
        if changed:
            added += 1
        else:
            unchanged += 1

    if failed:<commit_msg>homebrew_tap: fixes #40853 (#40863)<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2013, <NAME> <<EMAIL>>
# (c) 2016, <NAME> <<EMAIL>>
#
# Based on homebrew (<NAME> <<EMAIL>>)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: homebrew_tap
author:
    - "<NAME> (@indrajitr)"
    - "<NAME> (@danieljaouen)"
short_description: Tap a Homebrew repository.
description:
    - Tap external Homebrew repositories.
version_added: "1.6"
options:
    name:
        description:
            - The GitHub user/organization repository to tap.
        required: true
        aliases: ['tap']
    url:
        description:
            - The optional git URL of the repository to tap. The URL is not
              assumed to be on GitHub, and the protocol doesn't have to be HTTP.
              Any location and protocol that git can handle is fine.
            - I(name) option may not be a list of multiple taps (but a single
              tap instead) when this option is provided.
        required: false
        version_added: "2.2"
    state:
        description:
            - state of the repository.
        choices: [ 'present', 'absent' ]
        required: false
        default: 'present'
requirements: [ homebrew ]
'''

EXAMPLES = '''
- homebrew_tap:
    name: homebrew/dupes

- homebrew_tap:
    name: homebrew/dupes
    state: absent

- homebrew_tap:
    name: homebrew/dupes,homebrew/science
    state: present

- homebrew_tap:
    name: telemachus/brew
    url: 'https://bitbucket.org/telemachus/brew'
'''

import re

from ansible.module_utils.basic import AnsibleModule


def a_valid_tap(tap):
    '''Returns True if the tap is valid.'''
    regex = re.compile(r'^([\w-]+)/(homebrew-)?([\w-]+)$')
    return regex.match(tap)


def already_tapped(module, brew_path, tap):
    '''Returns True if already tapped.'''

    rc, out, err = module.run_command([
        brew_path,
        'tap',
    ])

    taps = [tap_.strip().lower() for tap_ in out.split('\n') if tap_]
    tap_name = re.sub('homebrew-', '', tap.lower())

    return tap_name in taps


def add_tap(module, brew_path, tap, url=None):
    '''Adds a single tap.'''
    failed, changed, msg = False, False, ''

    if not a_valid_tap(tap):
        failed = True
        msg = 'not a valid tap: %s' % tap

    elif not already_tapped(module, brew_path, tap):
        if module.check_mode:
            module.exit_json(changed=True)

        rc, out, err = module.run_command([
            brew_path,
            'tap',
            tap,
            url,
        ])
        if rc == 0:
            changed = True
            msg = 'successfully tapped: %s' % tap
        else:
            failed = True
            msg = 'failed to tap: %s' % tap

    else:
        msg = 'already tapped: %s' % tap

    return (failed, changed, msg)


def add_taps(module, brew_path, taps):
    '''Adds one or more taps.'''
    failed, unchanged, added, msg = False, 0, 0, ''

    for tap in taps:
        (failed, changed, msg) = add_tap(module, brew_path, tap)
        if failed:
            break
        if changed:
            added += 1
        else:
            unchanged += 1

    if failed:
{'fraction_non_alphanumeric': 0.1253064167267484, 'fraction_numerical': 0.00821917808219178, 'mean_word_length': 2.1787351054078825, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2395191', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2148, 'n_words': 657}