# Path: src/funfuzz/util/create_collector.py
# def make_collector():
# def printCrashInfo(crashInfo):  # pylint: disable=invalid-name,missing-docstring
# def printMatchingSignature(match):  # pylint: disable=invalid-name,missing-docstring
# def submit_collector(collector, crash_info, testcase, quality, meta_data=None):
# 
# Path: src/funfuzz/util/lithium_helpers.py
# (HAPPY, LITH_NO_REPRO, LITH_FINISHED, LITH_RETESTED_STILL_INTERESTING, LITH_BUSTED) = range(5)
# def pinpoint(itest, logPrefix, jsEngine, engineFlags, infilename,  # pylint: disable=invalid-name,missing-param-doc
#              bisectRepo, build_options_str, targetTime, suspiciousLevel):
# def run_lithium(lithArgs, logPrefix, targetTime):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
# def readLithiumResult(lithlogfn):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def reduction_strat(logPrefix, infilename, lithArgs, targetTime, lev):  # pylint: disable=invalid-name
#     def lith_reduce(strategy):


import io
import os
import subprocess
import sys
import tempfile
import FTB.Signatures.CrashInfo as Crash_Info
from optparse import OptionParser  # pylint: disable=deprecated-module
from pathlib import Path
from random import random
from shlex import quote
from FTB.ProgramConfiguration import ProgramConfiguration
from . import js_interesting
from . import shell_flags
from ..util import create_collector
from ..util import file_system_helpers
from ..util import lithium_helpers


def parseOptions(args):  # pylint: disable=invalid-name
    parser = OptionParser()
    parser.disable_interspersed_args()
    parser.add_option("--minlevel",
                      type="int", dest="minimumInterestingLevel",
                      default=js_interesting.JS_OVERALL_MISMATCH,
                      help="minimum js_interesting level for lithium to consider the testcase interesting")
    parser.add_option("--timeout",
                      type="int", dest="timeout",
                      default=10,
                      help="timeout in seconds")
    parser.add_option("--flags",
                      dest="flagsSpaceSep",
                      default="",
                      help="space-separated list of one set of flags")
    options, args = parser.parse_args(args)
    if len(args) != 3:
        raise Exception("Wrong number of positional arguments. Need 3 (knownPath, jsengine, infilename).")
    options.knownPath = Path(args[0]).expanduser().resolve()
    options.jsengine = Path(args[1]).expanduser().resolve()
    options.infilename = Path(args[2]).expanduser().resolve()
    options.flags = options.flagsSpaceSep.split(" ") if options.flagsSpaceSep else []
    if not options.jsengine.is_file():
        raise OSError(f"js shell does not exist: {options.jsengine}")

    # For js_interesting:
    options.valgrind = False
    options.shellIsDeterministic = True  # We shouldn't be in compare_jit with a non-deterministic build

# Path: src/funfuzz/util/create_collector.py
# def make_collector():
# def printCrashInfo(crashInfo):  # pylint: disable=invalid-name,missing-docstring
# def printMatchingSignature(match):  # pylint: disable=invalid-name,missing-docstring
# def submit_collector(collector, crash_info, testcase, quality, meta_data=None):
# 
# Path: src/funfuzz/util/lithium_helpers.py
# (HAPPY, LITH_NO_REPRO, LITH_FINISHED, LITH_RETESTED_STILL_INTERESTING, LITH_BUSTED) = range(5)
# def pinpoint(itest, logPrefix, jsEngine, engineFlags, infilename,  # pylint: disable=invalid-name,missing-param-doc
#              bisectRepo, build_options_str, targetTime, suspiciousLevel):
# def run_lithium(lithArgs, logPrefix, targetTime):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
# def readLithiumResult(lithlogfn):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def reduction_strat(logPrefix, infilename, lithArgs, targetTime, lev):  # pylint: disable=invalid-name
#     def lith_reduce(strategy):


# Path: src/funfuzz/js/compare_jit.py
import io
import os
import subprocess
import sys
import tempfile
import FTB.Signatures.CrashInfo as Crash_Info
from optparse import OptionParser  # pylint: disable=deprecated-module
from pathlib import Path
from random import random
from shlex import quote
from FTB.ProgramConfiguration import ProgramConfiguration
from . import js_interesting
from . import shell_flags
from ..util import create_collector
from ..util import file_system_helpers
from ..util import lithium_helpers



def parseOptions(args):  # pylint: disable=invalid-name
    parser = OptionParser()
    parser.disable_interspersed_args()
    parser.add_option("--minlevel",
                      type="int", dest="minimumInterestingLevel",
                      default=js_interesting.JS_OVERALL_MISMATCH,
                      help="minimum js_interesting level for lithium to consider the testcase interesting")
    parser.add_option("--timeout",
                      type="int", dest="timeout",
                      default=10,
                      help="timeout in seconds")
    parser.add_option("--flags",
                      dest="flagsSpaceSep",
                      default="",
                      help="space-separated list of one set of flags")
    options, args = parser.parse_args(args)
    if len(args) != 3:
        raise Exception("Wrong number of positional arguments. Need 3 (knownPath, jsengine, infilename).")
    options.knownPath = Path(args[0]).expanduser().resolve()
    options.jsengine = Path(args[1]).expanduser().resolve()
    options.infilename = Path(args[2]).expanduser().resolve()
    options.flags = options.flagsSpaceSep.split(" ") if options.flagsSpaceSep else []
    if not options.jsengine.is_file():
        raise OSError(f"js shell does not exist: {options.jsengine}")

    # For js_interesting:
    options.valgrind = False
    options.shellIsDeterministic = True  # We shouldn't be in compare_jit with a non-deterministic build

    options.collector = create_collector.make_collector()