<commit_before># -*- coding: utf-8 -*-
"""Clowder command controller class

.. codeauthor:: <NAME> <<EMAIL>>

"""

import sys

from termcolor import cprint

from clowder.commands.util import (
    existing_branch_groups,
    existing_branch_projects,
    run_group_command,
    run_project_command,
    validate_groups,
    validate_projects
)


def prune(clowder, group_names, branch, **kwargs):
    """Prune branches

    .. py:function:: prune(group_names, local=False, remote=False, force=False, project_names=None, skip=[])

    :param ClowderController clowder: ClowderController instance
    :param list(str) group_names: Group names to prune branches for
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) project_names: Project names to prune
    :param list(str) skip: Project names to skip
    """

    project_names = kwargs.get('project_names', None)
    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    if project_names is None:
        groups = [g for g in clowder.groups if g.name in group_names]
        validate_groups(groups)
        _prune_groups(groups, branch, skip=skip, force=force, local=local, remote=remote)
        return

    projects = [p for g in clowder.groups for p in g.projects if p.name in project_names]
    validate_projects(projects)
    _prune_projects(projects, branch, skip=skip, force=force, local=local, remote=remote)


def _prune_groups(groups, branch, **kwargs):
    """Prune group branches

    .. py:function:: _prune_groups(groups, branch, local=False, remote=False, force=False, skip=[])

    :param list(Group) groups: Groups to prune
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) skip: Project names to skip
    """

    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    if local and remote:
        local_branch_exists = existing_branch_groups(groups, branch, is_remote=False)
        remote_branch_exists = existing_branch_groups(groups, branch, is_remote=True)
        branch_exists = local_branch_exists or remote_branch_exists
        if not branch_exists:
            cprint(' - No local or remote branches to prune\n', 'red')
            sys.exit()

        print(' - Prune local and remote branches\n')
        for group in groups:
            local_branch_exists = group.existing_branch(branch, is_remote=False)
            remote_branch_exists = group.existing_branch(branch, is_remote=True)
            if local_branch_exists or remote_branch_exists:
                run_group_command(group, skip, 'prune', branch, force=force, local=True, remote=True)
    elif local:
        if not existing_branch_groups(groups, branch, is_remote=False):
            print(' - No local branches to prune\n')
            sys.exit()

        for group in groups:
            if group.existing_branch(branch, is_remote=False):
                run_group_command(group, skip, 'prune', branch, force=force, local=True)
    elif remote:
        if not existing_branch_groups(groups, branch, is_remote=True):
            cprint(' - No remote branches to prune\n', 'red')
            sys.exit()

        for group in groups:
            if group.existing_branch(branch, is_remote=True):
                run_group_command(group, skip, 'prune', branch, remote=True)


def _prune_projects(projects, branch, **kwargs):
    """Prune project branches

    .. py:function:: _prune_projects(projects, branch, local=False, remote=False, force=False, skip=[])

    :param list(Project) projects: Projects to prune
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) skip: Project names to skip
    """

    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    if local and remote:
        local_branch_exists = existing_branch_projects(projects, branch, is_remote=False)
        remote_branch_exists = existing_branch_projects(projects, branch, is_remote=True)
        branch_exists = local_branch_exists or remote_branch_exists
        if not branch_exists:
            cprint(' - No local or remote branches to prune\n', 'red')
            sys.exit()

        print(' - Prune local and remote branches\n')
        for project in projects:
            run_project_command(project, skip, 'prune', branch, force=force, local=True, remote=True)
    elif local:
        if not existing_branch_projects(projects, branch, is_remote=False):
            print(' - No local branches to prune\n')
            sys.exit()

        for project in projects:
            run_project_command(project, skip, 'prune', branch, force=force, local=True)
    elif remote:
        if not existing_branch_projects(projects, branch, is_remote=True):
            cprint(' - No remote branches to prune\n', 'red')
            sys.exit()

        for project in projects:
            run_project_command(project, skip, 'prune', branch, remote=True)<commit_msg>Refactor prune logic to reduce complexity<commit_after># -*- coding: utf-8 -*-
