From: Michael Tremer Date: Mon, 19 Apr 2021 14:05:39 +0000 (+0000) Subject: libpakfire: Export repositories to Python X-Git-Tag: 0.9.28~1285^2~337 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=78cc880011c13cd7683ebb428c0c27d8dc5586a7;p=pakfire.git libpakfire: Export repositories to Python Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 848861e85..59f99b647 100644 --- a/Makefile.am +++ b/Makefile.am @@ -143,13 +143,6 @@ pakfire_packagesdir = $(pythondir)/pakfire/packages # ------------------------------------------------------------------------------ -pakfire_repository_PYTHON = \ - src/pakfire/repository/__init__.py - -pakfire_repositorydir = $(pythondir)/pakfire/repository - -# ------------------------------------------------------------------------------ - pakfire_ui_PYTHON = \ src/pakfire/ui/__init__.py \ src/pakfire/ui/base.py \ @@ -279,6 +272,7 @@ libpakfire_la_SOURCES = \ src/libpakfire/relation.c \ src/libpakfire/relationlist.c \ src/libpakfire/repo.c \ + src/libpakfire/repolist.c \ src/libpakfire/request.c \ src/libpakfire/scriptlet.c \ src/libpakfire/selector.c \ @@ -317,6 +311,7 @@ pkginclude_HEADERS += \ src/libpakfire/include/pakfire/relation.h \ src/libpakfire/include/pakfire/relationlist.h \ src/libpakfire/include/pakfire/repo.h \ + src/libpakfire/include/pakfire/repolist.h \ src/libpakfire/include/pakfire/request.h \ src/libpakfire/include/pakfire/scriptlet.h \ src/libpakfire/include/pakfire/selector.h \ diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 98e774a60..ad12e8a39 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -791,6 +792,36 @@ static PyObject* Pakfire_restore_snapshot(PakfireObject* self, PyObject* args) { Py_RETURN_NONE; } +static PyObject* Pakfire_get_repos(PakfireObject* self) { + struct pakfire_repolist* repos = pakfire_get_repos(self->pakfire); + if (!repos) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + + const size_t l = pakfire_repolist_size(repos); + + PyObject* list = PyList_New(l); + if (!list) + goto ERROR; + + for (unsigned int i = 0; i < l; i++) { + PakfireRepo repo = pakfire_repolist_get(repos, i); + if (!repo) + continue; + + PyObject* obj = new_repo(&RepoType, repo); + PyList_SET_ITEM(list, i, obj); + + pakfire_repo_unref(repo); + } + +ERROR: + pakfire_repolist_unref(repos); + + return list; +} + static struct PyMethodDef Pakfire_methods[] = { { "bind", @@ -946,6 +977,13 @@ static struct PyGetSetDef Pakfire_getsetters[] = { NULL, NULL }, + { + "repos", + (getter)Pakfire_get_repos, + NULL, + NULL, + NULL + }, { NULL }, }; diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 32ffbfa46..a81faf31e 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -27,6 +27,7 @@ #include #include +#include #include int pakfire_create(Pakfire* pakfire, const char* path, const char* arch, const char* conf); @@ -54,6 +55,7 @@ int pakfire_version_compare(Pakfire pakfire, const char* evr1, const char* evr2) size_t pakfire_count_packages(Pakfire pakfire); +struct pakfire_repolist* pakfire_get_repos(Pakfire pakfire); PakfireRepo pakfire_get_repo(Pakfire pakfire, const char* name); PakfireRepo pakfire_get_installed_repo(Pakfire pakfire); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 5181eac6c..9607d8c5b 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -36,6 +36,7 @@ global: pakfire_get_path; pakfire_get_pool; pakfire_get_repo; + pakfire_get_repos; pakfire_make_cache_path; pakfire_make_path; pakfire_read_makefile; @@ -321,6 +322,16 @@ global: pakfire_repo_write_solv_fp; pakfire_repo_unref; + # repolist + pakfire_repolist_append; + pakfire_repolist_clear; + pakfire_repolist_create; + pakfire_repolist_empty; + pakfire_repolist_get; + pakfire_repolist_size; + pakfire_repolist_ref; + pakfire_repolist_unref; + # relation pakfire_relation_create; pakfire_relation_create_from_id; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 8ae3e1e82..1c308f518 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -970,6 +970,40 @@ void pakfire_pool_apply_changes(Pakfire pakfire) { } } +PAKFIRE_EXPORT struct pakfire_repolist* pakfire_get_repos(Pakfire pakfire) { + struct pakfire_repolist* list; + + int r = pakfire_repolist_create(&list); + if (r) + return NULL; + + Pool* pool = pakfire_get_solv_pool(pakfire); + Repo* solv_repo; + int i; + + FOR_REPOS(i, solv_repo) { + PakfireRepo repo = pakfire_repo_create_from_repo(pakfire, solv_repo); + if (!repo) { + r = 1; + goto ERROR; + } + + r = pakfire_repolist_append(list, repo); + if (r) { + pakfire_repo_unref(repo); + goto ERROR; + } + + pakfire_repo_unref(repo); + } + + return list; + +ERROR: + pakfire_repolist_unref(list); + return NULL; +} + PAKFIRE_EXPORT PakfireRepo pakfire_get_repo(Pakfire pakfire, const char* name) { Pool* pool = pakfire_get_solv_pool(pakfire); diff --git a/src/pakfire/base.py b/src/pakfire/base.py index 93cdb06be..17c5d3b11 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -28,7 +28,6 @@ from . import _pakfire from . import distro from . import logger from . import packages -from . import repository from . import util from .config import Config @@ -52,8 +51,6 @@ class Pakfire(_pakfire.Pakfire): # Load configuration self.config = config or Config("general.conf") - self.repos = repository.Repositories(self) - def _setup_logger(self): log = logging.getLogger("pakfire") log.propagate = 0 diff --git a/src/pakfire/repository/__init__.py b/src/pakfire/repository/__init__.py deleted file mode 100644 index bd9982c18..000000000 --- a/src/pakfire/repository/__init__.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/python -############################################################################### -# # -# Pakfire - The IPFire package management system # -# Copyright (C) 2011 Pakfire development team # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -############################################################################### - -import logging -log = logging.getLogger("pakfire") - -class Repositories(object): - """ - Class that loads all repositories from the configuration files. - - This is the place where repositories can be activated or deactivated. - """ - - def __init__(self, pakfire): - self.pakfire = pakfire - - # Place to store the repositories - self.__repos = {} - - def __iter__(self): - repositories = list(self.__repos.values()) - repositories.sort() - - return iter(repositories) - - def __len__(self): - """ - Return the count of enabled repositories. - """ - return len([r for r in self if r.enabled]) - - @property - def distro(self): - return self.pakfire.distro - - def get_repo(self, name): - """ - Get the repository with the given name, if not available, return - the dummy repository. - """ - try: - return self.__repos[name] - except KeyError: - pass - - def clean(self): - log.info("Cleaning up all repository caches...") - - for repo in self: - repo.clean()