From: Michael Tremer Date: Mon, 22 Jan 2018 16:21:29 +0000 (+0100) Subject: libpakfire: Automatically determine the repository priority X-Git-Tag: 0.9.28~1285^2~1137 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5c4095960a2c4dc41093643f39330eab595acd27;p=pakfire.git libpakfire: Automatically determine the repository priority Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index b9318d59a..df32e5bd1 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -36,6 +36,7 @@ void* pakfire_realloc(void* ptr, size_t size); void* pakfire_free(void* mem); char* pakfire_strdup(const char* s); +int pakfire_string_startswith(const char* s, const char* prefix); char* pakfire_format_size(double size); char* pakfire_format_date(time_t t); diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index be797754f..3498c78f6 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -206,8 +206,35 @@ PAKFIRE_EXPORT void pakfire_repo_set_enabled(PakfireRepo repo, int enabled) { pakfire_pool_has_changed(repo->pakfire); } +// Returns a default priority based on the repository configuration +static int pakfire_repo_auto_priority(PakfireRepo repo) { + // The @system repository has a priority of zero + if (pakfire_repo_is_installed_repo(repo) == 0) + return 0; + + if (repo->appdata->baseurl) { + // HTTPS + if (pakfire_string_startswith(repo->appdata->baseurl, "https://")) + return 75; + + // HTTP + if (pakfire_string_startswith(repo->appdata->baseurl, "http://")) + return 75; + + // Local path + if (pakfire_string_startswith(repo->appdata->baseurl, "dir://")) + return 50; + } + + // Default to 100 + return 100; +} + PAKFIRE_EXPORT int pakfire_repo_get_priority(PakfireRepo repo) { - return repo->repo->priority; + if (repo->repo->priority > 0) + return repo->repo->priority; + + return pakfire_repo_auto_priority(repo); } PAKFIRE_EXPORT void pakfire_repo_set_priority(PakfireRepo repo, int priority) { diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 6b7c5218d..df1a74753 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -95,6 +95,10 @@ char* pakfire_strdup(const char* s) { return r; } +int pakfire_string_startswith(const char* s, const char* prefix) { + return strncmp(s, prefix, strlen(prefix)); +} + char* pakfire_format_size(double size) { char string[STRING_SIZE]; const char* units[] = {" ", "k", "M", "G", "T", NULL}; diff --git a/src/pakfire/repository/remote.py b/src/pakfire/repository/remote.py index 9b52f6a28..416884af6 100644 --- a/src/pakfire/repository/remote.py +++ b/src/pakfire/repository/remote.py @@ -58,34 +58,6 @@ class RepositoryRemote(base.RepositoryFactory): return keyfile - @property - def priority(self): - priority = self.settings.get("priority", None) - if not priority is None: - # Try to concert the given input to an integer - # and return the value if possible. - try: - priority = int(priority) - return priority - - except ValueError: - pass - - # The default priority is 100. - priority = 100 - - url2priority = { - "file://" : 50, - "http://" : 75, - } - - for url, prio in list(url2priority.items()): - if self.baseurl.startswith(url): - priority = prio - break - - return priority - def make_downloader(self): """ Creates a downloader that can be used to download