Esempio n. 1
0
def update_conf(new_conf: TypedConfigParser) -> None:
    """
    Checks the custom conf for anything to update and updates it accordingly
    :param new_conf: the new configuration to create
    """
    old_conf = get_global_conf()
    changed_conf = TypedConfigParser(interpolation=ExtendedInterpolation())

    for section in new_conf.sections():
        differences = set(new_conf.items(section)) ^ set(
            old_conf.items(section))
        modifications = differences & set(new_conf.items(section))
        if modifications:
            if section not in changed_conf.sections():
                changed_conf.add_section(section)

            for option, value in modifications:
                if TypedConfigParser.LIST_SEPARATOR in value:
                    old_value = set(old_conf.getlist(section, option))
                    new_value = set(new_conf.getlist(section, option))
                    if not old_value ^ new_value:
                        continue

                changed_conf.set(section, option, value)

    # cleanup empty added sections, not to save a new custom conf for nothing
    for section in changed_conf.sections():
        if len(changed_conf.items(section)) == 0:
            changed_conf.remove_section(section)

    if len(changed_conf.sections()):
        custom_conf = TypedConfigParser(interpolation=ExtendedInterpolation())
        custom_conf.read(os.path.join(constants.CONF_PATH, "custom.conf"))

        custom_conf.read_dict(changed_conf)

        try:
            with open(os.path.join(constants.CONF_PATH, "custom.conf"),
                      "w") as config:
                custom_conf.write(config)
        except PermissionError:
            values_to_add = io.StringIO()
            changed_conf.write(values_to_add)
            logging.error(
                "Could not update the config file : Permission Denied.\nPlease add to conf/custom.conf :\n\n%(entry)s",
                dict(entry=values_to_add.getvalue()))
            return 1
Esempio n. 2
0
def update() -> None:
    """
    Asks multiple questions to user to update the configuration then update the custom.conf file
    """
    new_config = TypedConfigParser(interpolation=ExtendedInterpolation())
    new_config.read(os.path.join(constants.CONF_PATH, "default.conf"))
    new_config.read(os.path.join(constants.CONF_PATH, "custom.conf"))

    new_config.set("DEFAULT", "show_progress", show_progress())
    new_config.set("install", "llvm_bitcode", choose_wllvm())
    new_config.set("install", "module_handling", choose_module_handling())
    new_config.set("install", "git_protocol", choose_git_protocol())
    new_config.set("trigger", "core_dump_pattern", choose_coredump_pattern())
    new_config.set("trigger", "core_dump_filter", choose_coredump_filter())
    new_config.set("trigger", "core_dump_location", choose_coredump_location())
    new_config.set("install", "build_directory", choose_build_dir())
    new_config.set("install", "install_directory", choose_install_dir())
    new_config.set("install", "source_directory", choose_source_storage_dir())
    new_config.set(
        "install", "make_args",
        TypedConfigParser.LIST_SEPARATOR.join(choose_make_arguments()))
    new_config.set(
        "plugins", "repositories",
        TypedConfigParser.LIST_SEPARATOR.join(
            choose_plugin_repositories(
                new_config.get("install", "git_protocol"))))
    new_config.set("install", "compiler", choose_compiler())
    new_config.set(
        "plugins", "enabled_plugins",
        TypedConfigParser.LIST_SEPARATOR.join(choose_plugins_to_enable()))

    if not update_conf(new_config):
        reload_global_conf()
    else:
        return 1