From: Michael Tremer Date: Fri, 16 Apr 2021 16:33:02 +0000 (+0000) Subject: pakfire: Load repositories from configuration X-Git-Tag: 0.9.28~1285^2~340 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5db84e92562f588d8875ccdc0461e5ad123fb489;p=pakfire.git pakfire: Load repositories from configuration Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index cf3b493e2..4e43dcdd1 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -91,8 +91,11 @@ int pakfire_repo_refresh(PakfireRepo repo, const int force); #include +#include #include +int pakfire_repo_import(Pakfire pakfire, struct pakfire_config* config); + Id pakfire_repo_add_solvable(PakfireRepo repo); PakfireRepo pakfire_repo_create_from_repo(Pakfire pakfire, Repo* r); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 1d6fa2745..8ae3e1e82 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -744,6 +744,11 @@ PAKFIRE_EXPORT int pakfire_create( if (r) goto ERROR; + // Create repositories + r = pakfire_repo_import(p, p->config); + if (r) + goto ERROR; + *pakfire = p; return 0; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index b74998247..1cfe68619 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -77,6 +78,70 @@ struct _PakfireRepo { struct pakfire_downloader* downloader; }; +int pakfire_repo_import(Pakfire pakfire, struct pakfire_config* config) { + char** sections = pakfire_config_sections(config); + + // The configuration seems to be empty + if (!sections) + return 0; + + int r = 0; + + // Walk through all sections + for (char** section = sections; *section; section++) { + // Skip sections that do not start with "repo:" + if (!pakfire_string_startswith(*section, "repo:")) + continue; + + const char* name = (*section) + strlen("repo:"); + if (!*name) + continue; + + DEBUG(pakfire, "Creating repository %s\n", name); + + // Create a new repository + PakfireRepo repo = pakfire_repo_create(pakfire, name); + if (!repo) { + ERROR(pakfire, "Could not create repository '%s': %s\n", name, strerror(errno)); + r = 1; + goto ERROR; + } + + // Enabled + int enabled = pakfire_config_get_bool(config, *section, "enabled", 1); + pakfire_repo_set_enabled(repo, enabled); + + // Priority + int priority = pakfire_config_get_int(config, *section, "priority", 0); + if (priority) + pakfire_repo_set_priority(repo, priority); + + // Description + const char* description = pakfire_config_get(config, *section, "description", NULL); + if (description) + pakfire_repo_set_description(repo, description); + + // baseurl + const char* baseurl = pakfire_config_get(config, *section, "baseurl", NULL); + if (baseurl) + pakfire_repo_set_baseurl(repo, baseurl); + + // mirrors + const char* mirrors = pakfire_config_get(config, *section, "mirrors", NULL); + if (mirrors) + pakfire_repo_set_mirrorlist_url(repo, mirrors); + + pakfire_repo_unref(repo); + } + +ERROR: + for (char** section = sections; *section; section++) + free(*section); + free(sections); + + return r; +} + Id pakfire_repo_add_solvable(PakfireRepo repo) { return repo_add_solvable(repo->repo); } diff --git a/src/pakfire/base.py b/src/pakfire/base.py index 435dfe569..93cdb06be 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -54,11 +54,6 @@ class Pakfire(_pakfire.Pakfire): self.repos = repository.Repositories(self) - # Load default repository configuration - repos_dir = self.make_path(CONFIG_REPOS_DIR) - if repos_dir: - self.repos.load_configuration(repos_dir) - def _setup_logger(self): log = logging.getLogger("pakfire") log.propagate = 0 diff --git a/src/pakfire/constants.py b/src/pakfire/constants.py index fd50934d9..56ffaf396 100644 --- a/src/pakfire/constants.py +++ b/src/pakfire/constants.py @@ -31,7 +31,6 @@ PAKFIRE_HUB = "https://pakfirehub.ipfire.org/" SYSCONFDIR = "/etc" CONFIG_DIR = os.path.join(SYSCONFDIR, "pakfire") -CONFIG_REPOS_DIR = os.path.join(CONFIG_DIR, "repos") CONFIG_DISTRO_DIR = os.path.join(CONFIG_DIR, "distros") CACHE_DIR = "/var/cache/pakfire" diff --git a/src/pakfire/repository/__init__.py b/src/pakfire/repository/__init__.py index eafbade5e..bd9982c18 100644 --- a/src/pakfire/repository/__init__.py +++ b/src/pakfire/repository/__init__.py @@ -19,18 +19,9 @@ # # ############################################################################### -import glob -import os -import re - import logging log = logging.getLogger("pakfire") -from .. import _pakfire -from .. import config - -from ..i18n import _ - class Repositories(object): """ Class that loads all repositories from the configuration files. @@ -44,8 +35,6 @@ class Repositories(object): # Place to store the repositories self.__repos = {} - self._load_from_configuration(self.pakfire.config) - def __iter__(self): repositories = list(self.__repos.values()) repositories.sort() @@ -62,84 +51,6 @@ class Repositories(object): def distro(self): return self.pakfire.distro - def load_configuration(self, *paths): - c = config.Config() - - for path in paths: - # Read directories - if os.path.isdir(path): - for file in glob.glob("%s/*.repo" % path): - c.read(file) - - # Read files - else: - c.read(path) - - self._load_from_configuration(c) - - def _load_from_configuration(self, config): - # Add all repositories that have been found - for name, settings in config.get_repos(): - self._parse(name, settings) - - def _parse(self, name, args): - _args = { - "name" : name, - "enabled" : True, - "gpgkey" : None, - "mirrors" : None, - } - _args.update(args) - - # Handle variable expansion. - replaces = { - "name" : name, - "arch" : "%s" % self.pakfire.arch, - } - - for k, v in list(_args.items()): - # Skip all non-strings. - if not type(v) == type("a"): - continue - - while True: - m = re.search(r"\%\{([A-Za-z0-9_\-]+)\}", v) - - # If we cannot find a match, we are done. - if not m: - _args[k] = v - break - - # Get the name of the variable. - (var,) = m.groups() - - # Replace the variable with its value. - v = v.replace("%%{%s}" % var, replaces.get(var, "")) - - repo = _pakfire.Repo(self.pakfire, name) - repo.enabled = _args.get("enabled", True) - repo.description = _args.get("description", None) - repo.baseurl = _args.get("baseurl", None) - repo.keyfile = _args.get("gpgkey", None) - repo.mirrorlist = _args.get("mirrors", None) - - self.add_repo(repo) - - def add_repo(self, repo): - if repo.name in self.__repos: - raise Exception("Repository with that name does already exist: %s" % repo.name) - - self.__repos[repo.name] = repo - - def rem_repo(self, repo): - """ - Remove the given repository from the global repository set. - """ - try: - del self.__repos[repo.name] - except KeyError: - log.debug("Repository that was to be removed does not exist: %s" % repo.name) - def get_repo(self, name): """ Get the repository with the given name, if not available, return @@ -148,7 +59,7 @@ class Repositories(object): try: return self.__repos[name] except KeyError: - return self.dummy + pass def clean(self): log.info("Cleaning up all repository caches...")