2 * \file python/rpmfi-py.c
18 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args)
19 /*@globals _Py_NoneStruct @*/
20 /*@modifies _Py_NoneStruct @*/
22 if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
29 rpmfi_FC(rpmfiObject * s, PyObject * args)
32 if (!PyArg_ParseTuple(args, ":FC")) return NULL;
33 return Py_BuildValue("i", rpmfiFC(s->fi));
38 rpmfi_FX(rpmfiObject * s, PyObject * args)
41 if (!PyArg_ParseTuple(args, ":FX")) return NULL;
42 return Py_BuildValue("i", rpmfiFX(s->fi));
47 rpmfi_DC(rpmfiObject * s, PyObject * args)
50 if (!PyArg_ParseTuple(args, ":DC")) return NULL;
51 return Py_BuildValue("i", rpmfiDC(s->fi));
56 rpmfi_DX(rpmfiObject * s, PyObject * args)
59 if (!PyArg_ParseTuple(args, ":DX")) return NULL;
60 return Py_BuildValue("i", rpmfiDX(s->fi));
65 rpmfi_BN(rpmfiObject * s, PyObject * args)
68 if (!PyArg_ParseTuple(args, ":BN")) return NULL;
69 return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
74 rpmfi_DN(rpmfiObject * s, PyObject * args)
77 if (!PyArg_ParseTuple(args, ":DN")) return NULL;
78 return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
83 rpmfi_FN(rpmfiObject * s, PyObject * args)
86 if (!PyArg_ParseTuple(args, ":FN")) return NULL;
87 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
92 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
95 if (!PyArg_ParseTuple(args, ":FFlags")) return NULL;
96 return Py_BuildValue("i", rpmfiFFlags(s->fi));
101 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
104 if (!PyArg_ParseTuple(args, ":VFlags")) return NULL;
105 return Py_BuildValue("i", rpmfiVFlags(s->fi));
110 rpmfi_FMode(rpmfiObject * s, PyObject * args)
113 if (!PyArg_ParseTuple(args, ":FMode")) return NULL;
114 return Py_BuildValue("i", rpmfiFMode(s->fi));
119 rpmfi_FState(rpmfiObject * s, PyObject * args)
122 if (!PyArg_ParseTuple(args, ":FState")) return NULL;
123 return Py_BuildValue("i", rpmfiFState(s->fi));
129 rpmfi_MD5(rpmfiObject * s, PyObject * args)
132 const unsigned char * MD5;
137 if (!PyArg_ParseTuple(args, ":MD5")) return NULL;
138 MD5 = rpmfiMD5(s->fi);
141 for (i = 0; i < 16; i++, t += 2)
142 sprintf(t, "%02x", MD5[i]);
144 return Py_BuildValue("s", xstrdup(fmd5));
149 rpmfi_FLink(rpmfiObject * s, PyObject * args)
152 if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
153 return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
158 rpmfi_FSize(rpmfiObject * s, PyObject * args)
161 if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
162 return Py_BuildValue("i", rpmfiFSize(s->fi));
167 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
170 if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
171 return Py_BuildValue("i", rpmfiFRdev(s->fi));
176 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
179 if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
180 return Py_BuildValue("i", rpmfiFMtime(s->fi));
185 rpmfi_FUser(rpmfiObject * s, PyObject * args)
188 if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
189 return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
194 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
197 if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
198 return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
203 rpmfi_FColor(rpmfiObject * s, PyObject * args)
206 if (!PyArg_ParseTuple(args, ":FColor")) return NULL;
207 return Py_BuildValue("i", rpmfiFColor(s->fi));
212 rpmfi_FClass(rpmfiObject * s, PyObject * args)
217 if (!PyArg_ParseTuple(args, ":FClass")) return NULL;
218 if ((FClass = rpmfiFClass(s->fi)) == NULL)
220 return Py_BuildValue("s", xstrdup(FClass));
223 #if Py_TPFLAGS_HAVE_ITER
225 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
229 return (PyObject *)s;
235 rpmfi_iternext(rpmfiObject * s)
236 /*@globals _Py_NoneStruct @*/
237 /*@modifies s, _Py_NoneStruct @*/
239 PyObject * result = NULL;
241 /* Reset loop indices on 1st entry. */
243 s->fi = rpmfiInit(s->fi, 0);
247 /* If more to do, return the file tuple. */
248 if (rpmfiNext(s->fi) >= 0) {
249 const char * FN = rpmfiFN(s->fi);
250 int FSize = rpmfiFSize(s->fi);
251 int FMode = rpmfiFMode(s->fi);
252 int FMtime = rpmfiFMtime(s->fi);
253 int FFlags = rpmfiFFlags(s->fi);
254 int FRdev = rpmfiFRdev(s->fi);
255 int FInode = rpmfiFInode(s->fi);
256 int FNlink = rpmfiFNlink(s->fi);
257 int FState = rpmfiFState(s->fi);
258 int VFlags = rpmfiVFlags(s->fi);
259 const char * FUser = rpmfiFUser(s->fi);
260 const char * FGroup = rpmfiFGroup(s->fi);
262 const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
264 char FMD5[2*16+1], *t = FMD5;
265 static const char hex[] = "0123456789abcdef";
270 for (i = 0; i < 16; i++) {
272 *t++ = hex[ (*s >> 4) & 0xf ];
273 *t++ = hex[ (*s++ ) & 0xf ];
277 result = PyTuple_New(13);
280 PyTuple_SET_ITEM(result, 0, Py_None);
282 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
283 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
284 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
285 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
286 PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
287 PyTuple_SET_ITEM(result, 5, PyInt_FromLong(FRdev));
288 PyTuple_SET_ITEM(result, 6, PyInt_FromLong(FInode));
289 PyTuple_SET_ITEM(result, 7, PyInt_FromLong(FNlink));
290 PyTuple_SET_ITEM(result, 8, PyInt_FromLong(FState));
291 PyTuple_SET_ITEM(result, 9, PyInt_FromLong(VFlags));
294 PyTuple_SET_ITEM(result, 10, Py_None);
296 PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
297 if (FGroup == NULL) {
299 PyTuple_SET_ITEM(result, 11, Py_None);
301 PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
304 PyTuple_SET_ITEM(result, 12, Py_None);
306 PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
315 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
316 /*@globals _Py_NoneStruct @*/
317 /*@modifies s, _Py_NoneStruct @*/
319 PyObject * result = NULL;
321 result = rpmfi_iternext(s);
323 if (result == NULL) {
334 rpmfi_NextD(rpmfiObject * s, PyObject * args)
337 if (!PyArg_ParseTuple(args, ":NextD"))
345 rpmfi_InitD(rpmfiObject * s, PyObject * args)
348 if (!PyArg_ParseTuple(args, ":InitD"))
356 /*@unchecked@*/ /*@observer@*/
357 static struct PyMethodDef rpmfi_methods[] = {
358 {"Debug", (PyCFunction)rpmfi_Debug, METH_VARARGS,
360 {"FC", (PyCFunction)rpmfi_FC, METH_VARARGS,
362 {"FX", (PyCFunction)rpmfi_FX, METH_VARARGS,
364 {"DC", (PyCFunction)rpmfi_DC, METH_VARARGS,
366 {"DX", (PyCFunction)rpmfi_DX, METH_VARARGS,
368 {"BN", (PyCFunction)rpmfi_BN, METH_VARARGS,
370 {"DN", (PyCFunction)rpmfi_DN, METH_VARARGS,
372 {"FN", (PyCFunction)rpmfi_FN, METH_VARARGS,
374 {"FFlags", (PyCFunction)rpmfi_FFlags, METH_VARARGS,
376 {"VFlags", (PyCFunction)rpmfi_VFlags, METH_VARARGS,
378 {"FMode", (PyCFunction)rpmfi_FMode, METH_VARARGS,
380 {"FState", (PyCFunction)rpmfi_FState, METH_VARARGS,
382 {"MD5", (PyCFunction)rpmfi_MD5, METH_VARARGS,
384 {"FLink", (PyCFunction)rpmfi_FLink, METH_VARARGS,
386 {"FSize", (PyCFunction)rpmfi_FSize, METH_VARARGS,
388 {"FRdev", (PyCFunction)rpmfi_FRdev, METH_VARARGS,
390 {"FMtime", (PyCFunction)rpmfi_FMtime, METH_VARARGS,
392 {"FUser", (PyCFunction)rpmfi_FUser, METH_VARARGS,
394 {"FGroup", (PyCFunction)rpmfi_FGroup, METH_VARARGS,
396 {"FColor", (PyCFunction)rpmfi_FColor, METH_VARARGS,
398 {"FClass", (PyCFunction)rpmfi_FClass, METH_VARARGS,
400 {"next", (PyCFunction)rpmfi_Next, METH_VARARGS,
401 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
402 - Retrieve next file info tuple.\n" },
404 {"NextD", (PyCFunction)rpmfi_NextD, METH_VARARGS,
406 {"InitD", (PyCFunction)rpmfi_InitD, METH_VARARGS,
409 {NULL, NULL} /* sentinel */
416 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
420 s->fi = rpmfiFree(s->fi);
426 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
427 /*@globals fileSystem @*/
428 /*@modifies s, fp, fileSystem @*/
433 s->fi = rpmfiInit(s->fi, 0);
434 while (rpmfiNext(s->fi) >= 0)
435 fprintf(fp, "%s\n", rpmfiFN(s->fi));
439 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
442 return PyObject_GenericGetAttr(o, n);
445 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
448 return PyObject_GenericSetAttr(o, n, v);
452 rpmfi_length(rpmfiObject * s)
455 return rpmfiFC(s->fi);
460 rpmfi_subscript(rpmfiObject * s, PyObject * key)
465 if (!PyInt_Check(key)) {
466 PyErr_SetString(PyExc_TypeError, "integer expected");
470 ix = (int) PyInt_AsLong(key);
471 rpmfiSetFX(s->fi, ix);
472 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
475 /*@unchecked@*/ /*@observer@*/
476 static PyMappingMethods rpmfi_as_mapping = {
477 (inquiry) rpmfi_length, /* mp_length */
478 (binaryfunc) rpmfi_subscript, /* mp_subscript */
479 (objobjargproc)0, /* mp_ass_subscript */
484 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
485 /*@globals rpmGlobalMacroContext @*/
486 /*@modifies s, rpmGlobalMacroContext @*/
488 hdrObject * ho = NULL;
489 PyObject * to = NULL;
490 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
491 int tagN = RPMTAG_BASENAMES;
494 if (_rpmfi_debug < 0)
495 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
497 if (!PyArg_ParseTuple(args, "O!|Oi:rpmfi_init", &hdr_Type, &ho, &to, &flags))
500 tagN = tagNumFromPyObject(to);
502 PyErr_SetString(PyExc_KeyError, "unknown header tag");
506 s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
514 static void rpmfi_free(/*@only@*/ rpmfiObject * s)
518 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
519 s->fi = rpmfiFree(s->fi);
521 PyObject_Del((PyObject *)s);
526 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
529 PyObject * s = PyType_GenericAlloc(subtype, nitems);
531 if (_rpmfi_debug < 0)
532 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
538 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
539 /*@globals rpmGlobalMacroContext @*/
540 /*@modifies rpmGlobalMacroContext @*/
542 rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
544 /* Perform additional initialization. */
545 if (rpmfi_init(s, args, kwds) < 0) {
551 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
553 return (PyObject *)s;
558 /*@unchecked@*/ /*@observer@*/
559 static char rpmfi_doc[] =
563 PyTypeObject rpmfi_Type = {
564 PyObject_HEAD_INIT(&PyType_Type)
566 "rpm.fi", /* tp_name */
567 sizeof(rpmfiObject), /* tp_basicsize */
570 (destructor) rpmfi_dealloc, /* tp_dealloc */
571 (printfunc) rpmfi_print, /* tp_print */
572 (getattrfunc)0, /* tp_getattr */
573 (setattrfunc)0, /* tp_setattr */
574 (cmpfunc)0, /* tp_compare */
575 (reprfunc)0, /* tp_repr */
576 0, /* tp_as_number */
577 0, /* tp_as_sequence */
578 &rpmfi_as_mapping, /* tp_as_mapping */
579 (hashfunc)0, /* tp_hash */
580 (ternaryfunc)0, /* tp_call */
581 (reprfunc)0, /* tp_str */
582 (getattrofunc) rpmfi_getattro, /* tp_getattro */
583 (setattrofunc) rpmfi_setattro, /* tp_setattro */
584 0, /* tp_as_buffer */
585 Py_TPFLAGS_DEFAULT, /* tp_flags */
586 rpmfi_doc, /* tp_doc */
587 #if Py_TPFLAGS_HAVE_ITER
590 0, /* tp_richcompare */
591 0, /* tp_weaklistoffset */
592 (getiterfunc) rpmfi_iter, /* tp_iter */
593 (iternextfunc) rpmfi_iternext, /* tp_iternext */
594 rpmfi_methods, /* tp_methods */
599 0, /* tp_descr_get */
600 0, /* tp_descr_set */
601 0, /* tp_dictoffset */
602 (initproc) rpmfi_init, /* tp_init */
603 (allocfunc) rpmfi_alloc, /* tp_alloc */
604 (newfunc) rpmfi_new, /* tp_new */
605 rpmfi_free, /* tp_free */
613 rpmfi fiFromFi(rpmfiObject * s)
621 rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
631 hdr_fiFromHeader(PyObject * s, PyObject * args)
633 hdrObject * ho = (hdrObject *)s;
634 PyObject * to = NULL;
635 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
636 rpmTag tagN = RPMTAG_BASENAMES;
639 if (!PyArg_ParseTuple(args, "|Oi:fiFromHeader", &to, &flags))
642 tagN = tagNumFromPyObject(to);
644 PyErr_SetString(PyExc_KeyError, "unknown header tag");
648 return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );