2 * \file python/rpmfi-py.c
7 #include <rpm/rpmtypes.h>
8 #include <rpm/rpmpgp.h>
10 #include "header-py.h"
17 rpmfi_Debug(rpmfiObject * s, PyObject * args, PyObject * kwds)
19 char * kwlist[] = {"debugLevel", NULL};
21 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
29 rpmfi_FC(rpmfiObject * s)
31 return Py_BuildValue("i", rpmfiFC(s->fi));
35 rpmfi_FX(rpmfiObject * s)
37 return Py_BuildValue("i", rpmfiFX(s->fi));
41 rpmfi_DC(rpmfiObject * s)
43 return Py_BuildValue("i", rpmfiDC(s->fi));
47 rpmfi_DX(rpmfiObject * s)
49 return Py_BuildValue("i", rpmfiDX(s->fi));
53 rpmfi_BN(rpmfiObject * s)
55 return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
59 rpmfi_DN(rpmfiObject * s)
61 return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
65 rpmfi_FN(rpmfiObject * s)
67 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
71 rpmfi_FFlags(rpmfiObject * s)
73 return Py_BuildValue("i", rpmfiFFlags(s->fi));
77 rpmfi_VFlags(rpmfiObject * s)
79 return Py_BuildValue("i", rpmfiVFlags(s->fi));
83 rpmfi_FMode(rpmfiObject * s)
85 return Py_BuildValue("i", rpmfiFMode(s->fi));
89 rpmfi_FState(rpmfiObject * s)
91 return Py_BuildValue("i", rpmfiFState(s->fi));
94 /* XXX rpmfiFDigest */
96 rpmfi_Digest(rpmfiObject * s)
98 const unsigned char *digest;
101 digest = rpmfiFDigest(s->fi, NULL, &diglen);
103 char *dig = pgpHexStr(digest, diglen);
104 return Py_BuildValue("s", dig);
111 rpmfi_FLink(rpmfiObject * s)
113 return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
117 rpmfi_FSize(rpmfiObject * s)
119 return Py_BuildValue("i", rpmfiFSize(s->fi));
123 rpmfi_FRdev(rpmfiObject * s)
125 return Py_BuildValue("i", rpmfiFRdev(s->fi));
129 rpmfi_FMtime(rpmfiObject * s)
131 return Py_BuildValue("i", rpmfiFMtime(s->fi));
135 rpmfi_FUser(rpmfiObject * s)
137 return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
141 rpmfi_FGroup(rpmfiObject * s)
143 return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
147 rpmfi_FColor(rpmfiObject * s)
149 return Py_BuildValue("i", rpmfiFColor(s->fi));
153 rpmfi_FClass(rpmfiObject * s)
157 if ((FClass = rpmfiFClass(s->fi)) == NULL)
159 return Py_BuildValue("s", xstrdup(FClass));
162 #if Py_TPFLAGS_HAVE_ITER
164 rpmfi_iter(rpmfiObject * s)
167 return (PyObject *)s;
172 rpmfi_iternext(rpmfiObject * s)
174 PyObject * result = NULL;
176 /* Reset loop indices on 1st entry. */
178 s->fi = rpmfiInit(s->fi, 0);
182 /* If more to do, return the file tuple. */
183 if (rpmfiNext(s->fi) >= 0) {
184 const char * FN = rpmfiFN(s->fi);
185 int FSize = rpmfiFSize(s->fi);
186 int FMode = rpmfiFMode(s->fi);
187 int FMtime = rpmfiFMtime(s->fi);
188 int FFlags = rpmfiFFlags(s->fi);
189 int FRdev = rpmfiFRdev(s->fi);
190 int FInode = rpmfiFInode(s->fi);
191 int FNlink = rpmfiFNlink(s->fi);
192 int FState = rpmfiFState(s->fi);
193 int VFlags = rpmfiVFlags(s->fi);
194 const char * FUser = rpmfiFUser(s->fi);
195 const char * FGroup = rpmfiFGroup(s->fi);
197 result = PyTuple_New(13);
200 PyTuple_SET_ITEM(result, 0, Py_None);
202 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
203 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
204 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
205 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
206 PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
207 PyTuple_SET_ITEM(result, 5, PyInt_FromLong(FRdev));
208 PyTuple_SET_ITEM(result, 6, PyInt_FromLong(FInode));
209 PyTuple_SET_ITEM(result, 7, PyInt_FromLong(FNlink));
210 PyTuple_SET_ITEM(result, 8, PyInt_FromLong(FState));
211 PyTuple_SET_ITEM(result, 9, PyInt_FromLong(VFlags));
214 PyTuple_SET_ITEM(result, 10, Py_None);
216 PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
217 if (FGroup == NULL) {
219 PyTuple_SET_ITEM(result, 11, Py_None);
221 PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
222 PyTuple_SET_ITEM(result, 12, rpmfi_Digest(s));
231 rpmfi_Next(rpmfiObject * s)
233 PyObject * result = NULL;
235 result = rpmfi_iternext(s);
237 if (result == NULL) {
247 rpmfi_NextD(rpmfiObject * s)
254 rpmfi_InitD(rpmfiObject * s)
261 static struct PyMethodDef rpmfi_methods[] = {
262 {"Debug", (PyCFunction)rpmfi_Debug, METH_VARARGS|METH_KEYWORDS,
264 {"FC", (PyCFunction)rpmfi_FC, METH_NOARGS,
266 {"FX", (PyCFunction)rpmfi_FX, METH_NOARGS,
268 {"DC", (PyCFunction)rpmfi_DC, METH_NOARGS,
270 {"DX", (PyCFunction)rpmfi_DX, METH_NOARGS,
272 {"BN", (PyCFunction)rpmfi_BN, METH_NOARGS,
274 {"DN", (PyCFunction)rpmfi_DN, METH_NOARGS,
276 {"FN", (PyCFunction)rpmfi_FN, METH_NOARGS,
278 {"FFlags", (PyCFunction)rpmfi_FFlags, METH_NOARGS,
280 {"VFlags", (PyCFunction)rpmfi_VFlags, METH_NOARGS,
282 {"FMode", (PyCFunction)rpmfi_FMode, METH_NOARGS,
284 {"FState", (PyCFunction)rpmfi_FState, METH_NOARGS,
286 {"MD5", (PyCFunction)rpmfi_Digest, METH_NOARGS,
288 {"Digest", (PyCFunction)rpmfi_Digest, METH_NOARGS,
290 {"FLink", (PyCFunction)rpmfi_FLink, METH_NOARGS,
292 {"FSize", (PyCFunction)rpmfi_FSize, METH_NOARGS,
294 {"FRdev", (PyCFunction)rpmfi_FRdev, METH_NOARGS,
296 {"FMtime", (PyCFunction)rpmfi_FMtime, METH_NOARGS,
298 {"FUser", (PyCFunction)rpmfi_FUser, METH_NOARGS,
300 {"FGroup", (PyCFunction)rpmfi_FGroup, METH_NOARGS,
302 {"FColor", (PyCFunction)rpmfi_FColor, METH_NOARGS,
304 {"FClass", (PyCFunction)rpmfi_FClass, METH_NOARGS,
306 {"next", (PyCFunction)rpmfi_Next, METH_NOARGS,
307 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FDigest))\n\
308 - Retrieve next file info tuple.\n" },
310 {"NextD", (PyCFunction)rpmfi_NextD, METH_NOARGS,
312 {"InitD", (PyCFunction)rpmfi_InitD, METH_NOARGS,
315 {NULL, NULL} /* sentinel */
321 rpmfi_dealloc(rpmfiObject * s)
324 s->fi = rpmfiFree(s->fi);
330 rpmfi_print(rpmfiObject * s, FILE * fp, int flags)
335 s->fi = rpmfiInit(s->fi, 0);
336 while (rpmfiNext(s->fi) >= 0)
337 fprintf(fp, "%s\n", rpmfiFN(s->fi));
341 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
343 return PyObject_GenericGetAttr(o, n);
346 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
348 return PyObject_GenericSetAttr(o, n, v);
352 rpmfi_length(rpmfiObject * s)
354 return rpmfiFC(s->fi);
358 rpmfi_subscript(rpmfiObject * s, PyObject * key)
362 if (!PyInt_Check(key)) {
363 PyErr_SetString(PyExc_TypeError, "integer expected");
367 ix = (int) PyInt_AsLong(key);
368 rpmfiSetFX(s->fi, ix);
369 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
372 static PyMappingMethods rpmfi_as_mapping = {
373 (lenfunc) rpmfi_length, /* mp_length */
374 (binaryfunc) rpmfi_subscript, /* mp_subscript */
375 (objobjargproc)0, /* mp_ass_subscript */
380 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
382 hdrObject * ho = NULL;
383 PyObject * to = NULL;
384 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
385 rpmTag tagN = RPMTAG_BASENAMES;
387 char * kwlist[] = {"header", "tag", "flags", NULL};
389 if (_rpmfi_debug < 0)
390 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
392 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
393 &hdr_Type, &ho, &to, &flags))
397 tagN = tagNumFromPyObject(to);
399 PyErr_SetString(PyExc_KeyError, "unknown header tag");
403 s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
411 static void rpmfi_free(rpmfiObject * s)
414 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
415 s->fi = rpmfiFree(s->fi);
417 PyObject_Del((PyObject *)s);
422 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
424 PyObject * s = PyType_GenericAlloc(subtype, nitems);
426 if (_rpmfi_debug < 0)
427 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
433 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
435 rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
437 /* Perform additional initialization. */
438 if (rpmfi_init(s, args, kwds) < 0) {
444 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
446 return (PyObject *)s;
451 static char rpmfi_doc[] =
454 PyTypeObject rpmfi_Type = {
455 PyObject_HEAD_INIT(&PyType_Type)
457 "rpm.fi", /* tp_name */
458 sizeof(rpmfiObject), /* tp_basicsize */
461 (destructor) rpmfi_dealloc, /* tp_dealloc */
462 (printfunc) rpmfi_print, /* tp_print */
463 (getattrfunc)0, /* tp_getattr */
464 (setattrfunc)0, /* tp_setattr */
465 (cmpfunc)0, /* tp_compare */
466 (reprfunc)0, /* tp_repr */
467 0, /* tp_as_number */
468 0, /* tp_as_sequence */
469 &rpmfi_as_mapping, /* tp_as_mapping */
470 (hashfunc)0, /* tp_hash */
471 (ternaryfunc)0, /* tp_call */
472 (reprfunc)0, /* tp_str */
473 (getattrofunc) rpmfi_getattro, /* tp_getattro */
474 (setattrofunc) rpmfi_setattro, /* tp_setattro */
475 0, /* tp_as_buffer */
476 Py_TPFLAGS_DEFAULT, /* tp_flags */
477 rpmfi_doc, /* tp_doc */
478 #if Py_TPFLAGS_HAVE_ITER
481 0, /* tp_richcompare */
482 0, /* tp_weaklistoffset */
483 (getiterfunc) rpmfi_iter, /* tp_iter */
484 (iternextfunc) rpmfi_iternext, /* tp_iternext */
485 rpmfi_methods, /* tp_methods */
490 0, /* tp_descr_get */
491 0, /* tp_descr_set */
492 0, /* tp_dictoffset */
493 (initproc) rpmfi_init, /* tp_init */
494 (allocfunc) rpmfi_alloc, /* tp_alloc */
495 (newfunc) rpmfi_new, /* tp_new */
496 (freefunc) rpmfi_free, /* tp_free */
503 rpmfi fiFromFi(rpmfiObject * s)
511 rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
521 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
523 hdrObject * ho = (hdrObject *)s;
524 PyObject * to = NULL;
525 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
526 rpmTag tagN = RPMTAG_BASENAMES;
528 char * kwlist[] = {"tag", "flags", NULL};
530 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
535 tagN = tagNumFromPyObject(to);
537 PyErr_SetString(PyExc_KeyError, "unknown header tag");
541 return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );