<commit_before>import logging
import re
import shlex
import subprocess

from rbtools.commands import RB_MAIN


# Regular expression for matching argument replacement
_arg_re = re.compile(r'\$(\d+)')


def replace_arguments(cmd, args):
    """Do parameter substitution for the given command.

    The special variable $* is expanded to contain all filenames.
    """
    def arg_sub(m):
        """Replace a positional variable with the appropriate argument."""
        index = int(m.group(1)) - 1

        try:
            return args[index]
        except IndexError:
            return ''

    did_replacement = False

    for part in shlex.split(cmd):
        if part == '$*':
            did_replacement = True

            for arg in args:
                yield arg
        else:
            part, subs = _arg_re.subn(arg_sub, part)

            if subs != 0:
                did_replacement = True

            yield part

    if not did_replacement:
        for arg in args:
            yield arg


def run_alias(alias, args):
    """Run the alias with the given arguments, after expanding parameters.

    Parameter expansion is done by the replace_arguments function.
    """
    use_shell = alias.startswith('!')

    try:
        if use_shell:
            # If we are using the shell, we must provide our program as a
            # string instead of a sequence.
            cmd = subprocess.list2cmdline(replace_arguments(alias[1:], args))
        else:
            cmd = [RB_MAIN] + list(replace_arguments(alias, args))

        return subprocess.call(cmd, shell=use_shell)
    except ValueError as e:
        logging.error("Could not execute alias '%s'; it was malformed: %s",
                      alias, e)
<commit_msg>Cleanup rbtools.utils.aliases<commit_after>from __future__ import unicode_literals

import logging
import re
import shlex
import subprocess

from rbtools.commands import RB_MAIN


# Regular expression for matching argument replacement
_arg_re = re.compile(r'\$(\d+)')


def replace_arguments(cmd, args):
    """Do parameter substitution for the given command.

    The special variable $* is expanded to contain all filenames.
    """
    def arg_sub(m):
        """Replace a positional variable with the appropriate argument."""
        index = int(m.group(1)) - 1

        try:
            return args[index]
        except IndexError:
            return ''

    did_replacement = False

    for part in shlex.split(cmd):
        if part == '$*':
            did_replacement = True

            for arg in args:
                yield arg
        else:
            part, subs = _arg_re.subn(arg_sub, part)

            if subs != 0:
                did_replacement = True

            yield part

    if not did_replacement:
        for arg in args:
            yield arg


def run_alias(alias, args):
    """Run the alias with the given arguments, after expanding parameters.

    Parameter expansion is done by the replace_arguments function.
    """
    use_shell = alias.startswith('!')

    try:
        if use_shell:
            # If we are using the shell, we must provide our program as a
            # string instead of a sequence.
            cmd = subprocess.list2cmdline(replace_arguments(alias[1:], args))
        else:
            cmd = [RB_MAIN] + list(replace_arguments(alias, args))

        return subprocess.call(cmd, shell=use_shell)
    except ValueError as e:
        logging.error('Could not execute alias "%s"; it was malformed: %s',
                      alias, e)

{'fraction_non_alphanumeric': 0.08340425531914894, 'fraction_numerical': 0.0028368794326241137, 'mean_word_length': 2.1231178033658105, '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': '2098888', 'n_tokens_mistral': 979, 'n_tokens_neox': 933, 'n_words': 335}