def process_module(self, action, package, module_name,
                       package_localisation, is_url, target_folder):
        parsed_module = TentacleUtil.parse_module_header(package[module_name])
        module_type = parsed_module[TENTACLE_MODULE_TYPE]
        module_subtype = parsed_module[TENTACLE_MODULE_SUBTYPE]
        module_tests = parsed_module[TENTACLE_MODULE_TESTS]
        module_file_content = ""
        module_test_files = {test: ""
                             for test in module_tests} if module_tests else {}

        if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:
            module_loc = "{0}.py".format(
                TentacleUtil.create_localization_from_type(
                    package_localisation, module_type, module_subtype,
                    module_name))

            if is_url:
                module_file_content = TentaclePackageUtil.get_package_file_content_from_url(
                    module_loc)
            else:
                with open(module_loc, "r") as module_file:
                    module_file_content = module_file.read()

            if module_test_files:
                for test in module_tests:
                    test_loc = "{0}.py".format(
                        TentacleUtil.create_localization_from_type(
                            package_localisation, module_type, module_subtype,
                            test, True))

                    if is_url:
                        module_test_files[
                            test] = TentaclePackageUtil.get_package_file_content_from_url(
                                test_loc)
                    else:
                        with open(test_loc, "r") as module_file:
                            module_test_files[test] = module_file.read()

        if self._process_action_on_module(
                action, module_type, module_subtype,
                parsed_module[TENTACLE_MODULE_VERSION], module_file_content,
                module_test_files, target_folder, module_name):
            # manage module config
            self._try_action_on_config(action, package, module_name, is_url,
                                       package_localisation)

        if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:
            self._try_action_on_requirements(action, package, module_name)
    def _try_action_on_config(self, action, package, module_name, is_url,
                              package_localisation):
        parsed_module = TentacleUtil.parse_module_header(package[module_name])

        if parsed_module[TENTACLE_MODULE_CONFIG_FILES]:
            for config_file in parsed_module[TENTACLE_MODULE_CONFIG_FILES]:

                file_dir = TentacleUtil.create_path_from_type(
                    parsed_module[TENTACLE_MODULE_TYPE],
                    parsed_module[TENTACLE_MODULE_SUBTYPE], "")

                config_file_path = "{0}{1}/{2}".format(
                    file_dir, EVALUATOR_CONFIG_FOLDER, config_file)
                default_config_file_path = "{0}{1}/{2}/{3}".format(
                    file_dir, EVALUATOR_CONFIG_FOLDER,
                    EVALUATOR_DEFAULT_FOLDER, config_file)
                if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:

                    try:
                        # get config file content from localization
                        module_loc = TentacleUtil.create_localization_from_type(
                            package_localisation,
                            parsed_module[TENTACLE_MODULE_TYPE],
                            parsed_module[TENTACLE_MODULE_SUBTYPE],
                            config_file)

                        if is_url:
                            config_file_content = TentaclePackageUtil.get_package_file_content_from_url(
                                module_loc)
                        else:
                            with open(module_loc, "r") as module_file:
                                config_file_content = module_file.read()

                        # install local config file content
                        if action == TentacleManagerActions.INSTALL:
                            with open(config_file_path,
                                      "w") as new_config_file:
                                new_config_file.write(config_file_content)
                        with open(default_config_file_path,
                                  "w") as new_default_config_file:
                            new_default_config_file.write(config_file_content)

                        if action == TentacleManagerActions.UPDATE:
                            self.logger.info(
                                "{0} configuration file for {1} module ignored to save the current "
                                "configuration. The default configuration file has been updated in: {2}."
                                .format(config_file, module_name,
                                        default_config_file_path))

                    except Exception as e:
                        raise Exception(
                            "Fail to install configuration : {}".format(e))

                elif action == TentacleManagerActions.UNINSTALL:
                    try:
                        os.remove(config_file_path)
                        os.remove(default_config_file_path)
                    except OSError:
                        pass
