]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Create scaffolding for a context object
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Nov 2023 11:34:56 +0000 (11:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Nov 2023 11:34:56 +0000 (11:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/_pakfire/_pakfiremodule.c
src/_pakfire/ctx.c [new file with mode: 0644]
src/_pakfire/ctx.h [new file with mode: 0644]

index a6319d924697b03320fa94fd33be251dcec450c7..4188697ec2f7eb92bce726d5d7c62e0631a01cb8 100644 (file)
@@ -140,6 +140,8 @@ _pakfire_la_SOURCES = \
        src/_pakfire/archive_file.h \
        src/_pakfire/buildservice.c \
        src/_pakfire/buildservice.h \
+       src/_pakfire/ctx.c \
+       src/_pakfire/ctx.h \
        src/_pakfire/errors.h \
        src/_pakfire/file.c \
        src/_pakfire/file.h \
index 7a60575fa03b5f839bc3a1fc57922c394f80bb22..fd56a95a2bd9cc1ef34aaddcf61a7734939b56e0 100644 (file)
@@ -30,6 +30,7 @@
 #include "archive.h"
 #include "archive_file.h"
 #include "buildservice.h"
+#include "ctx.h"
 #include "errors.h"
 #include "file.h"
 #include "key.h"
@@ -377,6 +378,13 @@ PyMODINIT_FUNC PyInit__pakfire(void) {
        Py_INCREF(&BuildServiceType);
        PyModule_AddObject(module, "BuildService", (PyObject*)&BuildServiceType);
 
+       // Ctx
+       if (PyType_Ready(&CtxType) < 0)
+               return NULL;
+
+       Py_INCREF(&CtxType);
+       PyModule_AddObject(module, "Ctx", (PyObject*)&CtxType);
+
        // File
        if (PyType_Ready(&FileType) < 0)
                return NULL;
diff --git a/src/_pakfire/ctx.c b/src/_pakfire/ctx.c
new file mode 100644 (file)
index 0000000..d146361
--- /dev/null
@@ -0,0 +1,82 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <Python.h>
+
+#include <pakfire/ctx.h>
+
+#include "ctx.h"
+
+static PyObject* Ctx_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
+       CtxObject* self = (CtxObject *)type->tp_alloc(type, 0);
+       if (self)
+               self->ctx = NULL;
+
+       return (PyObject *)self;
+}
+
+static int Ctx_init(CtxObject* self, PyObject* args, PyObject* kwargs) {
+       char* kwlist[] = { (char*)"path", NULL };
+       const char* path = NULL;
+       int r;
+
+       // Parse arguments
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &path))
+               return -1;
+
+       // Create context
+       r = pakfire_ctx_create(&self->ctx, path);
+       if (r) {
+               errno = -r;
+
+               PyErr_SetFromErrno(PyExc_OSError);
+               return -1;
+       }
+
+       return 0;
+}
+
+static void Ctx_dealloc(CtxObject* self) {
+       if (self->ctx)
+               pakfire_ctx_unref(self->ctx);
+
+       Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static struct PyMethodDef Ctx_methods[] = {
+       { NULL },
+};
+
+static struct PyGetSetDef Ctx_getsetters[] = {
+    { NULL },
+};
+
+PyTypeObject CtxType = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       tp_name:            "_pakfire.Ctx",
+       tp_basicsize:       sizeof(CtxObject),
+       tp_flags:           Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+       tp_new:             Ctx_new,
+       tp_dealloc:         (destructor)Ctx_dealloc,
+       tp_init:            (initproc)Ctx_init,
+       tp_doc:             "Ctx Object",
+       tp_methods:         Ctx_methods,
+       tp_getset:          Ctx_getsetters,
+};
diff --git a/src/_pakfire/ctx.h b/src/_pakfire/ctx.h
new file mode 100644 (file)
index 0000000..8c31774
--- /dev/null
@@ -0,0 +1,35 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PYTHON_PAKFIRE_CTX_H
+#define PYTHON_PAKFIRE_CTX_H
+
+#include <Python.h>
+
+#include <pakfire/ctx.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct pakfire_ctx* ctx;
+} CtxObject;
+
+extern PyTypeObject CtxType;
+
+#endif /* PYTHON_PAKFIRE_CTX_H */