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