Example #3
0
    def _try_action_on_config_or_resource_file(self,
                                               action,
                                               module_name,
                                               package_localisation,
                                               is_url,
                                               parsed_module,
                                               file,
                                               file_path,
                                               default_file_path=None,
                                               read_as_bytes=False):

        if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:

            try:
                # get config file content from localization
                module_loc = TentacleUtil.create_localization_from_type(
                    package_localisation, parsed_module[TENTACLE_MODULE_TYPE],
                    parsed_module[TENTACLE_MODULE_SUBTYPE], file)

                if is_url:
                    config_file_content = TentaclePackageUtil.get_package_file_content_from_url(
                        module_loc, as_bytes=read_as_bytes)
                else:
                    with open(module_loc,
                              "rb" if read_as_bytes else "r") as module_file:
                        config_file_content = module_file.read()

                # install local file content
                if action == TentacleManagerActions.INSTALL:
                    with open(file_path,
                              "wb" if read_as_bytes else "w") as new_file:
                        new_file.write(config_file_content)

                # copy into default
                if default_file_path:
                    with open(default_file_path, "wb"
                              if read_as_bytes else "w") as new_default_file:
                        new_default_file.write(config_file_content)

                    if action == TentacleManagerActions.UPDATE:
                        self.logger.info(
                            "{0} configuration / resource file for {1} module ignored to save the current "
                            "configuration. The default configuration file has been updated in: {2}."
                            .format(file, module_name, default_file_path))

            except Exception as e:
                raise Exception(
                    "Fail to install configuration / resource : {}".format(e))

        elif action == TentacleManagerActions.UNINSTALL:
            try:
                os.remove(file_path)

                if default_file_path:
                    os.remove(default_file_path)
            except OSError:
                pass
Example #4
0
    def process_module(self, action, package, module_name,
                       package_localisation, is_url, target_folder,
                       package_name):
        parsed_module = TentacleUtil.parse_module_header(package[module_name])
        module_type = parsed_module[TENTACLE_MODULE_TYPE]
        module_subtype = parsed_module[TENTACLE_MODULE_SUBTYPE]
        module_tests = parsed_module[TENTACLE_MODULE_TESTS]
        module_file_content = ""
        module_dev = parsed_module[TENTACLE_MODULE_DEV]
        module_test_files = {test: ""
                             for test in module_tests} if module_tests else {}

        if TentacleUtil.install_on_development(self.config, module_dev):
            if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:
                module_loc = "{0}.py".format(
                    TentacleUtil.create_localization_from_type(
                        package_localisation, module_type, module_subtype,
                        module_name))

                if is_url:
                    module_file_content = TentaclePackageUtil.get_package_file_content_from_url(
                        module_loc)
                else:
                    with open(module_loc, "r") as module_file:
                        module_file_content = module_file.read()

                module_file_content = TentaclePackageUtil.add_package_name(
                    module_file_content, package_name)

                if module_test_files:
                    for test in module_tests:
                        test_loc = "{0}.py".format(
                            TentacleUtil.create_localization_from_type(
                                package_localisation, module_type,
                                module_subtype, test, True))

                        if is_url:
                            module_test_files[
                                test] = TentaclePackageUtil.get_package_file_content_from_url(
                                    test_loc)
                        else:
                            with open(test_loc, "r") as module_file:
                                module_test_files[test] = module_file.read()

            if self._process_action_on_module(
                    action, module_type, module_subtype,
                    parsed_module[TENTACLE_MODULE_VERSION],
                    module_file_content, module_test_files, target_folder,
                    module_name):
                # manage module config
                self._try_action_on_config_or_resources(
                    action, package, module_name, is_url, package_localisation)

            if action == TentacleManagerActions.INSTALL or action == TentacleManagerActions.UPDATE:
                self._try_action_on_requirements(action, package, module_name,
                                                 package_name)
        else:
            self.logger.warning(
                "{0} is currently on development, "
                "it will not be installed (to install it anyway, "
                "add \"DEV-MODE\": true in your config.json)".format(
                    module_name))