def reset_simulation_scenario_state(self): from miniworld import log from miniworld.Config import config log.info("resetting simulation_scenario_state") # objects may require singletons, hence first garbage collect objects for obj in self[KEY_OBJECT]: log.debug("resetting '%s'", obj) try: obj.reset() except NotImplementedError: log.critical( "Object '%s@%s' did not implement the reset() method!", obj, obj.__class__) except Exception as e: if config.is_log_cleanup(): log.exception(e) for singleton in self[KEY_SINGLETON]: log.debug("resetting '%s'", singleton) try: singleton.reset() except Exception as e: if config.is_log_cleanup(): log.exception(e) log.info("clearing simulate state objects ...") self[KEY_OBJECT] = []
def reset(self): ConnectionDummy.shaped_ifaces = defaultdict(lambda: False) # we stored the commands since the tap dev mapping is already reseted at this point for cleanup_cmd in ConnectionDummy.cleanup_commands: try: self.run(cleanup_cmd) except subprocess.CalledProcessError as e: log.exception(e) ConnectionDummy.cleanup_commands = set()
def close_sockets(self): if not self.is_reuse_socket(): try: self.sock.shutdown(socket.SHUT_RDWR) except socket.error as e: log.exception(e) try: self.sock.close() except socket.error as e: log.exception(e)
def parse_scenario_config(scenario_config=None, customize_scenario=None): """ Parameters ---------- scenario_config : optional (default value is taken from the `scenario_config_parser`) customize_scenario : optional (default value is taken from the `scenario_config_parser`) Returns ------- str, str, str """ def parse_args(): return scenario_config_parser.parse_known_args()[0] if customize_scenario is None: customize_scenario = parse_args().customize_scenario if scenario_config is None: scenario_config = parse_args().scenario_config # set scenario config if scenario_config: scenario_config = JSONConfig.read_json_config(scenario_config) if customize_scenario: try: custom_scenario = JSONConfig.read_json_config(customize_scenario, raw=True) # TODO: #61 DictUtil.merge_recursive_in_place(scenario_config, custom_scenario) except ValueError as e: log.exception(e) raise ConfigMalformed( "The supplied custom json scenario is invalid!") scenario_config_json = json.dumps(scenario_config) Scenario.set_scenario_config(scenario_config_json, raw=True) return scenario_config, custom_scenario, scenario_config_json
def __call__(self, *args, **kwargs): """ Execute the code lazily in the REPL. 1. Wait until the REPL is reachable 2. Connect 3. Execute the code line for line and wait for the shell prompt Returns ------- generator<socket, str, str, ...> Raises ------ REPLUnexpectedResult If the `return_value_checker` raised an Exception. REPLTimeout If the `timeout` occurred while waiting for results from the REPL socket. """ try: self.sock = None # wait until uds is reachable if self.is_reuse_socket(): if self.replable.uds_socket is None: self.replable.uds_socket = self.replable.wait_until_uds_reachable( return_sock=True) self.sock = self.replable.uds_socket else: self.sock = self.replable.wait_until_uds_reachable( return_sock=True) # TODO: old stuff # return socket object first yield self.sock # we may need to press enter first to activate the console/management socket self.enter_shell() # execute script on socket # NOTE: needed to let the generator finish the method execution for x in self.execute_script(): yield x except socket.error as e: self.brief_logger.exception(e) except Timeout as e: self.brief_logger.info('sending CTRL-C to shell ...') try: self.sock.send(b'\x03') except socket.error: pass raise REPLTimeout( "The REPL '%s' encountered a timeout (%s) while looking for shell prompt (%s)" % (self.replable, self.timeout, self.shell_prompt)) # finally close socket finally: try: self.close_sockets() except AttributeError as e: log.exception(e) raise ValueError( "The REPLable subclass '%s' did not return a valid socket!" % self.replable.__class__.__name__)