1 #include "rpmsystem-py.h"
12 * \brief A python rpm.ii key iterator object represents the keys of a
15 * The rpm.ii class conains the following methods:
16 * - next() -> key Return the next key.
18 * To obtain a rpm.ii object to query the database used by a transaction,
19 * the ts.dbIndex(tag) method is used.
21 * Here's an example that prints the name of all installed packages:
24 * ts = rpm.TransactionSet()
25 * for name in ts.dbIndex("conflictname"):
29 * ts.dbIndex() can be used to get the packages containing the keys of interest
36 struct rpmiiObject_s {
38 PyObject *md_dict; /*!< to look like PyModuleObject */
39 PyObject *ref; /* for db/ts refcounting */
40 rpmdbIndexIterator ii;
45 rpmii_iternext(rpmiiObject * s)
47 PyObject *keyo = NULL;
50 if (rpmdbIndexIteratorNextTd(s->ii, s->keytd) == 0) {
51 /* The keys must never be arrays so rpmtd_AsPyObj() wont work */
52 keyo = rpmtd_ItemAsPyobj(s->keytd, rpmtdClass(s->keytd));
53 rpmtdFreeData(s->keytd);
55 s->ii = rpmdbIndexIteratorFree(s->ii);
63 rpmii_instances(rpmiiObject * s)
65 int entries = rpmdbIndexIteratorNumPkgs(s->ii);
66 PyObject * list = PyList_New(entries);
68 for (int i = 0; i < entries; i++) {
69 tuple = PyTuple_New(2);
70 PyTuple_SET_ITEM(tuple, 0,
71 PyInt_FromLong(rpmdbIndexIteratorPkgOffset(s->ii, i)));
72 PyTuple_SET_ITEM(tuple, 1,
73 PyInt_FromLong(rpmdbIndexIteratorTagNum(s->ii, i)));
74 PyList_SET_ITEM(list, i, tuple);
79 static struct PyMethodDef rpmii_methods[] = {
80 {"instances", (PyCFunction) rpmii_instances, METH_NOARGS, NULL},
81 {NULL, NULL} /* sentinel */
84 static void rpmii_dealloc(rpmiiObject * s)
86 s->ii = rpmdbIndexIteratorFree(s->ii);
89 Py_TYPE(s)->tp_free((PyObject *)s);
92 static int rpmii_bool(rpmiiObject *s)
94 return (s->ii != NULL);
97 static PyNumberMethods rpmii_as_number = {
101 #if PY_MAJOR_VERSION < 3
104 0, /* nb_remainder */
110 (inquiry)rpmii_bool, /* nb_bool/nonzero */
113 static char rpmii_doc[] =
116 PyTypeObject rpmii_Type = {
117 PyVarObject_HEAD_INIT(&PyType_Type, 0)
118 "rpm.ii", /* tp_name */
119 sizeof(rpmiiObject), /* tp_size */
121 (destructor) rpmii_dealloc, /* tp_dealloc */
123 (getattrfunc)0, /* tp_getattr */
127 &rpmii_as_number, /* tp_as_number */
128 0, /* tp_as_sequence */
129 0, /* tp_as_mapping */
133 PyObject_GenericGetAttr, /* tp_getattro */
134 PyObject_GenericSetAttr, /* tp_setattro */
135 0, /* tp_as_buffer */
136 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
137 rpmii_doc, /* tp_doc */
140 0, /* tp_richcompare */
141 0, /* tp_weaklistoffset */
142 PyObject_SelfIter, /* tp_iter */
143 (iternextfunc) rpmii_iternext, /* tp_iternext */
144 rpmii_methods, /* tp_methods */
149 0, /* tp_descr_get */
150 0, /* tp_descr_set */
151 0, /* tp_dictoffset */
159 PyObject * rpmii_Wrap(PyTypeObject *subtype, rpmdbIndexIterator ii, PyObject *s)
161 rpmiiObject * iio = (rpmiiObject *)subtype->tp_alloc(subtype, 0);
162 if (iio == NULL) return NULL;
166 iio->keytd = rpmtdNew();
168 return (PyObject *) iio;