]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Import Archive class
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Dec 2016 17:29:38 +0000 (18:29 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 17 Dec 2016 17:29:38 +0000 (18:29 +0100)
This can be used to read a package archive and extract
files from it.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/_pakfire/_pakfiremodule.c
src/_pakfire/archive.c [new file with mode: 0644]
src/_pakfire/archive.h [new file with mode: 0644]

index 7e492a2af7d5b3ced9b27731162ad9c6f94ba9db..8f445fb1509cf6986a146e56d0d0a745ddcb096c 100644 (file)
@@ -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 \
index 086da7a60ecbe182beb5badb384f728a50c90ca0..119317e78176d9cce672e7ae6da9104fac4e3bb6 100644 (file)
@@ -25,6 +25,7 @@
 #include <sched.h>
 #include <sys/personality.h>
 
+#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 (file)
index 0000000..5a9a648
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <Python.h>
+
+#include <pakfire/archive.h>
+#include <pakfire/util.h>
+
+#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 (file)
index 0000000..a750842
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PYTHON_PAKFIRE_ARCHIVE_H
+#define PYTHON_PAKFIRE_ARCHIVE_H
+
+#include <Python.h>
+
+#include <pakfire/archive.h>
+
+typedef struct {
+       PyObject_HEAD
+       PakfireArchive archive;
+} ArchiveObject;
+
+extern PyTypeObject ArchiveType;
+
+#endif /* PYTHON_PAKFIRE_ARCHIVE_H */