summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorFlorian Festi <ffesti@redhat.com>2009-12-15 13:15:10 +0100
committerFlorian Festi <ffesti@redhat.com>2010-10-21 10:32:20 +0200
commit5914d0ec2995d4f4d128c3e96b79f22ec8f48d64 (patch)
tree32cf33c6ca6a04f465cf803d714761ea935f50e8 /python
parenta916e399ab0734816f1e4567b34db0b55e9f5c7c (diff)
downloadlibrpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.tar.gz
librpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.tar.bz2
librpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.zip
rpmdbKeyIterator: loop over keys in the database indexes
Diffstat (limited to 'python')
-rw-r--r--python/Makefile.am1
-rw-r--r--python/rpmki-py.c138
-rw-r--r--python/rpmki-py.h12
-rw-r--r--python/rpmmodule.c5
-rw-r--r--python/rpmts-py.c29
5 files changed, 185 insertions, 0 deletions
diff --git a/python/Makefile.am b/python/Makefile.am
index b952743b0..57bcb6bf0 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -23,6 +23,7 @@ _rpmmodule_la_SOURCES = rpmmodule.c rpmsystem-py.h \
rpmfi-py.c rpmfi-py.h \
rpmkeyring-py.c rpmkeyring-py.h \
rpmmi-py.c rpmmi-py.h \
+ rpmki-py.c rpmki-py.h \
rpmps-py.c rpmps-py.h \
rpmmacro-py.c rpmmacro-py.h \
rpmtd-py.c rpmtd-py.h \
diff --git a/python/rpmki-py.c b/python/rpmki-py.c
new file mode 100644
index 000000000..8ed04871d
--- /dev/null
+++ b/python/rpmki-py.c
@@ -0,0 +1,138 @@
+#include "rpmsystem-py.h"
+
+#include <rpm/rpmdb.h>
+
+#include "rpmki-py.h"
+#include "header-py.h"
+
+#include "debug.h"
+
+/** \ingroup python
+ * \class Rpmki
+ * \brief A python rpm.ki key iterator object represents the keys of a
+ * database index.
+ *
+ * The rpm.ki class conains the following methods:
+ * - next() -> key Return the next key.
+ *
+ * To obtain a rpm.ki object to query the database used by a transaction,
+ * the ts.dbKeys(tag) method is used.
+ *
+ * Here's an example that prints the name of all installed packages:
+ * \code
+ * import rpm
+ * ts = rpm.TransactionSet()
+ * for name in ts.dbKeys("conflictname"):
+ * print name
+ * \endcode
+ *
+ * ts.dbMatch() can be used to get the packages containing the keys of interest
+
+/** \ingroup python
+ * \name Class: Rpmki
+ */
+
+struct rpmkiObject_s {
+ PyObject_HEAD
+ PyObject *md_dict; /*!< to look like PyModuleObject */
+ PyObject *ref; /* for db/ts refcounting */
+ rpmdbKeyIterator ki;
+};
+
+static PyObject *
+rpmki_iternext(rpmkiObject * s)
+{
+ if (s->ki == NULL || (rpmdbKeyIteratorNext(s->ki)) != 0) {
+ s->ki = rpmdbKeyIteratorFree(s->ki);
+ return NULL;
+ }
+ return PyString_FromStringAndSize(rpmdbKeyIteratorKey(s->ki),
+ rpmdbKeyIteratorKeySize(s->ki));
+};
+
+static struct PyMethodDef rpmki_methods[] = {
+ {NULL, NULL} /* sentinel */
+};
+
+static void rpmki_dealloc(rpmkiObject * s)
+{
+ s->ki = rpmdbKeyIteratorFree(s->ki);
+ Py_DECREF(s->ref);
+ Py_TYPE(s)->tp_free((PyObject *)s);
+}
+
+static int rpmki_bool(rpmkiObject *s)
+{
+ return (s->ki != NULL);
+}
+
+static PyNumberMethods rpmki_as_number = {
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_divide */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, (inquiry)rpmki_bool, /* nb_bool/nonzero */
+};
+
+static char rpmki_doc[] =
+"";
+
+PyTypeObject rpmki_Type = {
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "rpm.ki", /* tp_name */
+ sizeof(rpmkiObject), /* tp_size */
+ 0, /* tp_itemsize */
+ (destructor) rpmki_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ (getattrfunc)0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ 0, /* tp_repr */
+ &rpmki_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
+ rpmki_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc) rpmki_iternext, /* tp_iternext */
+ rpmki_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
+ 0, /* tp_is_gc */
+};
+
+PyObject * rpmki_Wrap(PyTypeObject *subtype, rpmdbKeyIterator ki, PyObject *s)
+{
+ rpmkiObject * kio = (rpmkiObject *)subtype->tp_alloc(subtype, 0);
+ if (kio == NULL) return NULL;
+
+ kio->ki = ki;
+ kio->ref = s;
+ Py_INCREF(kio->ref);
+ return (PyObject *) kio;
+}
diff --git a/python/rpmki-py.h b/python/rpmki-py.h
new file mode 100644
index 000000000..5fc3543d8
--- /dev/null
+++ b/python/rpmki-py.h
@@ -0,0 +1,12 @@
+#ifndef H_RPMKI_PY
+#define H_RPMKI_PY
+
+typedef struct rpmkiObject_s rpmkiObject;
+
+extern PyTypeObject rpmki_Type;
+
+#define rpmkiObject_Check(v) ((v)->ob_type == &rpmki_Type)
+
+PyObject * rpmki_Wrap(PyTypeObject *subtype, rpmdbKeyIterator ki, PyObject *s);
+
+#endif
diff --git a/python/rpmmodule.c b/python/rpmmodule.c
index 78340a9d8..e396cd01f 100644
--- a/python/rpmmodule.c
+++ b/python/rpmmodule.c
@@ -12,6 +12,7 @@
#include "rpmfi-py.h"
#include "rpmkeyring-py.h"
#include "rpmmi-py.h"
+#include "rpmki-py.h"
#include "rpmps-py.h"
#include "rpmmacro-py.h"
#include "rpmtd-py.h"
@@ -202,6 +203,7 @@ static int prepareInitModule(void)
if (PyType_Ready(&rpmfi_Type) < 0) return 0;
if (PyType_Ready(&rpmKeyring_Type) < 0) return 0;
if (PyType_Ready(&rpmmi_Type) < 0) return 0;
+ if (PyType_Ready(&rpmki_Type) < 0) return 0;
if (PyType_Ready(&rpmProblem_Type) < 0) return 0;
if (PyType_Ready(&rpmPubkey_Type) < 0) return 0;
#if 0
@@ -301,6 +303,9 @@ static int initModule(PyObject *m)
Py_INCREF(&rpmmi_Type);
PyModule_AddObject(m, "mi", (PyObject *) &rpmmi_Type);
+ Py_INCREF(&rpmki_Type);
+ PyModule_AddObject(m, "ki", (PyObject *) &rpmki_Type);
+
Py_INCREF(&rpmProblem_Type);
PyModule_AddObject(m, "prob", (PyObject *) &rpmProblem_Type);
diff --git a/python/rpmts-py.c b/python/rpmts-py.c
index 16a824318..13bb3aa58 100644
--- a/python/rpmts-py.c
+++ b/python/rpmts-py.c
@@ -12,6 +12,7 @@
#include "rpmkeyring-py.h"
#include "rpmfi-py.h" /* XXX for rpmfiNew */
#include "rpmmi-py.h"
+#include "rpmki-py.h"
#include "rpmps-py.h"
#include "rpmte-py.h"
@@ -628,6 +629,31 @@ exit:
Py_XDECREF(str);
return mio;
}
+static PyObject *
+rpmts_Keys(rpmtsObject * s, PyObject * args, PyObject * kwds)
+{
+ rpmTag tag;
+ PyObject *mio = NULL;
+ char * kwlist[] = {"tag", "pattern", "type", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:Keys", kwlist,
+ tagNumFromPyObject, &tag))
+ return NULL;
+
+ /* XXX If not already opened, open the database O_RDONLY now. */
+ if (rpmtsGetRdb(s->ts) == NULL) {
+ int rc = rpmtsOpenDB(s->ts, O_RDONLY);
+ if (rc || rpmtsGetRdb(s->ts) == NULL) {
+ PyErr_SetString(pyrpmError, "rpmdb open failed");
+ goto exit;
+ }
+ }
+
+ mio = rpmki_Wrap(&rpmki_Type, rpmdbKeyIteratorInit(rpmtsGetRdb(s->ts), tag), (PyObject*)s);
+
+exit:
+ return mio;
+}
static struct PyMethodDef rpmts_methods[] = {
{"addInstall", (PyCFunction) rpmts_AddInstall, METH_VARARGS,
@@ -681,6 +707,9 @@ static struct PyMethodDef rpmts_methods[] = {
{"dbMatch", (PyCFunction) rpmts_Match, METH_VARARGS|METH_KEYWORDS,
"ts.dbMatch([TagN, [key]]) -> mi\n\
- Create a match iterator for the default transaction rpmdb.\n" },
+ {"dbKeys", (PyCFunction) rpmts_Keys, METH_VARARGS|METH_KEYWORDS,
+"ts.dbKeys(TagN) -> ki\n\
+-Create a key iterator for the default transaction rpmdb.\n" },
{NULL, NULL} /* sentinel */
};