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
    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)
Beispiel #3
0
    def _try_action_on_config_or_resources(self, action, package, module_name,
                                           is_url, package_localisation):
        parsed_module = TentacleUtil.parse_module_header(package[module_name])

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

        if parsed_module[TENTACLE_MODULE_CONFIG_FILES]:
            for config_file in parsed_module[TENTACLE_MODULE_CONFIG_FILES]:
                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)

                self._try_action_on_config_or_resource_file(
                    action,
                    module_name,
                    package_localisation,
                    is_url,
                    parsed_module,
                    config_file,
                    config_file_path,
                    default_file_path=default_config_file_path)

        if parsed_module[TENTACLE_MODULE_RESOURCE_FILES]:
            for res_file in parsed_module[TENTACLE_MODULE_RESOURCE_FILES]:
                res_file_path = "{0}{1}/{2}".format(file_dir,
                                                    EVALUATOR_RESOURCE_FOLDER,
                                                    res_file)

                self._try_action_on_config_or_resource_file(
                    action,
                    module_name,
                    package_localisation,
                    is_url,
                    parsed_module,
                    res_file,
                    res_file_path,
                    read_as_bytes=True)
Beispiel #4
0
    def _try_action_on_requirements(self, action, package, module_name,
                                    package_name):
        parsed_module = TentacleUtil.parse_module_header(package[module_name])
        success = True
        module_name = parsed_module[TENTACLE_MODULE_NAME]
        applied_modules = [module_name]
        if parsed_module[TENTACLE_MODULE_REQUIREMENTS]:
            for requirement_data in parsed_module[
                    TENTACLE_MODULE_REQUIREMENTS]:
                if success:
                    requirement_module_name = requirement_data[
                        TENTACLE_MODULE_NAME]
                    requirement_module_version = requirement_data[
                        TENTACLE_MODULE_VERSION]
                    if not self._has_just_processed_module(requirement_module_name, requirement_module_version) and \
                            self._should_do_something(action, requirement_module_name,
                                                      requirement_module_version, True, module_name):
                        try:
                            req_package, _, localisation, is_url, destination, _ = self.tentacle_manager. \
                                get_package_in_lists(requirement_module_name, requirement_module_version)

                            if req_package:
                                self.process_module(action, req_package,
                                                    requirement_module_name,
                                                    localisation, is_url,
                                                    destination, package_name)
                                applied_modules.append(requirement_module_name)
                            else:
                                raise Exception(
                                    "module requirement '{0}' not found in package lists"
                                    .format(requirement_data[
                                        TENTACLE_MODULE_REQUIREMENT_WITH_VERSION]
                                            ))

                        except Exception as e:
                            error = "failed for tentacle module requirement '{0}' of module {1} ({2})" \
                                .format(requirement_module_name, module_name, e)
                            if action == TentacleManagerActions.INSTALL:
                                self.logger.error(
                                    "installation {0}".format(error))
                            elif action == TentacleManagerActions.UNINSTALL:
                                self.logger.error(
                                    "uninstalling {0}".format(error))
                            elif action == TentacleManagerActions.UPDATE:
                                self.logger.error("updating {0}".format(error))
                            success = False

            # failed to install requirements
            if not success:
                if action == TentacleManagerActions.UPDATE:
                    # uninstall module
                    req_package, _, localisation, is_url, destination, _ = self.tentacle_manager. \
                        get_package_in_lists(module_name)
                    if req_package:
                        self.process_module(TentacleManagerActions.UNINSTALL,
                                            req_package, module_name,
                                            localisation, is_url, destination,
                                            package_name)

                elif action == TentacleManagerActions.INSTALL:
                    # uninstall module and requirements
                    for module_to_remove in applied_modules:
                        req_package, _, localisation, is_url, destination, _ = self.tentacle_manager. \
                            get_package_in_lists(module_to_remove)
                        if req_package:
                            self.process_module(
                                TentacleManagerActions.UNINSTALL, req_package,
                                module_to_remove, localisation, is_url,
                                destination, package_name)
Beispiel #5
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))