2 * \file python/rpmfi-py.c
10 #define PyObject_HEAD int _PyObjectHead;
15 #include "header-py.h"
23 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args)
24 /*@globals _Py_NoneStruct @*/
25 /*@modifies _Py_NoneStruct @*/
27 if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
33 rpmfi_FC(rpmfiObject * s, PyObject * args)
36 if (!PyArg_ParseTuple(args, ":FC")) return NULL;
37 return Py_BuildValue("i", rpmfiFC(s->fi));
41 rpmfi_FX(rpmfiObject * s, PyObject * args)
44 if (!PyArg_ParseTuple(args, ":FX")) return NULL;
45 return Py_BuildValue("i", rpmfiFX(s->fi));
49 rpmfi_DC(rpmfiObject * s, PyObject * args)
52 if (!PyArg_ParseTuple(args, ":DC")) return NULL;
53 return Py_BuildValue("i", rpmfiDC(s->fi));
57 rpmfi_DX(rpmfiObject * s, PyObject * args)
60 if (!PyArg_ParseTuple(args, ":DX")) return NULL;
61 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)));
73 rpmfi_DN(rpmfiObject * s, PyObject * args)
76 if (!PyArg_ParseTuple(args, ":DN")) return NULL;
77 return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
81 rpmfi_FN(rpmfiObject * s, PyObject * args)
84 if (!PyArg_ParseTuple(args, ":FN")) return NULL;
85 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
89 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
92 if (!PyArg_ParseTuple(args, ":FFlags")) return NULL;
93 return Py_BuildValue("i", rpmfiFFlags(s->fi));
97 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
100 if (!PyArg_ParseTuple(args, ":VFlags")) return NULL;
101 return Py_BuildValue("i", rpmfiVFlags(s->fi));
105 rpmfi_FMode(rpmfiObject * s, PyObject * args)
108 if (!PyArg_ParseTuple(args, ":FMode")) return NULL;
109 return Py_BuildValue("i", rpmfiFMode(s->fi));
113 rpmfi_FState(rpmfiObject * s, PyObject * args)
116 if (!PyArg_ParseTuple(args, ":FState")) return NULL;
117 return Py_BuildValue("i", rpmfiFState(s->fi));
122 rpmfi_MD5(rpmfiObject * s, PyObject * args)
125 const unsigned char * MD5;
130 if (!PyArg_ParseTuple(args, ":MD5")) return NULL;
131 MD5 = rpmfiMD5(s->fi);
132 for (i = 0, t = fmd5; i < 16; i++, t += 2)
133 sprintf(t, "%02x", MD5[i]);
135 return Py_BuildValue("s", xstrdup(fmd5));
139 rpmfi_FLink(rpmfiObject * s, PyObject * args)
142 if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
143 return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
147 rpmfi_FSize(rpmfiObject * s, PyObject * args)
150 if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
151 return Py_BuildValue("i", rpmfiFSize(s->fi));
155 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
158 if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
159 return Py_BuildValue("i", rpmfiFRdev(s->fi));
163 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
166 if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
167 return Py_BuildValue("i", rpmfiFMtime(s->fi));
171 rpmfi_FUser(rpmfiObject * s, PyObject * args)
174 if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
175 return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
179 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
182 if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
183 return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
186 #if Py_TPFLAGS_HAVE_ITER
188 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
192 return (PyObject *)s;
197 rpmfi_iternext(rpmfiObject * s)
198 /*@globals _Py_NoneStruct @*/
199 /*@modifies s, _Py_NoneStruct @*/
201 PyObject * result = NULL;
203 /* Reset loop indices on 1st entry. */
209 /* If more to do, return the file tuple. */
210 if (rpmfiNext(s->fi) >= 0) {
211 const char * FN = rpmfiFN(s->fi);
212 int FSize = rpmfiFSize(s->fi);
213 int FMode = rpmfiFMode(s->fi);
214 int FMtime = rpmfiFMtime(s->fi);
215 int FFlags = rpmfiFFlags(s->fi);
216 int FRdev = rpmfiFRdev(s->fi);
217 int FInode = rpmfiFInode(s->fi);
218 int FNlink = rpmfiFNlink(s->fi);
219 int FState = rpmfiFState(s->fi);
220 int VFlags = rpmfiVFlags(s->fi);
221 const char * FUser = rpmfiFUser(s->fi);
222 const char * FGroup = rpmfiFGroup(s->fi);
224 const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
226 char FMD5[2*16+1], *t = FMD5;
227 static const char hex[] = "0123456789abcdef";
232 for (i = 0; i < 16; i++) {
234 *t++ = hex[ (*s >> 4) & 0xf ];
235 *t++ = hex[ (*s++ ) & 0xf ];
239 result = PyTuple_New(13);
242 PyTuple_SET_ITEM(result, 0, Py_None);
244 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
245 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
246 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
247 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
248 PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
249 PyTuple_SET_ITEM(result, 5, PyInt_FromLong(FRdev));
250 PyTuple_SET_ITEM(result, 6, PyInt_FromLong(FInode));
251 PyTuple_SET_ITEM(result, 7, PyInt_FromLong(FNlink));
252 PyTuple_SET_ITEM(result, 8, PyInt_FromLong(FState));
253 PyTuple_SET_ITEM(result, 9, PyInt_FromLong(VFlags));
256 PyTuple_SET_ITEM(result, 10, Py_None);
258 PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
259 if (FGroup == NULL) {
261 PyTuple_SET_ITEM(result, 11, Py_None);
263 PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
266 PyTuple_SET_ITEM(result, 12, Py_None);
268 PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
277 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
278 /*@globals _Py_NoneStruct @*/
279 /*@modifies s, _Py_NoneStruct @*/
281 PyObject * result = NULL;
283 result = rpmfi_iternext(s);
285 if (result == NULL) {
295 rpmfi_NextD(rpmfiObject * s, PyObject * args)
298 if (!PyArg_ParseTuple(args, ":NextD"))
305 rpmfi_InitD(rpmfiObject * s, PyObject * args)
308 if (!PyArg_ParseTuple(args, ":InitD"))
316 /*@unchecked@*/ /*@observer@*/
317 static struct PyMethodDef rpmfi_methods[] = {
318 {"Debug", (PyCFunction)rpmfi_Debug, METH_VARARGS,
320 {"FC", (PyCFunction)rpmfi_FC, METH_VARARGS,
322 {"FX", (PyCFunction)rpmfi_FX, METH_VARARGS,
324 {"DC", (PyCFunction)rpmfi_DC, METH_VARARGS,
326 {"DX", (PyCFunction)rpmfi_DX, METH_VARARGS,
328 {"BN", (PyCFunction)rpmfi_BN, METH_VARARGS,
330 {"DN", (PyCFunction)rpmfi_DN, METH_VARARGS,
332 {"FN", (PyCFunction)rpmfi_FN, METH_VARARGS,
334 {"FFlags", (PyCFunction)rpmfi_FFlags, METH_VARARGS,
336 {"VFlags", (PyCFunction)rpmfi_VFlags, METH_VARARGS,
338 {"FMode", (PyCFunction)rpmfi_FMode, METH_VARARGS,
340 {"FState", (PyCFunction)rpmfi_FState, METH_VARARGS,
342 {"MD5", (PyCFunction)rpmfi_MD5, METH_VARARGS,
344 {"FLink", (PyCFunction)rpmfi_FLink, METH_VARARGS,
346 {"FSize", (PyCFunction)rpmfi_FSize, METH_VARARGS,
348 {"FRdev", (PyCFunction)rpmfi_FRdev, METH_VARARGS,
350 {"FMtime", (PyCFunction)rpmfi_FMtime, METH_VARARGS,
352 {"FUser", (PyCFunction)rpmfi_FUser, METH_VARARGS,
354 {"FGroup", (PyCFunction)rpmfi_FGroup, METH_VARARGS,
356 {"next", (PyCFunction)rpmfi_Next, METH_VARARGS,
357 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
358 - Retrieve next file info tuple.\n" },
360 {"NextD", (PyCFunction)rpmfi_NextD, METH_VARARGS,
362 {"InitD", (PyCFunction)rpmfi_InitD, METH_VARARGS,
365 {NULL, NULL} /* sentinel */
372 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
376 s->fi = rpmfiFree(s->fi);
382 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
383 /*@globals fileSystem @*/
384 /*@modifies s, fp, fileSystem @*/
390 while (rpmfiNext(s->fi) >= 0)
391 fprintf(fp, "%s\n", rpmfiFN(s->fi));
396 rpmfi_getattr(rpmfiObject * s, char * name)
399 return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
403 rpmfi_length(rpmfiObject * s)
406 return rpmfiFC(s->fi);
410 rpmfi_subscript(rpmfiObject * s, PyObject * key)
415 if (!PyInt_Check(key)) {
416 PyErr_SetString(PyExc_TypeError, "integer expected");
420 ix = (int) PyInt_AsLong(key);
421 rpmfiSetFX(s->fi, ix);
422 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
425 /*@unchecked@*/ /*@observer@*/
426 static PyMappingMethods rpmfi_as_mapping = {
427 (inquiry) rpmfi_length, /* mp_length */
428 (binaryfunc) rpmfi_subscript, /* mp_subscript */
429 (objobjargproc)0, /* mp_ass_subscript */
434 /*@unchecked@*/ /*@observer@*/
435 static char rpmfi_doc[] =
439 PyTypeObject rpmfi_Type = {
440 PyObject_HEAD_INIT(&PyType_Type)
442 "rpm.fi", /* tp_name */
443 sizeof(rpmfiObject), /* tp_basicsize */
446 (destructor) rpmfi_dealloc, /* tp_dealloc */
447 (printfunc) rpmfi_print, /* tp_print */
448 (getattrfunc) rpmfi_getattr, /* tp_getattr */
449 (setattrfunc)0, /* tp_setattr */
450 (cmpfunc)0, /* tp_compare */
451 (reprfunc)0, /* tp_repr */
452 0, /* tp_as_number */
453 0, /* tp_as_sequence */
454 &rpmfi_as_mapping, /* tp_as_mapping */
455 (hashfunc)0, /* tp_hash */
456 (ternaryfunc)0, /* tp_call */
457 (reprfunc)0, /* tp_str */
460 0, /* tp_as_buffer */
461 Py_TPFLAGS_DEFAULT, /* tp_flags */
462 rpmfi_doc, /* tp_doc */
463 #if Py_TPFLAGS_HAVE_ITER
466 0, /* tp_richcompare */
467 0, /* tp_weaklistoffset */
468 (getiterfunc) rpmfi_iter, /* tp_iter */
469 (iternextfunc) rpmfi_iternext, /* tp_iternext */
470 rpmfi_methods, /* tp_methods */
475 0, /* tp_descr_get */
476 0, /* tp_descr_set */
477 0, /* tp_dictoffset */
489 rpmfi fiFromFi(rpmfiObject * s)
497 rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
507 hdr_fiFromHeader(PyObject * s, PyObject * args)
509 hdrObject * ho = (hdrObject *)s;
510 PyObject * to = NULL;
511 rpmts ts = NULL; /* XXX FIXME: fiFromHeader should be a ts method. */
512 rpmTag tagN = RPMTAG_BASENAMES;
515 if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
518 tagN = tagNumFromPyObject(to);
520 PyErr_SetString(PyExc_KeyError, "unknown header tag");
524 return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );