rpm  4.5
rpmmi-py.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include <rpmlib.h>
8 #include <rpmdb.h>
9 
10 #include "rpmmi-py.h"
11 #include "header-py.h"
12 
13 #include "debug.h"
14 
66 static PyObject *
68  /*@*/
69 {
70  Py_INCREF(s);
71  return (PyObject *)s;
72 }
73 
76 /*@null@*/
77 static PyObject *
79  /*@globals rpmGlobalMacroContext @*/
80  /*@modifies s, rpmGlobalMacroContext @*/
81 {
82  Header h;
83 
84  if (s->mi == NULL || (h = rpmdbNextIterator(s->mi)) == NULL) {
85  s->mi = rpmdbFreeIterator(s->mi);
86  return NULL;
87  }
88  return (PyObject *) hdr_Wrap(h);
89 }
90 
93 /*@null@*/
94 static PyObject *
96  /*@globals rpmGlobalMacroContext, _Py_NoneStruct @*/
97  /*@modifies s, rpmGlobalMacroContext, _Py_NoneStruct @*/
98 {
99  PyObject * result;
100 
101  result = rpmmi_iternext(s);
102 
103  if (result == NULL) {
104  Py_INCREF(Py_None);
105  return Py_None;
106  }
107  return result;
108 }
109 
114 
117 /*@null@*/
118 static PyObject *
120  /*@*/
121 {
122  int rc = 0;
123 
124  if (s->mi != NULL)
125  rc = rpmdbGetIteratorOffset(s->mi);
126 
127  return Py_BuildValue("i", rc);
128 }
129 
132 /*@null@*/
133 static PyObject *
135  /*@*/
136 {
137  int rc = 0;
138 
139  if (s->mi != NULL)
140  rc = rpmdbGetIteratorCount(s->mi);
141 
142  return Py_BuildValue("i", rc);
143 }
144 
147 /*@null@*/
148 static PyObject *
149 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
150  /*@globals rpmGlobalMacroContext, _Py_NoneStruct @*/
151  /*@modifies s, rpmGlobalMacroContext, _Py_NoneStruct @*/
152 {
153  PyObject *TagN = NULL;
154  int type;
155  char * pattern;
156  rpmTag tag;
157  char * kwlist[] = {"tag", "type", "patern", NULL};
158 
159  if (!PyArg_ParseTupleAndKeywords(args, kwds, "Ois:Pattern", kwlist,
160  &TagN, &type, &pattern))
161  return NULL;
162 
163  if ((tag = tagNumFromPyObject (TagN)) == -1) {
164  PyErr_SetString(PyExc_TypeError, "unknown tag type");
165  return NULL;
166  }
167 
168  rpmdbSetIteratorRE(s->mi, tag, type, pattern);
169 
170  Py_INCREF (Py_None);
171  return Py_None;
172 
173 }
174 
179 /*@-fullinitblock@*/
180 /*@unchecked@*/ /*@observer@*/
181 static struct PyMethodDef rpmmi_methods[] = {
182  {"next", (PyCFunction) rpmmi_Next, METH_NOARGS,
183 "mi.next() -> hdr\n\
184 - Retrieve next header that matches. Iterate directly in python if possible.\n" },
185  {"instance", (PyCFunction) rpmmi_Instance, METH_NOARGS,
186  NULL },
187  {"count", (PyCFunction) rpmmi_Count, METH_NOARGS,
188  NULL },
189  {"pattern", (PyCFunction) rpmmi_Pattern, METH_VARARGS|METH_KEYWORDS,
190 "mi.pattern(TagN, mire_type, pattern)\n\
191 - Set a secondary match pattern on tags from retrieved header.\n" },
192  {NULL, NULL} /* sentinel */
193 };
194 /*@=fullinitblock@*/
195 
198 static void rpmmi_dealloc(/*@only@*/ /*@null@*/ rpmmiObject * s)
199  /*@globals rpmGlobalMacroContext @*/
200  /*@modifies s, rpmGlobalMacroContext @*/
201 {
202  if (s) {
203  s->mi = rpmdbFreeIterator(s->mi);
204  Py_DECREF(s->ref);
205  PyObject_Del(s);
206  }
207 }
208 
209 static PyObject * rpmmi_getattro(PyObject * o, PyObject * n)
210  /*@*/
211 {
212  return PyObject_GenericGetAttr(o, n);
213 }
214 
215 static int rpmmi_setattro(PyObject * o, PyObject * n, PyObject * v)
216  /*@*/
217 {
218  return PyObject_GenericSetAttr(o, n, v);
219 }
220 
223 /*@unchecked@*/ /*@observer@*/
224 static char rpmmi_doc[] =
225 "";
226 
229 /*@-fullinitblock@*/
230 PyTypeObject rpmmi_Type = {
231  PyObject_HEAD_INIT(&PyType_Type)
232  0, /* ob_size */
233  "rpm.mi", /* tp_name */
234  sizeof(rpmmiObject), /* tp_size */
235  0, /* tp_itemsize */
236  (destructor) rpmmi_dealloc, /* tp_dealloc */
237  0, /* tp_print */
238  (getattrfunc)0, /* tp_getattr */
239  0, /* tp_setattr */
240  0, /* tp_compare */
241  0, /* tp_repr */
242  0, /* tp_as_number */
243  0, /* tp_as_sequence */
244  0, /* tp_as_mapping */
245  0, /* tp_hash */
246  0, /* tp_call */
247  0, /* tp_str */
248  (getattrofunc) rpmmi_getattro, /* tp_getattro */
249  (setattrofunc) rpmmi_setattro, /* tp_setattro */
250  0, /* tp_as_buffer */
251  Py_TPFLAGS_DEFAULT, /* tp_flags */
252  rpmmi_doc, /* tp_doc */
253 #if Py_TPFLAGS_HAVE_ITER
254  0, /* tp_traverse */
255  0, /* tp_clear */
256  0, /* tp_richcompare */
257  0, /* tp_weaklistoffset */
258  (getiterfunc) rpmmi_iter, /* tp_iter */
259  (iternextfunc) rpmmi_iternext, /* tp_iternext */
260  rpmmi_methods, /* tp_methods */
261  0, /* tp_members */
262  0, /* tp_getset */
263  0, /* tp_base */
264  0, /* tp_dict */
265  0, /* tp_descr_get */
266  0, /* tp_descr_set */
267  0, /* tp_dictoffset */
268  0, /* tp_init */
269  0, /* tp_alloc */
270  0, /* tp_new */
271  0, /* tp_free */
272  0, /* tp_is_gc */
273 #endif
274 };
275 /*@=fullinitblock@*/
276 
278 {
279  rpmmiObject * mio = (rpmmiObject *) PyObject_New(rpmmiObject, &rpmmi_Type);
280 
281  if (mio == NULL) {
282  PyErr_SetString(pyrpmError, "out of memory creating rpmmiObject");
283  return NULL;
284  }
285  mio->mi = mi;
286  mio->ref = s;
287  Py_INCREF(mio->ref);
288  return mio;
289 }