<commit_before># Copyright (c) 2016-2018 <NAME>, <NAME>.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

import chardet
import json
import logging
import os
import picire
import picireny

from .picire_tester import PicireTester

logger = logging.getLogger(__name__)


def Picireny(sut_call, sut_call_kwargs, listener, ident, issue, work_dir,
             hddmin=None, parallel=False, combine_loops=False,
             split_method='zeller', subset_first=True, subset_iterator='forward', complement_iterator='forward',
             jobs=os.cpu_count(), max_utilization=100, encoding=None,
             antlr=None, format=None, grammar=None, start=None, replacements=None, lang='python',
             hdd_star=True, flatten_recursion=False, squeeze_tree=True, skip_unremovable=True, skip_whitespace=False,
             build_hidden_tokens=False, granularity=2, cache_class='ContentCache', cleanup=True,
             **kwargs):
    """
    Test case reducer based on the Picireny Hierarchical Delta Debugging
    Framework.

    **Mandatory parameters of the reducer:**

      - Either ``format`` or ``grammar`` and ``start`` must be defined.

    **Optional parameters of the reducer:**

      - ``hddmin``, ``parallel``, ``combine_loops``, ``split_method``, ``subset_first``,
        ``subset_iterator``, ``complement_iterator``, ``jobs``, ``max_utilization``, ``encoding``,
        ``antlr``, ``format``, ``grammar``, ``start``, ``replacements``, ``lang``,
        ``hdd_star``, ``flatten_recursion``, ``squeeze_tree``, ``skip_unremovable``, ``skip_whitespace``,
        ``build_hidden_tokens``, ``granularity``, ``cache_class``, ``cleanup``

    Refer to https://github.com/renatahodovan/picireny for configuring Picireny.

    Note: This reducer is capable of detecting new issues found during the test
    reduction (if any).

    **Example configuration snippet:**

        .. code-block:: ini

            [sut.foo]
            #call=...
            cost=1
            reduce=fuzzinator.reduce.Picireny
            reduce_cost=4

            [sut.foo.reduce]
            hddmin=full
            grammar=/home/alice/grammars-v4/HTMLParser.g4 /home/alice/grammars-v4/HTMLLexer.g4
            start=htmlDocument
            parallel=True
            jobs=4
            subset_iterator=skip
    """

    def eval_arg(arg):
        return eval(arg) if isinstance(arg, str) else arg

    logging.getLogger('picireny').setLevel(logger.level)

    antlr = picireny.process_antlr4_path(antlr)
    if antlr is None:
        return None, []

    input_format, start = picireny.process_antlr4_format(format=format, grammar=json.loads(grammar), start=start, replacements=replacements)

    if not (input_format and start):
        logger.warning('Processing the arguments of picireny failed.')
        return None, []

    src = issue['test']<commit_msg>Fix grammar-handling in picireny interface.<commit_after># Copyright (c) 2016-2018 <NAME>, <NAME>.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

import chardet
import json
import logging
import os
import picire
import picireny

from .picire_tester import PicireTester

logger = logging.getLogger(__name__)


def Picireny(sut_call, sut_call_kwargs, listener, ident, issue, work_dir,
             hddmin=None, parallel=False, combine_loops=False,
             split_method='zeller', subset_first=True, subset_iterator='forward', complement_iterator='forward',
             jobs=os.cpu_count(), max_utilization=100, encoding=None,
             antlr=None, format=None, grammar=None, start=None, replacements=None, lang='python',
             hdd_star=True, flatten_recursion=False, squeeze_tree=True, skip_unremovable=True, skip_whitespace=False,
             build_hidden_tokens=False, granularity=2, cache_class='ContentCache', cleanup=True,
             **kwargs):
    """
    Test case reducer based on the Picireny Hierarchical Delta Debugging
    Framework.

    **Mandatory parameters of the reducer:**

      - Either ``format`` or ``grammar`` and ``start`` must be defined.

    **Optional parameters of the reducer:**

      - ``hddmin``, ``parallel``, ``combine_loops``, ``split_method``, ``subset_first``,
        ``subset_iterator``, ``complement_iterator``, ``jobs``, ``max_utilization``, ``encoding``,
        ``antlr``, ``format``, ``grammar``, ``start``, ``replacements``, ``lang``,
        ``hdd_star``, ``flatten_recursion``, ``squeeze_tree``, ``skip_unremovable``, ``skip_whitespace``,
        ``build_hidden_tokens``, ``granularity``, ``cache_class``, ``cleanup``

    Refer to https://github.com/renatahodovan/picireny for configuring Picireny.

    Note: This reducer is capable of detecting new issues found during the test
    reduction (if any).

    **Example configuration snippet:**

        .. code-block:: ini

            [sut.foo]
            #call=...
            cost=1
            reduce=fuzzinator.reduce.Picireny
            reduce_cost=4

            [sut.foo.reduce]
            hddmin=full
            grammar=/home/alice/grammars-v4/HTMLParser.g4 /home/alice/grammars-v4/HTMLLexer.g4
            start=htmlDocument
            parallel=True
            jobs=4
            subset_iterator=skip
    """

    def eval_arg(arg):
        return eval(arg) if isinstance(arg, str) else arg

    logging.getLogger('picireny').setLevel(logger.level)

    antlr = picireny.process_antlr4_path(antlr)
    if antlr is None:
        return None, []

    input_format, start = picireny.process_antlr4_format(format=format, grammar=json.loads(grammar) if grammar else None, start=start, replacements=replacements)

    if not (input_format and start):
        logger.warning('Processing the arguments of picireny failed.')
        return None, []

    src = issue['test']
{'fraction_non_alphanumeric': 0.12663250123987435, 'fraction_numerical': 0.0076045627376425855, 'mean_word_length': 3.318344039971449, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '3490649', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1836, 'n_words': 435}