]> git.ipfire.org Git - pakfire.git/commitdiff
python: Add function to generate list from filelist
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 13:58:04 +0000 (13:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 13:58:04 +0000 (13:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/archive.c
src/_pakfire/util.c
src/_pakfire/util.h

index 058a3297589ff29c6e01fe45472398a5c334a0e0..c0d5e71094b0490e5ab66839063b408c13d1de83 100644 (file)
 #include <Python.h>
 
 #include <pakfire/archive.h>
+#include <pakfire/filelist.h>
 #include <pakfire/package.h>
 #include <pakfire/repo.h>
 #include <pakfire/util.h>
 
 #include "archive.h"
 #include "errors.h"
-#include "file.h"
 #include "key.h"
 #include "package.h"
+#include "util.h"
 
 PyObject* new_archive(PyTypeObject* type, struct pakfire_archive* archive) {
        ArchiveObject* self = (ArchiveObject *)type->tp_alloc(type, 0);
@@ -204,8 +205,6 @@ static PyObject* Archive_get_path(ArchiveObject* self) {
 }
 
 static PyObject* Archive_get_filelist(ArchiveObject* self) {
-       int r;
-
        // Fetch the filelist
        struct pakfire_filelist* filelist = pakfire_archive_get_filelist(self->archive);
        if (!filelist) {
@@ -213,48 +212,13 @@ static PyObject* Archive_get_filelist(ArchiveObject* self) {
                return NULL;
        }
 
-       const size_t size = pakfire_filelist_size(filelist);
-
-       // Create a new Python list
-       PyObject* list = PyList_New(0);
-       if (!list)
-               goto ERROR;
-
-       for (unsigned int i = 0; i < size; i++) {
-               struct pakfire_file* file = pakfire_filelist_get(filelist, i);
-               if (!file)
-                       goto ERROR;
-
-               // Create a new File object
-               PyObject* obj = new_file(file);
-               if (!obj)
-                       goto ERROR;
-
-               // Append the new object to the list
-               r = PyList_Append(list, obj);
-               Py_DECREF(obj);
-
-               // If we could not append to the list, we will break
-               if (r)
-                       goto ERROR;
-
-               // Free file
-               pakfire_file_unref(file);
-       }
-
-       goto OUT;
+       // Convert to filelist
+       PyObject*  _filelist = PyList_FromFileList(filelist);
 
-ERROR:
-       if (list) {
-               Py_DECREF(list);
-               list = NULL;
-       }
-
-OUT:
-       if (filelist)
-               pakfire_filelist_unref(filelist);
+       // Cleanup
+       pakfire_filelist_unref(filelist);
 
-       return list;
+       return _filelist;
 }
 
 static struct PyMethodDef Archive_methods[] = {
index 7e24e6e06af799bc542a112e0f1e459efe4e78cd..e269cf09a29225dd1acf962d9effe59d1427f688 100644 (file)
 #include <time.h>
 #include <unistd.h>
 
+#include <pakfire/filelist.h>
 #include <pakfire/package.h>
 #include <pakfire/packagelist.h>
 
+#include "file.h"
 #include "package.h"
 #include "util.h"
 
@@ -98,3 +100,44 @@ PyObject* PyList_FromPackageList(struct pakfire_packagelist* packagelist) {
 
        return list;
 }
+
+PyObject* PyList_FromFileList(struct pakfire_filelist* filelist) {
+       struct pakfire_file* file = NULL;
+       PyObject* f = NULL;
+       int r;
+
+       PyObject* list = PyList_New(0);
+       if (!list)
+               goto ERROR;
+
+       // Fetch size
+       const size_t size = pakfire_filelist_size(filelist);
+
+       for (unsigned int i = 0; i < size; i++) {
+               file = pakfire_filelist_get(filelist, i);
+               if (!file)
+                       goto ERROR;
+
+               // Create a new File object
+               f = new_file(file);
+               if (!f)
+                       goto ERROR;
+
+               // Append the new object to the list
+               r = PyList_Append(list, f);
+               Py_DECREF(f);
+
+               // If we could not append to the list, we will break
+               if (r)
+                       goto ERROR;
+
+               // Free file
+               pakfire_file_unref(file);
+       }
+
+       return list;
+
+ERROR:
+       Py_XDECREF(list);
+       return NULL;
+}
index 7669acf46480ab7552e8068647d12d86298298ff..a8113cb06dffc8654b63f910dd3db0442b0ec0d2 100644 (file)
 
 #include <Python.h>
 
+#include <pakfire/filelist.h>
 #include <pakfire/packagelist.h>
 
 extern PyObject* performance_index(PyObject* self, PyObject* args);
 
 PyObject* PyList_FromPackageList(struct pakfire_packagelist* packagelist);
+PyObject* PyList_FromFileList(struct pakfire_filelist* filelist);
 
 #endif /* PYTHON_PAKFIRE_UTIL_H */