# Path: houston/exceptions.py
# class ConnectionLostError(HoustonException):
#     """
#     Thrown when the connection to the vehicle has been lost.
#     """
#     def __init__(self) -> None:
#         super().__init__("Connection to vehicle has been lost.")
# 
# class NoConnectionError(HoustonException):
#     """
#     Thrown when there is no connection to the vehicle running inside a sandbox.
#     """
#     def __init__(self) -> None:
#         super().__init__("No connection to vehicle inside sandbox.")


from typing import List, Iterator, Callable, Dict, Any
from houston.exceptions import ConnectionLostError, NoConnectionError
import os
import argparse
import concurrent.futures
import json
import logging
import contextlib
import functools
import signal, psutil
import bugzoo
import bugzoo.server
import houston
import settings
          sandbox_factory: SandboxFactory,
          jsn_mission: Dict[str, Any],
          num_repeats: int,
          dir_output: str,
          collect_coverage: bool
          ) -> None:
    mission = houston.Mission.from_dict(json.loads(jsn_mission))

    # generate a (very-likely-to-be) "unique" ID for the mission
    uid = hex(abs(hash(mission)))[2:][:8]
    logger.info("generating trace for mission %d: %s", index, uid)
    filename = "{}.json".format(uid)
    filename = os.path.join(dir_output, filename)
    if os.path.exists(filename):
        logger.info("skipping trace: %d ('%s' already exists)", index, filename)
        return

    try:
        traces = []  # List[MissionTrace]
        for _ in range(num_repeats):
            with sandbox_factory(collect_coverage) as sandbox:
                t = sandbox.run_and_trace(mission.commands, collect_coverage)
                traces.append(t)

        logger.debug("saving traces to file: %s", filename)
        with open(filename, 'w') as f:
            json.dump({'mission': mission.to_dict(),
                       'traces': [t.to_dict() for t in traces]},
                      f)
        logger.debug("saved trace to file: %s", filename)

# Path: houston/exceptions.py
# class ConnectionLostError(HoustonException):
#     """
#     Thrown when the connection to the vehicle has been lost.
#     """
#     def __init__(self) -> None:
#         super().__init__("Connection to vehicle has been lost.")
# 
# class NoConnectionError(HoustonException):
#     """
#     Thrown when there is no connection to the vehicle running inside a sandbox.
#     """
#     def __init__(self) -> None:
#         super().__init__("No connection to vehicle inside sandbox.")


# Path: experiments/build_traces.py
from typing import List, Iterator, Callable, Dict, Any
from houston.exceptions import ConnectionLostError, NoConnectionError
import os
import argparse
import concurrent.futures
import json
import logging
import contextlib
import functools
import signal, psutil
import bugzoo
import bugzoo.server
import houston
import settings

          sandbox_factory: SandboxFactory,
          jsn_mission: Dict[str, Any],
          num_repeats: int,
          dir_output: str,
          collect_coverage: bool
          ) -> None:
    mission = houston.Mission.from_dict(json.loads(jsn_mission))

    # generate a (very-likely-to-be) "unique" ID for the mission
    uid = hex(abs(hash(mission)))[2:][:8]
    logger.info("generating trace for mission %d: %s", index, uid)
    filename = "{}.json".format(uid)
    filename = os.path.join(dir_output, filename)
    if os.path.exists(filename):
        logger.info("skipping trace: %d ('%s' already exists)", index, filename)
        return

    try:
        traces = []  # List[MissionTrace]
        for _ in range(num_repeats):
            with sandbox_factory(collect_coverage) as sandbox:
                t = sandbox.run_and_trace(mission.commands, collect_coverage)
                traces.append(t)

        logger.debug("saving traces to file: %s", filename)
        with open(filename, 'w') as f:
            json.dump({'mission': mission.to_dict(),
                       'traces': [t.to_dict() for t in traces]},
                      f)
        logger.debug("saved trace to file: %s", filename)

    except (ConnectionLostError, NoConnectionError):