def _get_target_folder(resources, module_type): """ Return the folder from the resources and given a module type :param resources: Resource object :type resources: Resources :param module_type: type of the module :return: path of the folder """ # dict to get the path behind a type of resource module_type_converter = { TYPE_NEURON: resources.neuron_folder, TYPE_STT: resources.stt_folder, TYPE_TTS: resources.tts_folder, TYPE_TRIGGER: resources.trigger_folder } # Let's find the right path depending of the type try: folder_path = module_type_converter[module_type] except KeyError: folder_path = None # No folder_path has been found message = "No %s folder set in settings, cannot install." % module_type if folder_path is None: logger.debug(message) Utils.print_danger(message) return folder_path
def _get_target_folder(resources, module_type): """ Return the folder from the resources and given a module type :param resources: Resource object :type resources: Resources :param module_type: type of the module (TYPE_NEURON, TYPE_STT, TYPE_TTS, TYPE_TRIGGER, TYPE_SIGNAL) :return: path of the folder """ module_type_converter = dict() # dict to get the path behind a type of resource try: module_type_converter = { TYPE_NEURON: resources.neuron_folder, TYPE_STT: resources.stt_folder, TYPE_TTS: resources.tts_folder, TYPE_TRIGGER: resources.trigger_folder, TYPE_SIGNAL: resources.signal_folder } except AttributeError: # will be raised if the resource folder is not set in settings pass # Let's find the right path depending of the type try: folder_path = module_type_converter[module_type] except KeyError: folder_path = None # No folder_path has been found message = "No %s folder set in settings." % module_type if folder_path is None: logger.debug(message) Utils.print_danger(message) return folder_path
def is_repo_ok(dna_file_path, install_file_path): """ Check if the git cloned repo is fine to be installed :return: True if repo is ok to be installed, False otherwise """ Utils.print_info("Checking repository...") repo_ok = True # check that a install.yml file is present if not os.path.exists(install_file_path): Utils.print_danger("Missing %s file" % INSTALL_FILE_NAME) repo_ok = False if not os.path.exists(dna_file_path): Utils.print_danger("Missing %s file" % DNA_FILE_NAME) repo_ok = False return repo_ok
def install(self): """ Module installation method. """ # first, we clone the repo self._clone_repo(path=self.tmp_path, git_url=self.git_url) # check the content of the cloned repo if self.is_repo_ok(dna_file_path=self.dna_file_path, install_file_path=self.install_file_path): # Load the dna.yml file self.dna = DnaLoader(self.dna_file_path).get_dna() if self.dna is not None: logger.debug("[ResourcesManager] DNA file content: " + str(self.dna)) if self.is_settings_ok(resources=self.settings.resources, dna=self.dna): # the dna file is ok, check the supported version if self._check_supported_version(current_version=self.settings.kalliope_version, supported_versions=self.dna.kalliope_supported_version): # Let's find the target folder depending the type module_type = self.dna.module_type.lower() target_folder = self._get_target_folder(resources=self.settings.resources, module_type=module_type) if target_folder is not None: # let's move the tmp folder in the right folder and get a new path for the module module_name = self.dna.name.lower() target_path = self._rename_temp_folder(name=self.dna.name.lower(), target_folder=target_folder, tmp_path=self.tmp_path) # if the target_path exists, then run the install file within the new repository if target_path is not None: self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME if self.run_ansible_playbook_module(install_file_path=self.install_file_path): Utils.print_success("Module: %s installed" % module_name) else: Utils.print_danger("Module: %s not installed" % module_name) else: logger.debug("[ResourcesManager] installation cancelled, deleting temp repo %s" % str(self.tmp_path)) shutil.rmtree(self.tmp_path)
def is_settings_ok(resources, dna): """ Test if required settings files in config of Kalliope are ok. The resource object must not be empty Check id the use have set the an installation path in his settings for the target module type :param resources: the Resources model :param dna: DNA info about the module to install :return: """ settings_ok = True if resources is None: message = "Resources folder not set in settings, cannot install." logger.debug(message) Utils.print_danger(message) settings_ok = False else: if dna.module_type == "neuron" and resources.neuron_folder is None: message = "Resources folder for neuron installation not set in settings, cannot install." logger.debug(message) Utils.print_danger(message) settings_ok = False if dna.module_type == "stt" and resources.stt_folder is None: message = "Resources folder for stt installation not set in settings, cannot install." logger.debug(message) Utils.print_danger(message) settings_ok = False if dna.module_type == "tts" and resources.tts_folder is None: message = "Resources folder for tts installation not set in settings, cannot install." logger.debug(message) Utils.print_danger(message) settings_ok = False if dna.module_type == "trigger" and resources.trigger_folder is None: message = "Resources folder for trigger installation not set in settings, cannot install." logger.debug(message) Utils.print_danger(message) settings_ok = False return settings_ok
def _start_neuron(cls, neuron, params): """ Associate params and Starts a neuron. :param neuron: the neuron to start :param params: the params to check and associate to the neuron args. """ problem_in_neuron_found = False if isinstance(neuron.parameters, dict): # print neuron.parameters if "args" in neuron.parameters: logger.debug("The neuron waits for parameter") # check that the user added parameters to his order if params is None: # we don't raise an error and break the program but we don't run the neuron problem_in_neuron_found = True Utils.print_danger( "Error: The neuron %s is waiting for argument. " "Argument found in bracket in the given order" % neuron.name) else: # we add wanted arguments the existing neuron parameter dict for arg in neuron.parameters["args"]: if arg in params: logger.debug( "Parameter %s added to the current parameter " "of the neuron: %s" % (arg, neuron.name)) neuron.parameters[arg] = params[arg] else: # we don't raise an error and break the program but # we don't run the neuron problem_in_neuron_found = True Utils.print_danger( "Error: Argument \"%s\" not found in the" " order" % arg) # if no error detected, we run the neuron if not problem_in_neuron_found: NeuroneLauncher.start_neurone(neuron) else: Utils.print_danger("A problem has been found in the Synapse.")
def install(self, force=False): """ Module installation method. :arg force: True to skip the version compatibility :return Dna object if resource installed """ # first, we clone the repo self._clone_repo(path=self.tmp_path, git_url=self.git_url) # check the content of the cloned repo if not self.is_repo_ok(dna_file_path=self.dna_file_path, install_file_path=self.install_file_path): logger.debug("[ResourcesManager] Invalid resource repository") self.cleanup_after_failed_installation() raise ResourcesManagerException("Invalid resource repository") # Load the dna.yml file self.dna = DnaLoader(self.dna_file_path).get_dna() if self.dna is None: logger.debug( "[ResourcesManager] No DNA file found in the resource to install" ) self.cleanup_after_failed_installation() raise ResourcesManagerException( "No DNA file found in the resource to install") logger.debug("[ResourcesManager] DNA file content: " + str(self.dna)) if not self.is_settings_ok(resources=self.settings.resources, dna=self.dna): logger.debug("[ResourcesManager] Invalid settings") self.cleanup_after_failed_installation() raise ResourcesManagerException("Invalid settings") # the dna file is ok, check the supported version if not force: if not self._check_supported_version( current_version=self.settings.kalliope_version, supported_versions=self.dna.kalliope_supported_version): logger.debug("[ResourcesManager] Non supported version") self.cleanup_after_failed_installation() raise ResourcesManagerException("Non supported version") # Let's find the target folder depending the type module_type = self.dna.module_type.lower() target_folder = self._get_target_folder( resources=self.settings.resources, module_type=module_type) if target_folder is None: self.cleanup_after_failed_installation() raise ResourcesManagerException( "No resource folder set in settings") # let's move the tmp folder in the right folder and get a new path for the module module_name = self.dna.name.lower() target_path = self._rename_temp_folder(name=self.dna.name.lower(), target_folder=target_folder, tmp_path=self.tmp_path) # if the target_path exists, then run the install file within the new repository if target_path is None: raise ResourcesManagerException("Resource already present") else: self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME if self.run_ansible_playbook_module( install_file_path=self.install_file_path): Utils.print_success("Module: %s installed" % module_name) return self.dna else: Utils.print_danger("Module: %s not installed" % module_name) return None