From: Michael Tremer Date: Sat, 17 Dec 2016 17:29:38 +0000 (+0100) Subject: _pakfire: Import Archive class X-Git-Tag: 0.9.28~1285^2~1363 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f1f8452b2f2a597926c4c2f3d671ba575b6bd285;p=pakfire.git _pakfire: Import Archive class This can be used to read a package archive and extract files from it. Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 7e492a2af..8f445fb15 100644 --- a/Makefile.am +++ b/Makefile.am @@ -179,6 +179,8 @@ pkgpyexec_LTLIBRARIES += \ _pakfire_la_SOURCES = \ src/_pakfire/_pakfiremodule.c \ + src/_pakfire/archive.c \ + src/_pakfire/archive.h \ src/_pakfire/capabilities.c \ src/_pakfire/capabilities.h \ src/_pakfire/constants.h \ diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index 086da7a60..119317e78 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -25,6 +25,7 @@ #include #include +#include "archive.h" #include "capabilities.h" #include "constants.h" #include "pool.h" @@ -200,6 +201,13 @@ PyMODINIT_FUNC PyInit__pakfire(void) { if (!module) return NULL; + // Archive + if (PyType_Ready(&ArchiveType) < 0) + return; + + Py_INCREF(&ArchiveType); + PyModule_AddObject(module, "Archive", (PyObject *)&ArchiveType); + // Pool if (PyType_Ready(&PoolType) < 0) return NULL; diff --git a/src/_pakfire/archive.c b/src/_pakfire/archive.c new file mode 100644 index 000000000..5a9a64855 --- /dev/null +++ b/src/_pakfire/archive.c @@ -0,0 +1,120 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2014 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 + +#include "archive.h" + +static PyObject* Archive_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { + ArchiveObject* self = (ArchiveObject *)type->tp_alloc(type, 0); + if (self) { + self->archive = NULL; + } + + return (PyObject *)self; +} + +static void Archive_dealloc(ArchiveObject* self) { + if (self->archive) + pakfire_archive_free(self->archive); + + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static int Archive_init(ArchiveObject* self, PyObject* args, PyObject* kwds) { + const char* filename = NULL; + + if (!PyArg_ParseTuple(args, "s", &filename)) + return -1; + + self->archive = pakfire_archive_open(filename); + + return 0; +} + +static PyObject* Archive_get_format(ArchiveObject* self) { + unsigned int format = pakfire_archive_get_format(self->archive); + + return PyLong_FromUnsignedLong(format); +} + +static PyObject* Archive_read(ArchiveObject* self, PyObject* args, PyObject* kwds) { + char* kwlist[] = {"filename", "payload", NULL}; + + const char* filename = NULL; + int payload = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &filename, &payload)) + return NULL; + + int flags = 0; + if (payload) + flags |= PAKFIRE_ARCHIVE_USE_PAYLOAD; + + if (!filename) + Py_RETURN_NONE; + + void* data = NULL; + size_t data_size = 0; + + int ret = pakfire_archive_read(self->archive, filename, &data, &data_size, flags); + if (ret) { + pakfire_free(data); + + Py_RETURN_NONE; + } + + // XXX This is not ideal since PyBytes_FromStringAndSize creates a + // copy of data. + PyObject* bytes = PyBytes_FromStringAndSize(data, data_size); + pakfire_free(data); + + return bytes; +} + +static struct PyMethodDef Archive_methods[] = { + {"read", (PyCFunction)Archive_read, METH_VARARGS|METH_KEYWORDS, NULL}, + { NULL }, +}; + +static struct PyGetSetDef Archive_getsetters[] = { + {"format", (getter)Archive_get_format, NULL, NULL, NULL}, + { NULL }, +}; + +PyTypeObject ArchiveType = { + PyVarObject_HEAD_INIT(NULL, 0) + tp_name: "_pakfire.Archive", + tp_basicsize: sizeof(ArchiveObject), + tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + tp_new: Archive_new, + tp_dealloc: (destructor)Archive_dealloc, + tp_init: (initproc)Archive_init, + tp_doc: "Archive object", + tp_methods: Archive_methods, + tp_getset: Archive_getsetters, + //tp_hash: (hashfunc)Archive_hash, + //tp_repr: (reprfunc)Archive_repr, + //tp_str: (reprfunc)Archive_str, + //tp_richcompare: (richcmpfunc)Archive_richcompare, +}; diff --git a/src/_pakfire/archive.h b/src/_pakfire/archive.h new file mode 100644 index 000000000..a750842d2 --- /dev/null +++ b/src/_pakfire/archive.h @@ -0,0 +1,35 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2014 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_ARCHIVE_H +#define PYTHON_PAKFIRE_ARCHIVE_H + +#include + +#include + +typedef struct { + PyObject_HEAD + PakfireArchive archive; +} ArchiveObject; + +extern PyTypeObject ArchiveType; + +#endif /* PYTHON_PAKFIRE_ARCHIVE_H */