def test_unchanged_storage_format(caplog): """This test ensures that the state machine storage format does not change in patch releases""" from rafcon.core.storage import storage from rafcon.gui.models.state_machine import StateMachineModel import rafcon path = get_backward_compatibility_state_machines_path() if not os.path.exists(path): logger.info( "test_unchanged_storage_format: the current python interpreter version is not supported" ) return testing_utils.initialize_environment( gui_config={ 'HISTORY_ENABLED': False, 'AUTO_BACKUP_ENABLED': False }, libraries={ 'unit_test_state_machines': testing_utils.get_test_sm_path("unit_test_state_machines") }, gui_already_started=False) try: current_rafcon_version = StrictVersion(rafcon.__version__).version current_minor = "{}.{}".format(current_rafcon_version[0], current_rafcon_version[1]) for filename in os.listdir(path): if filename.startswith(current_minor): old_state_machine_path = os.path.join(path, filename) break else: assert False, "There is no state machine for the current RAFCON minor version {}".format( current_minor) state_machine = storage.load_state_machine_from_path( old_state_machine_path) state_machine_m = StateMachineModel(state_machine) new_state_machine_path = testing_utils.get_unique_temp_path() storage.save_state_machine_to_path(state_machine, new_state_machine_path, True, True) state_machine_m.store_meta_data(copy_path=new_state_machine_path) old_state_machine_hash = calculate_state_machine_hash( old_state_machine_path) new_state_machine_hash = calculate_state_machine_hash( new_state_machine_path) assert old_state_machine_hash.digest( ) == new_state_machine_hash.digest() except Exception: raise finally: testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
def test_on_clean_storing_with_name_in_path(caplog): testing_utils.dummy_gui(None) testing_utils.initialize_environment(gui_config={ 'HISTORY_ENABLED': False, 'AUTO_BACKUP_ENABLED': False }, gui_already_started=False) path_old_format = testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "id_to_name_plus_id_storage_format_test_do_not_update")) path_new_format = os.path.join( testing_utils.get_unique_temp_path(), "id_to_name_plus_id_storage_format_test_do_not_update") # gui imports better after initialization from rafcon.gui.models.state_machine import StateMachineModel shutil.copytree(path_old_format, path_new_format) from rafcon.core.storage import storage sm = storage.load_state_machine_from_path(path_new_format) check_state_recursively_if_state_scripts_are_valid(sm.root_state) sm.base_path = path_new_format sm_m = StateMachineModel(sm) try: on_save_activate(sm_m, logger) check_that_all_files_are_there(sm, with_print=False) check_id_and_name_plus_id_format(path_old_format, path_new_format, sm_m) finally: testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
def __init__(self, state_machine_manager, meta=None): """Constructor""" ModelMT.__init__(self) # pass columns as separate parameters self.register_observer(self) assert isinstance(state_machine_manager, StateMachineManager) self.state_machine_manager = state_machine_manager self.state_machines = {} for sm_id, sm in state_machine_manager.state_machines.items(): self.state_machines[sm_id] = StateMachineModel(sm) self._selected_state_machine_id = None if len(self.state_machines) > 0: self.selected_state_machine_id = list( self.state_machines.keys())[0] if isinstance(meta, Vividict): self.meta = meta else: self.meta = Vividict() # check if the sm_manager_model exists several times self.__class__.__sm_manager_creation_counter += 1 if self.__class__.__sm_manager_creation_counter == 2: logger.error("Sm_manager_model exists several times!") os._exit(0)
def model_changed(self, model, prop_name, info): if isinstance(info['result'], Exception): from rafcon.gui.utils.notification_overview import NotificationOverview logger.exception("An '{0}' exception was raised in the core. " "Details about the origin:\n{1}" "".format( type(info['result']).__name__, NotificationOverview(info))) return if info["method_name"] == "add_state_machine": logger.debug("Add new state machine model ... ") sm_id = info['result'] if sm_id in self.state_machine_manager.state_machines and sm_id not in self.state_machines: logger.debug( "Create new state machine model for state machine with id %s", sm_id) sm = self.state_machine_manager.state_machines[sm_id] with sm.modification_lock(): self.state_machines[sm_id] = StateMachineModel(sm) from rafcon.gui.models.abstract_state import AbstractStateModel logger.verbose("Number of created state models {}".format( AbstractStateModel.state_counter)) self.selected_state_machine_id = sm_id else: logger.error( "Model of state machine {0} is supposed to not exist but the state machine object should." "".format(sm_id)) elif info["method_name"] == "remove_state_machine": sm_id_to_delete = info['result'].state_machine_id if sm_id_to_delete is not None: logger.debug( "Delete state machine model for state machine with id %s", sm_id_to_delete) if self.selected_state_machine_id == sm_id_to_delete: self.selected_state_machine_id = None sm_m = self.state_machines[sm_id_to_delete] sm_m.prepare_destruction() del self.state_machines[sm_id_to_delete] sm_m.destroy()
def model_changed(self, model, prop_name, info): if isinstance(info['result'], Exception): from rafcon.gui.utils.notification_overview import NotificationOverview logger.exception( "The result type is {0} and the full notification {1}" "".format(type(info['result']), NotificationOverview(info))) return if info["method_name"] == "add_state_machine": logger.debug("Add new state machine model ... ") sm_id = info['result'] if sm_id in self.state_machine_manager.state_machines and sm_id not in self.state_machines: logger.debug( "Create new state machine model for state machine with id %s", sm_id) sm = self.state_machine_manager.state_machines[sm_id] with sm.modification_lock(): self.state_machines[sm_id] = StateMachineModel(sm) self.selected_state_machine_id = sm_id else: logger.error( "Model of state machine {0} is supposed to not exist but the state machine object should." "".format(sm_id)) elif info["method_name"] == "remove_state_machine": sm_id_to_delete = info['result'].state_machine_id if sm_id_to_delete is not None: logger.debug( "Delete state machine model for state machine with id %s", sm_id_to_delete) if self.selected_state_machine_id == sm_id_to_delete: self.selected_state_machine_id = None sm_m = self.state_machines[sm_id_to_delete] sm_m.prepare_destruction() del self.state_machines[sm_id_to_delete] sm_m.destroy()
def run_copy_performance_test_and_check_storage_copy(*args): """Run general test that """ from rafcon.gui.models.state_machine import StateMachineModel from rafcon.core.storage import storage sm_m = args[0] sm = sm_m.state_machine # storage copy tests if sm.file_system_path is None: tmp_sm_system_path = join(testing_utils.RAFCON_TEMP_PATH_TEST_BASE, 'copy_test_' + str(sm.state_machine_id)) else: tmp_sm_system_path = join(testing_utils.RAFCON_TEMP_PATH_TEST_BASE, 'copy_test' + sm.file_system_path) # performance tests time_only_storage_before = time.time() storage.save_state_machine_to_path(sm_m.state_machine, tmp_sm_system_path, delete_old_state_machine=False, as_copy=True) sm_m.store_meta_data(copy_path=tmp_sm_system_path) time_only_storage_after = time.time() only_storage_duration = round(time_only_storage_after * 1000000) - round( time_only_storage_before * 1000000) time_storage_before = time.time() storage.save_state_machine_to_path(sm_m.state_machine, tmp_sm_system_path, delete_old_state_machine=False, as_copy=True) sm_m.store_meta_data(copy_path=tmp_sm_system_path) sm1 = storage.load_state_machine_from_path(tmp_sm_system_path) time_model_before = time.time() sm1_m = StateMachineModel(sm1) time_model_after = time.time() time_storage_after = time_model_after only_model_duration = round(time_model_after * 1000000) - round( time_model_before * 1000000) storage_copy_duration = round(time_storage_after * 1000000) - round( time_storage_before * 1000000) equal_check_state(sm_m.root_state.state, sm1_m.root_state.state) equal_check_state_model(sm_m.root_state, sm1_m.root_state) time_copy_before = time.time() copy.copy(sm_m.state_machine) time_copy_after = time.time() core_copy_duration = round(time_copy_after * 1000000) - round( time_copy_before * 1000000) time_copy_m_before = time.time() copy.copy(sm_m) time_copy_m_after = time.time() model_copy_duration = round(time_copy_m_after * 1000000) - round( time_copy_m_before * 1000000) sm1_m.destroy() print("only_model_duration: {}".format(only_model_duration)) print("only_storage_duration: {}".format(only_storage_duration)) print("storage_copy_duration: {}".format(storage_copy_duration)) print("core_copy_duration: {}".format(core_copy_duration)) print("model_copy_duration: {}".format(model_copy_duration))