From: Michael Tremer Date: Wed, 1 Nov 2023 11:34:56 +0000 (+0000) Subject: _pakfire: Create scaffolding for a context object X-Git-Tag: 0.9.30~1351 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=049fe436a3d9e284ab5d4fed74f32103db31e16e;p=pakfire.git _pakfire: Create scaffolding for a context object Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index a6319d924..4188697ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index 7a60575fa..fd56a95a2 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -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 index 000000000..d1463619b --- /dev/null +++ b/src/_pakfire/ctx.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#include + +#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 index 000000000..8c3177442 --- /dev/null +++ b/src/_pakfire/ctx.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PYTHON_PAKFIRE_CTX_H +#define PYTHON_PAKFIRE_CTX_H + +#include + +#include + +typedef struct { + PyObject_HEAD + struct pakfire_ctx* ctx; +} CtxObject; + +extern PyTypeObject CtxType; + +#endif /* PYTHON_PAKFIRE_CTX_H */