"""Clowder command controller class

.. codeauthor:: <NAME> <<EMAIL>>

"""

import sys

from termcolor import cprint

from clowder.commands.util import (
    existing_branch_groups,
    existing_branch_projects,
    run_group_command,
    run_project_command,
    validate_groups,
    validate_projects
)


def prune(clowder, group_names, branch, **kwargs):
    """Prune branches

    .. py:function:: prune(group_names, local=False, remote=False, force=False, project_names=None, skip=[])

    :param ClowderController clowder: ClowderController instance
    :param list(str) group_names: Group names to prune branches for
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) project_names: Project names to prune
    :param list(str) skip: Project names to skip
    """

    project_names = kwargs.get('project_names', None)
    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    if project_names is None:
        groups = [g for g in clowder.groups if g.name in group_names]
        validate_groups(groups)
        _prune_groups(groups, branch, skip=skip, force=force, local=local, remote=remote)
        return

    projects = [p for g in clowder.groups for p in g.projects if p.name in project_names]
    validate_projects(projects)
    _prune_projects(projects, branch, skip=skip, force=force, local=local, remote=remote)


def _prune_groups(groups, branch, **kwargs):
    """Prune group branches

    .. py:function:: _prune_groups(groups, branch, local=False, remote=False, force=False, skip=[])

    :param list(Group) groups: Groups to prune
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) skip: Project names to skip
    """

    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    local_branch_exists = existing_branch_groups(groups, branch, is_remote=False)
    remote_branch_exists = existing_branch_groups(groups, branch, is_remote=True)

    _validate_branches(local, remote, local_branch_exists, remote_branch_exists)

    for group in groups:
        if group.existing_branch(branch, is_remote=remote):
            run_group_command(group, skip, 'prune', branch, force=force, local=local, remote=remote)


def _prune_projects(projects, branch, **kwargs):
    """Prune project branches

    .. py:function:: _prune_projects(projects, branch, local=False, remote=False, force=False, skip=[])

    :param list(Project) projects: Projects to prune
    :param str branch: Branch to prune
    :param bool force: Force delete branch
    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param list(str) skip: Project names to skip
    """

    skip = kwargs.get('skip', [])
    force = kwargs.get('force', False)
    local = kwargs.get('local', False)
    remote = kwargs.get('remote', False)

    local_branch_exists = existing_branch_projects(projects, branch, is_remote=False)
    remote_branch_exists = existing_branch_projects(projects, branch, is_remote=True)

    _validate_branches(local, remote, local_branch_exists, remote_branch_exists)

    for project in projects:
        run_project_command(project, skip, 'prune', branch, force=force, local=local, remote=remote)


def _validate_branches(local, remote, local_branch_exists, remote_branch_exists):
    """Prune project branches

    .. py:function:: _prune_projects(projects, branch, local=False, remote=False, force=False, skip=[])

    :param bool local: Delete local branch
    :param bool remote: Delete remote branch
    :param bool local_branch_exists: Whether a local branch exists
    :param bool remote_branch_exists: Whether a remote branch exists
    """

    if local and remote:
        branch_exists = local_branch_exists or remote_branch_exists
        if not branch_exists:
            cprint(' - No local or remote branches to prune\n', 'red')
            sys.exit()
        print(' - Prune local and remote branches\n')
    elif local:
        if not local_branch_exists:
            print(' - No local branches to prune\n')
            sys.exit()
        print(' - Prune local branches\n')
    elif remote:
        if not remote_branch_exists:
            cprint(' - No remote branches to prune\n', 'red')
            sys.exit()
        print(' - Prune remote branches\n')
{'fraction_non_alphanumeric': 0.10117830363229137, 'fraction_numerical': 0.00019476093095724997, 'mean_word_length': 3.3776641091219095, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '907523', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2935, 'n_words': 904}