def main(proj_name, project_inst, run_folder, template_fn, iters, aggr_min, aggr_max,
         keep, timeout, write_pickle, reduce, reduce_n):
    "main loop"
    ext = os.path.splitext(os.path.basename(template_fn))[1]
    results = 0
    iterno = 0
    is_replay = (iters == 1 and aggr_min == 0 and aggr_max == 0)
    log.info("Running project %s for %d iteration(s).", proj_name, iters)
    log.info("Results will be written to %s", run_folder)
    log.info("Iteration timeout: %r", timeout)
    log.info("Ctrl+C to quit")
    log.info("%-20s %-10s %s", "Iterations", "Rate", "Failures")
    start_time = time.time()
    print_time = start_time
    done = False
    if timeout is not None:
        timeout_event = threading.Event()
        timeout_continue = threading.Event()
        class TimeoutThread(threading.Thread):
            def run(self):
                while not done:
                    if timeout_event.wait(timeout) is False:
                        # dump thread stacks and exit
                        log.error("Iteration timeout occurred!")
                        for thread_id, stack in sys._current_frames().items():
                            if thread_id == self.ident:
                                continue
                            log.error("Thread: %d", thread_id)
                            traceback.print_stack(stack)
                            log.error("")
                        _thread.interrupt_main()
                        return
                    timeout_event.clear()
                    timeout_continue.set()
        tout_tid = TimeoutThread()
        tout_tid.start()
    try:
        while not iters or iterno < iters:
            printed = False
            # create mutation fn
            if is_replay:
                mutation_fn = os.path.basename(template_fn)
            else:
                mutation_fn = "mutation_%08X%s" % (iterno, ext)
            # do an iteration
            iter_had_result = False
            cls = ""
            result = project_inst.do_iteration(mutation_fn, random.randint(aggr_max, aggr_min))

            if result is not None:
                if not isinstance(result, FuzzResult):
                    raise TypeError("Expecting FuzzResult, not %s" % type(result))
                iter_had_result = True
                cls = result.classification
                if result.classification != "NOT_AN_EXCEPTION":
                    if not os.path.isfile(mutation_fn):
                        raise Exception("result reported before mutation written to disk")
                    results += 1
                if keep or result.classification != "NOT_AN_EXCEPTION":
                    if is_replay:
                        log_fn = "%s.log.xml" % os.path.basename(template_fn)
                        pkl_fn = "%s.pkl" % os.path.basename(template_fn)
                    else:
                        log_fn = "mutation_%08X.log.xml" % (iterno)
                        pkl_fn = "mutation_%08X.pkl" % iterno
                    with open(log_fn, "w") as logf:
                        logf.write("<log>\n")
                        logf.write("<classification>%s</classification>\n" % result.classification)
                        logf.write("<backtrace>\n")
                        for lso in result.backtrace:
                            logf.write("<sym>%s</sym>\n" % lso)
                        logf.write("</backtrace>\n")
                        logf.write("<text>\n")
                        logf.write(result.text)
                        logf.write("</text>\n")
                        logf.write("</log>\n")
                    if write_pickle:
                        with open(pkl_fn, "wb") as f:
                            pickle.dump(result, f)
                    if reduce:
                        with open(template_fn, "rb") as f:
                            mutation = f.read()
                        for r in reduce:
                            mutation = _reduce(project_inst, r, reduce_n,
                                               mutation, mutation_fn, result)
                        oresult = result
                        with open(mutation_fn, "wb") as f:
                            f.write(mutation)
                        result = project_inst.run_subject(mutation_fn)
                        if not project_inst.resultmatch(oresult, result):
                            raise Exception("Result didn't match post-reduce")
            elif reduce:
                log.warning("--reduce specified, but no failure was found")
            # remove the mutation if it didn't cause failure
            if not keep and (not iter_had_result or cls == "NOT_AN_EXCEPTION" or is_replay):
                delete(mutation_fn)
            iterno += 1
            if time.time() - print_time >= 10:
                print_time = time.time()
                print_progress(start_time, iterno, results)
                printed = True
            if timeout is not None:
                timeout_event.set()
                timeout_continue.wait()
            do_deletes()
    except __HOLE__:
        log.info("User interrupt")
    finally:
        if not printed:
            print_progress(start_time, iterno, results)
        elapsed_time = time.time() - start_time
        log.info("Ran %d iterations and found %d results in %.2fs", iterno, results, elapsed_time)
        project_inst.cleanup()
        project_inst.finish()
        done = True
        if timeout is not None:
            timeout_event.set()
        do_deletes()
dataset/ETHPy150Open blackberry/ALF/alf/local.py/main