<commit_before># coding: utf-8
import os
from functools import partial, wraps
from contextlib import contextmanager

from fabric.api import sudo, run, path, puts, quiet
from fabric.contrib.files import upload_template

from .git import get_active_branch_name


def su(user):
    return partial(sudo, user=user)


def requires_branch(cls):
    """
    Enforce function callers to provide a branch name
    unless the current working directory holds a git repository with a branch checked in.

    An exception is raised if failed to obtain branch name.

    The decorator may be optionally passed a required branch name
    limiting the function execution scope to the specified branch only.
    """
    def decorator(arg, required_branch=None):
        def wrapper(branch=None, *args, **kwargs):
            force_branch = kwargs.pop('force_branch', False)
            if not isinstance(branch, cls):
                ci_branch = os.environ.get('BUILD_BRANCH')
                branch_name = branch if branch else (ci_branch or get_active_branch_name())
                if not branch_name:
                    raise ValueError('Not in a git repository. Provide a branch name')
                branch = cls(branch_name)
            # check if the function is allowed to run with this particular branch
            if not force_branch and required_branch and required_branch != branch.name:
                puts('{} does not match the required branch {}'.format(branch.name, required_branch))
                return
            return arg(branch, *args, **kwargs)

        if callable(arg):
            return wraps(arg)(wrapper)
        else:
            def inner_dec(func):
                return decorator(func, arg)
            return inner_dec
    return decorator


def requires_user(func):
    """<commit_msg>add support for multiple required branches<commit_after># coding: utf-8
import os
from functools import partial, wraps
from contextlib import contextmanager

from fabric.api import sudo, run, path, puts, quiet
from fabric.contrib.files import upload_template

from .git import get_active_branch_name


def su(user):
    return partial(sudo, user=user)


def requires_branch(cls):
    """
    Enforce function callers to provide a branch name
    unless the current working directory holds a git repository with a branch checked in.

    An exception is raised if failed to obtain branch name.

    The decorator may be optionally passed a required branch name
    limiting the function execution scope to the specified branch only.
    """
    def decorator(arg, required_branches=None):

        if required_branches and not isinstance(required_branches, (tuple, list)):
            required_branches = [required_branches]

        def wrapper(branch=None, *args, **kwargs):
            force_branch = kwargs.pop('force_branch', False)
            if not isinstance(branch, cls):
                ci_branch = os.environ.get('BUILD_BRANCH')
                branch_name = branch if branch else (ci_branch or get_active_branch_name())
                if not branch_name:
                    raise ValueError('Not in a git repository. Provide a branch name')
                branch = cls(branch_name)
            # check if the function is allowed to run with this particular branch
            if not force_branch and required_branche and branch.name not in required_branches:
                puts('{} does not match the required branch {}'.format(branch.name, required_branch))
                return
            return arg(branch, *args, **kwargs)

        if callable(arg):
            return wraps(arg)(wrapper)
        else:
            def inner_dec(func):
                return decorator(func, arg)
            return inner_dec
    return decorator


def requires_user(func):
    """
{'fraction_non_alphanumeric': 0.06787687450670876, 'fraction_numerical': 0.0005261773217574323, 'mean_word_length': 2.603791469194313, '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': '6475692', 'n_tokens_mistral': 968, 'n_tokens_neox': 939, 'n_words': 372}