From 9faaedf7151eb7598e4750c07bb6c3801336808b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 23 Oct 2022 14:44:44 +0000 Subject: [PATCH] arch: Add function that returns a list of all supported arches Signed-off-by: Michael Tremer --- src/_pakfire/_pakfiremodule.c | 39 +++++++++++++++++++++++++++ src/libpakfire/arch.c | 25 +++++++++++++++++ src/libpakfire/include/pakfire/arch.h | 1 + src/libpakfire/libpakfire.sym | 1 + src/pakfire/__init__.py | 2 +- 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index 6fadebaec..839d0ac76 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -52,9 +52,48 @@ static PyObject* _pakfire_native_arch(void) { return PyUnicode_FromString(arch); } +static PyObject* _pakfire_supported_arches(void) { + int r; + + // Fetch all architectures + const char** arches = pakfire_supported_arches(); + if (!arches) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + + // Create a new list + PyObject* list = PyList_New(0); + if (!list) + return NULL; + + // Append all architectures + for (const char** arch = arches; *arch; arch++) { + PyObject* object = PyUnicode_FromString(*arch); + if (!object) + goto ERROR; + + // Append to list + r = PyList_Append(list, object); + if (r) { + Py_DECREF(object); + goto ERROR; + } + + Py_DECREF(object); + } + + return list; + +ERROR: + Py_DECREF(list); + return NULL; +} + static PyMethodDef pakfireModuleMethods[] = { {"performance_index", (PyCFunction)performance_index, METH_VARARGS, NULL}, {"native_arch", (PyCFunction)_pakfire_native_arch, METH_NOARGS, NULL }, + {"supported_arches", (PyCFunction)_pakfire_supported_arches, METH_NOARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/src/libpakfire/arch.c b/src/libpakfire/arch.c index a11bdd8a2..2d28b1594 100644 --- a/src/libpakfire/arch.c +++ b/src/libpakfire/arch.c @@ -131,6 +131,31 @@ PAKFIRE_EXPORT int pakfire_arch_supported(const char* name) { return 0; } +const char** __pakfire_supported_arches = NULL; + +PAKFIRE_EXPORT const char** pakfire_supported_arches() { + unsigned int counter = 0; + + if (!__pakfire_supported_arches) { + for (const struct pakfire_arch* arch = PAKFIRE_ARCHES; arch->name; arch++) { + __pakfire_supported_arches = reallocarray(__pakfire_supported_arches, + counter + 1, sizeof(*__pakfire_supported_arches)); + + // Exit if the allocation failed + if (!__pakfire_supported_arches) + return NULL; + + __pakfire_supported_arches[counter++] = arch->name; + } + + // Sentinel + if (__pakfire_supported_arches) + __pakfire_supported_arches[counter] = NULL; + } + + return __pakfire_supported_arches; +} + const char* pakfire_arch_platform(const char* name) { const struct pakfire_arch* arch = pakfire_arch_find(name); diff --git a/src/libpakfire/include/pakfire/arch.h b/src/libpakfire/include/pakfire/arch.h index c1ffa130f..268c1b68b 100644 --- a/src/libpakfire/include/pakfire/arch.h +++ b/src/libpakfire/include/pakfire/arch.h @@ -22,6 +22,7 @@ #define PAKFIRE_ARCH_H int pakfire_arch_supported(const char* name); +const char** pakfire_supported_arches(void); const char* pakfire_arch_native(void); #ifdef PAKFIRE_PRIVATE diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 4d472743f..a433327d5 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -48,6 +48,7 @@ global: # arch pakfire_arch_native; pakfire_arch_supported; + pakfire_supported_arches; # archive pakfire_archive_extract; diff --git a/src/pakfire/__init__.py b/src/pakfire/__init__.py index 8b281b0fc..ebe61a268 100644 --- a/src/pakfire/__init__.py +++ b/src/pakfire/__init__.py @@ -19,7 +19,7 @@ # # ############################################################################### -from ._pakfire import Pakfire +from ._pakfire import Pakfire, native_arch, supported_arches from .__version__ import PAKFIRE_VERSION as __version__ -- 2.39.5