2 * \file python/rpmfi-py.c
18 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args, PyObject * kwds)
19 /*@globals _Py_NoneStruct @*/
20 /*@modifies _Py_NoneStruct @*/
22 char * kwlist[] = {"debugLevel", NULL};
24 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
33 rpmfi_FC(rpmfiObject * s)
36 return Py_BuildValue("i", rpmfiFC(s->fi));
41 rpmfi_FX(rpmfiObject * s)
44 return Py_BuildValue("i", rpmfiFX(s->fi));
49 rpmfi_DC(rpmfiObject * s)
52 return Py_BuildValue("i", rpmfiDC(s->fi));
57 rpmfi_DX(rpmfiObject * s)
60 return Py_BuildValue("i", rpmfiDX(s->fi));
65 rpmfi_BN(rpmfiObject * s)
68 return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
73 rpmfi_DN(rpmfiObject * s)
76 return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
81 rpmfi_FN(rpmfiObject * s)
84 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
89 rpmfi_FFlags(rpmfiObject * s)
92 return Py_BuildValue("i", rpmfiFFlags(s->fi));
97 rpmfi_VFlags(rpmfiObject * s)
100 return Py_BuildValue("i", rpmfiVFlags(s->fi));
105 rpmfi_FMode(rpmfiObject * s)
108 return Py_BuildValue("i", rpmfiFMode(s->fi));
113 rpmfi_FState(rpmfiObject * s)
116 return Py_BuildValue("i", rpmfiFState(s->fi));
122 rpmfi_MD5(rpmfiObject * s)
125 const unsigned char * MD5;
130 MD5 = rpmfiMD5(s->fi);
133 for (i = 0; i < 16; i++, t += 2)
134 sprintf(t, "%02x", MD5[i]);
136 return Py_BuildValue("s", xstrdup(fmd5));
141 rpmfi_FLink(rpmfiObject * s)
144 return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
149 rpmfi_FSize(rpmfiObject * s)
152 return Py_BuildValue("i", rpmfiFSize(s->fi));
157 rpmfi_FRdev(rpmfiObject * s)
160 return Py_BuildValue("i", rpmfiFRdev(s->fi));
165 rpmfi_FMtime(rpmfiObject * s)
168 return Py_BuildValue("i", rpmfiFMtime(s->fi));
173 rpmfi_FUser(rpmfiObject * s)
176 return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
181 rpmfi_FGroup(rpmfiObject * s)
184 return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
189 rpmfi_FColor(rpmfiObject * s)
192 return Py_BuildValue("i", rpmfiFColor(s->fi));
197 rpmfi_FClass(rpmfiObject * s)
202 if ((FClass = rpmfiFClass(s->fi)) == NULL)
204 return Py_BuildValue("s", xstrdup(FClass));
207 #if Py_TPFLAGS_HAVE_ITER
209 rpmfi_iter(rpmfiObject * s)
213 return (PyObject *)s;
219 rpmfi_iternext(rpmfiObject * s)
220 /*@globals _Py_NoneStruct @*/
221 /*@modifies s, _Py_NoneStruct @*/
223 PyObject * result = NULL;
225 /* Reset loop indices on 1st entry. */
227 s->fi = rpmfiInit(s->fi, 0);
231 /* If more to do, return the file tuple. */
232 if (rpmfiNext(s->fi) >= 0) {
233 const char * FN = rpmfiFN(s->fi);
234 int FSize = rpmfiFSize(s->fi);
235 int FMode = rpmfiFMode(s->fi);
236 int FMtime = rpmfiFMtime(s->fi);
237 int FFlags = rpmfiFFlags(s->fi);
238 int FRdev = rpmfiFRdev(s->fi);
239 int FInode = rpmfiFInode(s->fi);
240 int FNlink = rpmfiFNlink(s->fi);
241 int FState = rpmfiFState(s->fi);
242 int VFlags = rpmfiVFlags(s->fi);
243 const char * FUser = rpmfiFUser(s->fi);
244 const char * FGroup = rpmfiFGroup(s->fi);
246 const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
248 char FMD5[2*16+1], *t = FMD5;
249 static const char hex[] = "0123456789abcdef";
254 for (i = 0; i < 16; i++) {
256 *t++ = hex[ (*s >> 4) & 0xf ];
257 *t++ = hex[ (*s++ ) & 0xf ];
261 result = PyTuple_New(13);
264 PyTuple_SET_ITEM(result, 0, Py_None);
266 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
267 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
268 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
269 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
270 PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
271 PyTuple_SET_ITEM(result, 5, PyInt_FromLong(FRdev));
272 PyTuple_SET_ITEM(result, 6, PyInt_FromLong(FInode));
273 PyTuple_SET_ITEM(result, 7, PyInt_FromLong(FNlink));
274 PyTuple_SET_ITEM(result, 8, PyInt_FromLong(FState));
275 PyTuple_SET_ITEM(result, 9, PyInt_FromLong(VFlags));
278 PyTuple_SET_ITEM(result, 10, Py_None);
280 PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
281 if (FGroup == NULL) {
283 PyTuple_SET_ITEM(result, 11, Py_None);
285 PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
288 PyTuple_SET_ITEM(result, 12, Py_None);
290 PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
299 rpmfi_Next(rpmfiObject * s)
300 /*@globals _Py_NoneStruct @*/
301 /*@modifies s, _Py_NoneStruct @*/
303 PyObject * result = NULL;
305 result = rpmfi_iternext(s);
307 if (result == NULL) {
318 rpmfi_NextD(rpmfiObject * s)
327 rpmfi_InitD(rpmfiObject * s)
336 /*@unchecked@*/ /*@observer@*/
337 static struct PyMethodDef rpmfi_methods[] = {
338 {"Debug", (PyCFunction)rpmfi_Debug, METH_VARARGS|METH_KEYWORDS,
340 {"FC", (PyCFunction)rpmfi_FC, METH_NOARGS,
342 {"FX", (PyCFunction)rpmfi_FX, METH_NOARGS,
344 {"DC", (PyCFunction)rpmfi_DC, METH_NOARGS,
346 {"DX", (PyCFunction)rpmfi_DX, METH_NOARGS,
348 {"BN", (PyCFunction)rpmfi_BN, METH_NOARGS,
350 {"DN", (PyCFunction)rpmfi_DN, METH_NOARGS,
352 {"FN", (PyCFunction)rpmfi_FN, METH_NOARGS,
354 {"FFlags", (PyCFunction)rpmfi_FFlags, METH_NOARGS,
356 {"VFlags", (PyCFunction)rpmfi_VFlags, METH_NOARGS,
358 {"FMode", (PyCFunction)rpmfi_FMode, METH_NOARGS,
360 {"FState", (PyCFunction)rpmfi_FState, METH_NOARGS,
362 {"MD5", (PyCFunction)rpmfi_MD5, METH_NOARGS,
364 {"FLink", (PyCFunction)rpmfi_FLink, METH_NOARGS,
366 {"FSize", (PyCFunction)rpmfi_FSize, METH_NOARGS,
368 {"FRdev", (PyCFunction)rpmfi_FRdev, METH_NOARGS,
370 {"FMtime", (PyCFunction)rpmfi_FMtime, METH_NOARGS,
372 {"FUser", (PyCFunction)rpmfi_FUser, METH_NOARGS,
374 {"FGroup", (PyCFunction)rpmfi_FGroup, METH_NOARGS,
376 {"FColor", (PyCFunction)rpmfi_FColor, METH_NOARGS,
378 {"FClass", (PyCFunction)rpmfi_FClass, METH_NOARGS,
380 {"next", (PyCFunction)rpmfi_Next, METH_NOARGS,
381 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
382 - Retrieve next file info tuple.\n" },
384 {"NextD", (PyCFunction)rpmfi_NextD, METH_NOARGS,
386 {"InitD", (PyCFunction)rpmfi_InitD, METH_NOARGS,
389 {NULL, NULL} /* sentinel */
396 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
400 s->fi = rpmfiFree(s->fi);
406 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
407 /*@globals fileSystem @*/
408 /*@modifies s, fp, fileSystem @*/
413 s->fi = rpmfiInit(s->fi, 0);
414 while (rpmfiNext(s->fi) >= 0)
415 fprintf(fp, "%s\n", rpmfiFN(s->fi));
419 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
422 return PyObject_GenericGetAttr(o, n);
425 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
428 return PyObject_GenericSetAttr(o, n, v);
432 rpmfi_length(rpmfiObject * s)
435 return rpmfiFC(s->fi);
440 rpmfi_subscript(rpmfiObject * s, PyObject * key)
445 if (!PyInt_Check(key)) {
446 PyErr_SetString(PyExc_TypeError, "integer expected");
450 ix = (int) PyInt_AsLong(key);
451 rpmfiSetFX(s->fi, ix);
452 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
455 /*@unchecked@*/ /*@observer@*/
456 static PyMappingMethods rpmfi_as_mapping = {
457 (inquiry) rpmfi_length, /* mp_length */
458 (binaryfunc) rpmfi_subscript, /* mp_subscript */
459 (objobjargproc)0, /* mp_ass_subscript */
464 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
465 /*@globals rpmGlobalMacroContext @*/
466 /*@modifies s, rpmGlobalMacroContext @*/
468 hdrObject * ho = NULL;
469 PyObject * to = NULL;
470 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
471 int tagN = RPMTAG_BASENAMES;
473 char * kwlist[] = {"header", "tag", "flags", NULL};
475 if (_rpmfi_debug < 0)
476 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
478 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
479 &hdr_Type, &ho, &to, &flags))
483 tagN = tagNumFromPyObject(to);
485 PyErr_SetString(PyExc_KeyError, "unknown header tag");
489 s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
497 static void rpmfi_free(/*@only@*/ rpmfiObject * s)
501 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
502 s->fi = rpmfiFree(s->fi);
504 PyObject_Del((PyObject *)s);
509 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
512 PyObject * s = PyType_GenericAlloc(subtype, nitems);
514 if (_rpmfi_debug < 0)
515 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
522 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
523 /*@globals rpmGlobalMacroContext @*/
524 /*@modifies rpmGlobalMacroContext @*/
526 rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
528 /* Perform additional initialization. */
529 if (rpmfi_init(s, args, kwds) < 0) {
535 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
537 return (PyObject *)s;
542 /*@unchecked@*/ /*@observer@*/
543 static char rpmfi_doc[] =
547 PyTypeObject rpmfi_Type = {
548 PyObject_HEAD_INIT(&PyType_Type)
550 "rpm.fi", /* tp_name */
551 sizeof(rpmfiObject), /* tp_basicsize */
554 (destructor) rpmfi_dealloc, /* tp_dealloc */
555 (printfunc) rpmfi_print, /* tp_print */
556 (getattrfunc)0, /* tp_getattr */
557 (setattrfunc)0, /* tp_setattr */
558 (cmpfunc)0, /* tp_compare */
559 (reprfunc)0, /* tp_repr */
560 0, /* tp_as_number */
561 0, /* tp_as_sequence */
562 &rpmfi_as_mapping, /* tp_as_mapping */
563 (hashfunc)0, /* tp_hash */
564 (ternaryfunc)0, /* tp_call */
565 (reprfunc)0, /* tp_str */
566 (getattrofunc) rpmfi_getattro, /* tp_getattro */
567 (setattrofunc) rpmfi_setattro, /* tp_setattro */
568 0, /* tp_as_buffer */
569 Py_TPFLAGS_DEFAULT, /* tp_flags */
570 rpmfi_doc, /* tp_doc */
571 #if Py_TPFLAGS_HAVE_ITER
574 0, /* tp_richcompare */
575 0, /* tp_weaklistoffset */
576 (getiterfunc) rpmfi_iter, /* tp_iter */
577 (iternextfunc) rpmfi_iternext, /* tp_iternext */
578 rpmfi_methods, /* tp_methods */
583 0, /* tp_descr_get */
584 0, /* tp_descr_set */
585 0, /* tp_dictoffset */
586 (initproc) rpmfi_init, /* tp_init */
587 (allocfunc) rpmfi_alloc, /* tp_alloc */
588 (newfunc) rpmfi_new, /* tp_new */
589 rpmfi_free, /* tp_free */
597 rpmfi fiFromFi(rpmfiObject * s)
605 rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
615 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
617 hdrObject * ho = (hdrObject *)s;
618 PyObject * to = NULL;
619 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
620 rpmTag tagN = RPMTAG_BASENAMES;
622 char * kwlist[] = {"tag", "flags", NULL};
624 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
629 tagN = tagNumFromPyObject(to);
631 PyErr_SetString(PyExc_KeyError, "unknown header tag");
635 return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );