[SATYR PATCH 2/8] python: bindings for struct sr_rpm_package
Martin Milata
mmilata at redhat.com
Mon Aug 26 14:49:01 UTC 2013
Needed for #86.
Signed-off-by: Martin Milata <mmilata at redhat.com>
---
python/Makefile.am | 2 +
python/py_module.c | 15 ++++
python/py_rpm_package.c | 225 ++++++++++++++++++++++++++++++++++++++++++++++++
python/py_rpm_package.h | 67 ++++++++++++++
tests/python/report.py | 16 ++++
5 files changed, 325 insertions(+)
create mode 100644 python/py_rpm_package.c
create mode 100644 python/py_rpm_package.h
diff --git a/python/Makefile.am b/python/Makefile.am
index fab2c13..34491c1 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -42,6 +42,8 @@ _satyr_la_SOURCES = \
py_java_thread.c \
py_java_stacktrace.h \
py_java_stacktrace.c \
+ py_rpm_package.h \
+ py_rpm_package.c \
py_metrics.h \
py_metrics.c \
py_operating_system.h \
diff --git a/python/py_module.c b/python/py_module.c
index 763ffaf..e566376 100644
--- a/python/py_module.c
+++ b/python/py_module.c
@@ -17,6 +17,7 @@
#include "py_core_frame.h"
#include "py_core_thread.h"
#include "py_core_stacktrace.h"
+#include "py_rpm_package.h"
#include "py_metrics.h"
#include "py_operating_system.h"
@@ -24,6 +25,7 @@
#include "thread.h"
#include "stacktrace.h"
#include "gdb/sharedlib.h"
+#include "rpm.h"
static PyMethodDef
module_methods[]=
@@ -164,6 +166,12 @@ init_satyr()
return;
}
+ if (PyType_Ready(&sr_py_rpm_package_type) < 0)
+ {
+ puts("PyType_Ready(&sr_py_rpm_package_type) < 0");
+ return;
+ }
+
PyObject *module = Py_InitModule("_satyr", module_methods);
if (!module)
@@ -274,4 +282,11 @@ init_satyr()
Py_INCREF(&sr_py_operating_system_type);
PyModule_AddObject(module, "OperatingSystem",
(PyObject *)&sr_py_operating_system_type);
+
+ Py_INCREF(&sr_py_rpm_package_type);
+ PyModule_AddObject(module, "RpmPackage",
+ (PyObject *)&sr_py_rpm_package_type);
+
+ PyModule_AddIntConstant(module, "ROLE_UNKNOWN", SR_ROLE_UNKNOWN);
+ PyModule_AddIntConstant(module, "ROLE_AFFECTED", SR_ROLE_AFFECTED);
}
diff --git a/python/py_rpm_package.c b/python/py_rpm_package.c
new file mode 100644
index 0000000..22165d7
--- /dev/null
+++ b/python/py_rpm_package.c
@@ -0,0 +1,225 @@
+/*
+ py_rpm_package.c
+
+ Copyright (C) 2013 Red Hat, Inc.
+
+ 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 2 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, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "py_common.h"
+#include "py_rpm_package.h"
+
+#include "rpm.h"
+#include "strbuf.h"
+
+#define rpm_package_doc "satyr.RpmPackage - RPM package representation\n" \
+ "Usage:\n" \
+ "satyr.RpmPackage() - creates an empty RPM package object\n" \
+ "satyr.RpmPackage(name, epoch, version, release, arch) - creates RPM package\n" \
+ "object with given properties"
+
+#define rpm_role_doc "Role the package plays in the problem. Currently ROLE_UNKNOWN or ROLE_AFFECTED."
+
+/* See python/py_common.h and python/py_gdb_frame.c for generic getters/setters documentation. */
+#define GSOFF_PY_STRUCT sr_py_rpm_package
+#define GSOFF_PY_MEMBER rpm_package
+#define GSOFF_C_STRUCT sr_rpm_package
+GSOFF_START
+GSOFF_MEMBER(name),
+GSOFF_MEMBER(epoch),
+GSOFF_MEMBER(version),
+GSOFF_MEMBER(release),
+GSOFF_MEMBER(architecture),
+GSOFF_MEMBER(install_time)
+/* GSOFF_MEMBER(role) */
+GSOFF_END
+
+static PyGetSetDef
+rpm_package_getset[] =
+{
+ SR_ATTRIBUTE_STRING(name, "Package name (string)" ),
+ SR_ATTRIBUTE_UINT32(epoch, "Epoch (int)" ),
+ SR_ATTRIBUTE_STRING(version, "Version (string)" ),
+ SR_ATTRIBUTE_STRING(release, "Release (string)" ),
+ SR_ATTRIBUTE_STRING(architecture, "Architecture (string)" ),
+ SR_ATTRIBUTE_UINT64(install_time, "Time of installation (long)" ),
+ { (char*)"role", sr_py_rpm_package_get_role, sr_py_rpm_package_set_role, (char*)rpm_role_doc, NULL },
+ { NULL },
+};
+
+PyTypeObject
+sr_py_rpm_package_type =
+{
+ PyObject_HEAD_INIT(NULL)
+ 0,
+ "satyr.RpmPackage", /* tp_name */
+ sizeof(struct sr_py_rpm_package), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ sr_py_rpm_package_free, /* tp_dealloc */
+ NULL, /* tp_print */
+ NULL, /* tp_getattr */
+ NULL, /* tp_setattr */
+ NULL, /* tp_compare */
+ NULL, /* tp_repr */
+ NULL, /* tp_as_number */
+ NULL, /* tp_as_sequence */
+ NULL, /* tp_as_mapping */
+ NULL, /* tp_hash */
+ NULL, /* tp_call */
+ sr_py_rpm_package_str, /* tp_str */
+ NULL, /* tp_getattro */
+ NULL, /* tp_setattro */
+ NULL, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ rpm_package_doc, /* tp_doc */
+ NULL, /* tp_traverse */
+ NULL, /* tp_clear */
+ NULL, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ NULL, /* tp_iter */
+ NULL, /* tp_iternext */
+ NULL, /* tp_methods */
+ NULL, /* tp_members */
+ rpm_package_getset, /* tp_getset */
+ NULL, /* tp_base */
+ NULL, /* tp_dict */
+ NULL, /* tp_descr_get */
+ NULL, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ NULL, /* tp_init */
+ NULL, /* tp_alloc */
+ sr_py_rpm_package_new, /* tp_new */
+ NULL, /* tp_free */
+ NULL, /* tp_is_gc */
+ NULL, /* tp_bases */
+ NULL, /* tp_mro */
+ NULL, /* tp_cache */
+ NULL, /* tp_subclasses */
+ NULL, /* tp_weaklist */
+};
+
+/* constructor */
+PyObject *
+sr_py_rpm_package_new(PyTypeObject *object, PyObject *args, PyObject *kwds)
+{
+ struct sr_py_rpm_package *rpm =
+ PyObject_New(struct sr_py_rpm_package, &sr_py_rpm_package_type);
+
+ if (!rpm)
+ return PyErr_NoMemory();
+
+ rpm->rpm_package = sr_rpm_package_new();
+
+ unsigned int epoch;
+ const char *name = NULL, *version = NULL, *rel = NULL, *arch = NULL;
+ if (!PyArg_ParseTuple(args, "|sIsss", &name, &epoch, &version, &rel, &arch))
+ return NULL;
+
+ if (name)
+ rpm->rpm_package->name = sr_strdup(name);
+
+ if (rel)
+ rpm->rpm_package->release = sr_strdup(rel);
+
+ if (version)
+ rpm->rpm_package->version = sr_strdup(version);
+
+ if (arch)
+ rpm->rpm_package->architecture = sr_strdup(arch);
+
+ rpm->rpm_package->epoch = (uint32_t)epoch;
+
+ return (PyObject*)rpm;
+}
+
+/* destructor */
+void
+sr_py_rpm_package_free(PyObject *object)
+{
+ struct sr_py_rpm_package *this = (struct sr_py_rpm_package *)object;
+ sr_rpm_package_free(this->rpm_package, false);
+ PyObject_Del(object);
+}
+
+/* str */
+PyObject *
+sr_py_rpm_package_str(PyObject *object)
+{
+ struct sr_py_rpm_package *this = (struct sr_py_rpm_package*)object;
+ struct sr_strbuf *buf = sr_strbuf_new();
+
+ if (this->rpm_package->name)
+ {
+ sr_strbuf_append_str(buf, this->rpm_package->name);
+
+ if (this->rpm_package->version)
+ {
+ sr_strbuf_append_str(buf, "-");
+
+ if (this->rpm_package->epoch)
+ sr_strbuf_append_strf(buf, "%u:", (unsigned)this->rpm_package->epoch);
+
+ sr_strbuf_append_str(buf, this->rpm_package->version);
+
+ if (this->rpm_package->release)
+ {
+ sr_strbuf_append_strf(buf, "-%s", this->rpm_package->release);
+
+ if (this->rpm_package->architecture)
+ sr_strbuf_append_strf(buf, ".%s", this->rpm_package->architecture);
+ }
+ }
+
+ }
+ else
+ sr_strbuf_append_str(buf, "(unknown)");
+
+ char *str = sr_strbuf_free_nobuf(buf);
+ PyObject *result = Py_BuildValue("s", str);
+ free(str);
+ return result;
+}
+
+/* getters & setters */
+
+PyObject *
+sr_py_rpm_package_get_role(PyObject *self, void *data)
+{
+ return Py_BuildValue("i", ((struct sr_py_rpm_package*)self)->rpm_package->role);
+}
+
+int
+sr_py_rpm_package_set_role(PyObject *self, PyObject *rhs, void *data)
+{
+ if (rhs == NULL)
+ {
+ PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute.");
+ return -1;
+ }
+
+ long newvalue = PyInt_AsLong(rhs);
+ if (PyErr_Occurred())
+ return -1;
+
+ if (newvalue != SR_ROLE_UNKNOWN && newvalue != SR_ROLE_AFFECTED)
+ {
+ PyErr_SetString(PyExc_ValueError, "Role must be either ROLE_UNKNOWN "
+ "or ROLE_AFFECTED.");
+ return -1;
+ }
+
+ ((struct sr_py_rpm_package *)self)->rpm_package->role = newvalue;
+ return 0;
+}
diff --git a/python/py_rpm_package.h b/python/py_rpm_package.h
new file mode 100644
index 0000000..0a00d8d
--- /dev/null
+++ b/python/py_rpm_package.h
@@ -0,0 +1,67 @@
+/*
+ py_rpm_package.h
+
+ Copyright (C) 2013 Red Hat, Inc.
+
+ 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 2 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, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#ifndef SATYR_PY_RPM_PACKAGE_H
+#define SATYR_PY_RPM_PACKAGE_H
+
+/**
+ * @file
+ * @brief Python bindings for RPM package.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <Python.h>
+#include <structmember.h>
+
+PyTypeObject sr_py_rpm_package_type;
+
+struct sr_py_rpm_package
+{
+ PyObject_HEAD
+ struct sr_rpm_package *rpm_package;
+};
+
+/**
+ * Constructor.
+ */
+PyObject *sr_py_rpm_package_new(PyTypeObject *object,
+ PyObject *args, PyObject *kwds);
+
+/**
+ * Destructor.
+ */
+void sr_py_rpm_package_free(PyObject *object);
+
+/**
+ * str
+ */
+PyObject *sr_py_rpm_package_str(PyObject *self);
+
+/* getters & setters */
+PyObject *sr_py_rpm_package_get_role(PyObject *self, void *data);
+int sr_py_rpm_package_set_role(PyObject *self, PyObject *rhs, void *data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/tests/python/report.py b/tests/python/report.py
index 7580fb5..da00b08 100755
--- a/tests/python/report.py
+++ b/tests/python/report.py
@@ -17,6 +17,22 @@ class TestOperatingSystem(BindingsTestCase):
self.assertGetSetCorrect(self.opsys, 'uptime', 0, 9000)
self.assertGetSetCorrect(self.opsys, 'cpe', None, 'cpe:/o:fedoraproject:fedora:19')
+class TestRpmPackage(BindingsTestCase):
+ def setUp(self):
+ self.rpm = satyr.RpmPackage('emacs-filesystem', 1, '24.3', '11.fc19', 'noarch')
+
+ def test_str(self):
+ self.assertEqual(str(self.rpm), 'emacs-filesystem-1:24.3-11.fc19.noarch')
+
+ def test_getset(self):
+ self.assertGetSetCorrect(self.rpm, 'name', 'emacs-filesystem', 'msword')
+ self.assertGetSetCorrect(self.rpm, 'epoch', 1, 0)
+ self.assertGetSetCorrect(self.rpm, 'version', '24.3', '48')
+ self.assertGetSetCorrect(self.rpm, 'release', '11.fc19', '1.asdf')
+ self.assertGetSetCorrect(self.rpm, 'architecture', 'noarch', 'armv7hl')
+ self.assertGetSetCorrect(self.rpm, 'install_time', 0, 1234565)
+ self.assertGetSetCorrect(self.rpm, 'role', satyr.ROLE_UNKNOWN, satyr.ROLE_AFFECTED)
+
if __name__ == '__main__':
unittest.main()
--
1.8.3.1
More information about the Crash-catcher
mailing list