1 #include "rpmsystem-py.h"
12 * \brief A python rpm.mi match iterator object represents the result of a
15 * Instances of the rpm.mi object provide access to headers that match
16 * certain criteria. Typically, a primary index is accessed to find
17 * a set of headers that contain a key, and each header is returned
20 * The rpm.mi class conains the following methods:
21 * - next() -> hdr Return the next header that matches.
23 * - pattern(tag,mire,pattern) Specify secondary match criteria.
25 * To obtain a rpm.mi object to query the database used by a transaction,
26 * the ts.match(tag,key,len) method is used.
28 * Here's an example that prints the name of all installed packages:
31 * ts = rpm.TransactionSet()
32 * for h in ts.dbMatch():
36 * Here's a more typical example that uses the Name index to retrieve
37 * all installed kernel(s):
40 * ts = rpm.TransactionSet()
41 * mi = ts.dbMatch('name', "kernel")
43 * print "%s-%s-%s" % (h['name'], h['version'], h['release'])
46 * Finally, here's an example that retrieves all packages whose name
47 * matches the glob expression "XFree*":
50 * ts = rpm.TransactionSet()
52 * mi.pattern('name', rpm.RPMMIRE_GLOB, "XFree*")
54 * print "%s-%s-%s" % (h['name'], h['version'], h['release'])
63 struct rpmmiObject_s {
65 PyObject *md_dict; /*!< to look like PyModuleObject */
66 PyObject *ref; /* for db/ts refcounting */
67 rpmdbMatchIterator mi;
71 rpmmi_iternext(rpmmiObject * s)
75 if (s->mi == NULL || (h = rpmdbNextIterator(s->mi)) == NULL) {
76 s->mi = rpmdbFreeIterator(s->mi);
79 return hdr_Wrap(&hdr_Type, h);
83 rpmmi_Instance(rpmmiObject * s)
88 rc = rpmdbGetIteratorOffset(s->mi);
90 return Py_BuildValue("i", rc);
94 rpmmi_Count(rpmmiObject * s)
99 rc = rpmdbGetIteratorCount(s->mi);
101 return Py_BuildValue("i", rc);
105 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
110 char * kwlist[] = {"tag", "type", "patern", NULL};
112 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&is:Pattern", kwlist,
113 tagNumFromPyObject, &tag, &type, &pattern))
116 rpmdbSetIteratorRE(s->mi, tag, type, pattern);
121 static struct PyMethodDef rpmmi_methods[] = {
122 {"instance", (PyCFunction) rpmmi_Instance, METH_NOARGS,
124 {"count", (PyCFunction) rpmmi_Count, METH_NOARGS,
126 {"pattern", (PyCFunction) rpmmi_Pattern, METH_VARARGS|METH_KEYWORDS,
127 "mi.pattern(TagN, mire_type, pattern)\n\
128 - Set a secondary match pattern on tags from retrieved header.\n" },
129 {NULL, NULL} /* sentinel */
132 static void rpmmi_dealloc(rpmmiObject * s)
134 s->mi = rpmdbFreeIterator(s->mi);
136 s->ob_type->tp_free((PyObject *)s);
139 static char rpmmi_doc[] =
142 PyTypeObject rpmmi_Type = {
143 PyObject_HEAD_INIT(&PyType_Type)
145 "rpm.mi", /* tp_name */
146 sizeof(rpmmiObject), /* tp_size */
148 (destructor) rpmmi_dealloc, /* tp_dealloc */
150 (getattrfunc)0, /* tp_getattr */
154 0, /* tp_as_number */
155 0, /* tp_as_sequence */
156 0, /* tp_as_mapping */
160 PyObject_GenericGetAttr, /* tp_getattro */
161 PyObject_GenericSetAttr, /* tp_setattro */
162 0, /* tp_as_buffer */
163 Py_TPFLAGS_DEFAULT, /* tp_flags */
164 rpmmi_doc, /* tp_doc */
167 0, /* tp_richcompare */
168 0, /* tp_weaklistoffset */
169 PyObject_SelfIter, /* tp_iter */
170 (iternextfunc) rpmmi_iternext, /* tp_iternext */
171 rpmmi_methods, /* tp_methods */
176 0, /* tp_descr_get */
177 0, /* tp_descr_set */
178 0, /* tp_dictoffset */
186 PyObject * rpmmi_Wrap(PyTypeObject *subtype, rpmdbMatchIterator mi, PyObject *s)
188 rpmmiObject * mio = (rpmmiObject *)subtype->tp_alloc(subtype, 0);
189 if (mio == NULL) return PyErr_NoMemory();
194 return (PyObject *) mio;