2 * \file python/rpmmi-py.c
10 #include "header-py.h"
16 * \brief A python rpm.mi match iterator object represents the result of a
19 * Instances of the rpm.mi object provide access to headers that match
20 * certain criteria. Typically, a primary index is accessed to find
21 * a set of headers that contain a key, and each header is returned
24 * The rpm.mi class conains the following methods:
25 * - next() -> hdr Return the next header that matches.
27 * - pattern(tag,mire,pattern) Specify secondary match criteria.
29 * To obtain a rpm.mi object to query the database used by a transaction,
30 * the ts.match(tag,key,len) method is used.
32 * Here's an example that prints the name of all installed packages:
35 * ts = rpm.TransactionSet()
36 * for h in ts.dbMatch():
40 * Here's a more typical example that uses the Name index to retrieve
41 * all installed kernel(s):
44 * ts = rpm.TransactionSet()
45 * mi = ts.dbMatch('name', "kernel")
47 * print "%s-%s-%s" % (h['name'], h['version'], h['release'])
50 * Finally, here's an example that retrieves all packages whose name
51 * matches the glob expression "XFree*":
54 * ts = rpm.TransactionSet()
56 * mi.pattern('name', rpm.RPMMIRE_GLOB, "XFree*")
58 * print "%s-%s-%s" % (h['name'], h['version'], h['release'])
69 struct rpmmiObject_s {
71 PyObject *md_dict; /*!< to look like PyModuleObject */
72 PyObject *ref; /* for db/ts refcounting */
73 rpmdbMatchIterator mi;
79 rpmmi_iternext(rpmmiObject * s)
83 if (s->mi == NULL || (h = rpmdbNextIterator(s->mi)) == NULL) {
84 s->mi = rpmdbFreeIterator(s->mi);
87 return (PyObject *) hdr_Wrap(h);
93 rpmmi_Instance(rpmmiObject * s)
98 rc = rpmdbGetIteratorOffset(s->mi);
100 return Py_BuildValue("i", rc);
106 rpmmi_Count(rpmmiObject * s)
111 rc = rpmdbGetIteratorCount(s->mi);
113 return Py_BuildValue("i", rc);
119 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
121 PyObject *TagN = NULL;
125 char * kwlist[] = {"tag", "type", "patern", NULL};
127 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Ois:Pattern", kwlist,
128 &TagN, &type, &pattern))
131 tag = tagNumFromPyObject (TagN);
132 if (tag == RPMTAG_NOT_FOUND) return NULL;
134 rpmdbSetIteratorRE(s->mi, tag, type, pattern);
141 static struct PyMethodDef rpmmi_methods[] = {
142 {"instance", (PyCFunction) rpmmi_Instance, METH_NOARGS,
144 {"count", (PyCFunction) rpmmi_Count, METH_NOARGS,
146 {"pattern", (PyCFunction) rpmmi_Pattern, METH_VARARGS|METH_KEYWORDS,
147 "mi.pattern(TagN, mire_type, pattern)\n\
148 - Set a secondary match pattern on tags from retrieved header.\n" },
149 {NULL, NULL} /* sentinel */
154 static void rpmmi_dealloc(rpmmiObject * s)
157 s->mi = rpmdbFreeIterator(s->mi);
165 static char rpmmi_doc[] =
170 PyTypeObject rpmmi_Type = {
171 PyObject_HEAD_INIT(&PyType_Type)
173 "rpm.mi", /* tp_name */
174 sizeof(rpmmiObject), /* tp_size */
176 (destructor) rpmmi_dealloc, /* tp_dealloc */
178 (getattrfunc)0, /* tp_getattr */
182 0, /* tp_as_number */
183 0, /* tp_as_sequence */
184 0, /* tp_as_mapping */
188 PyObject_GenericGetAttr, /* tp_getattro */
189 PyObject_GenericSetAttr, /* tp_setattro */
190 0, /* tp_as_buffer */
191 Py_TPFLAGS_DEFAULT, /* tp_flags */
192 rpmmi_doc, /* tp_doc */
195 0, /* tp_richcompare */
196 0, /* tp_weaklistoffset */
197 PyObject_SelfIter, /* tp_iter */
198 (iternextfunc) rpmmi_iternext, /* tp_iternext */
199 rpmmi_methods, /* tp_methods */
204 0, /* tp_descr_get */
205 0, /* tp_descr_set */
206 0, /* tp_dictoffset */
214 rpmmiObject * rpmmi_Wrap(rpmdbMatchIterator mi, PyObject *s)
216 rpmmiObject * mio = (rpmmiObject *) PyObject_New(rpmmiObject, &rpmmi_Type);
219 PyErr_SetString(pyrpmError, "out of memory creating rpmmiObject");