Add rpmfts-py.[ch] bindings for fts(3) from rpmio.
[platform/upstream/rpm.git] / python / rpmfi-py.c
1 /** \ingroup python
2  * \file python/rpmfi-py.c
3  */
4
5 #include "system.h"
6
7 #include "Python.h"
8 #ifdef __LCLINT__
9 #undef  PyObject_HEAD
10 #define PyObject_HEAD   int _PyObjectHead;
11 #endif
12
13 #include <rpmlib.h>
14
15 #include "header-py.h"
16 #include "rpmfi-py.h"
17
18 #include "debug.h"
19
20 /*@access rpmfi @*/
21
22 static PyObject *
23 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args)
24         /*@globals _Py_NoneStruct @*/
25         /*@modifies _Py_NoneStruct @*/
26 {
27     if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
28     Py_INCREF(Py_None);
29     return Py_None;
30 }
31
32 static PyObject *
33 rpmfi_FC(rpmfiObject * s, PyObject * args)
34         /*@*/
35 {
36     if (!PyArg_ParseTuple(args, ":FC")) return NULL;
37     return Py_BuildValue("i", rpmfiFC(s->fi));
38 }
39
40 static PyObject *
41 rpmfi_FX(rpmfiObject * s, PyObject * args)
42         /*@*/
43 {
44     if (!PyArg_ParseTuple(args, ":FX")) return NULL;
45     return Py_BuildValue("i", rpmfiFX(s->fi));
46 }
47
48 static PyObject *
49 rpmfi_DC(rpmfiObject * s, PyObject * args)
50         /*@*/
51 {
52     if (!PyArg_ParseTuple(args, ":DC")) return NULL;
53     return Py_BuildValue("i", rpmfiDC(s->fi));
54 }
55
56 static PyObject *
57 rpmfi_DX(rpmfiObject * s, PyObject * args)
58         /*@*/
59 {
60     if (!PyArg_ParseTuple(args, ":DX")) return NULL;
61     return Py_BuildValue("i", rpmfiDX(s->fi));
62 }
63
64 static PyObject *
65 rpmfi_BN(rpmfiObject * s, PyObject * args)
66         /*@*/
67 {
68     if (!PyArg_ParseTuple(args, ":BN")) return NULL;
69     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
70 }
71
72 static PyObject *
73 rpmfi_DN(rpmfiObject * s, PyObject * args)
74         /*@*/
75 {
76     if (!PyArg_ParseTuple(args, ":DN")) return NULL;
77     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
78 }
79
80 static PyObject *
81 rpmfi_FN(rpmfiObject * s, PyObject * args)
82         /*@modifies s @*/
83 {
84     if (!PyArg_ParseTuple(args, ":FN")) return NULL;
85     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
86 }
87
88 static PyObject *
89 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
90         /*@*/
91 {
92     if (!PyArg_ParseTuple(args, ":FFlags")) return NULL;
93     return Py_BuildValue("i", rpmfiFFlags(s->fi));
94 }
95
96 static PyObject *
97 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
98         /*@*/
99 {
100     if (!PyArg_ParseTuple(args, ":VFlags")) return NULL;
101     return Py_BuildValue("i", rpmfiVFlags(s->fi));
102 }
103
104 static PyObject *
105 rpmfi_FMode(rpmfiObject * s, PyObject * args)
106         /*@*/
107 {
108     if (!PyArg_ParseTuple(args, ":FMode")) return NULL;
109     return Py_BuildValue("i", rpmfiFMode(s->fi));
110 }
111
112 static PyObject *
113 rpmfi_FState(rpmfiObject * s, PyObject * args)
114         /*@*/
115 {
116     if (!PyArg_ParseTuple(args, ":FState")) return NULL;
117     return Py_BuildValue("i", rpmfiFState(s->fi));
118 }
119
120 /* XXX rpmfiMD5 */
121 static PyObject *
122 rpmfi_MD5(rpmfiObject * s, PyObject * args)
123         /*@*/
124 {
125     const unsigned char * MD5;
126     char fmd5[33];
127     char * t;
128     int i;
129     
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]);
134     *t = '\0';
135     return Py_BuildValue("s", xstrdup(fmd5));
136 }
137
138 static PyObject *
139 rpmfi_FLink(rpmfiObject * s, PyObject * args)
140         /*@*/
141 {
142     if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
143     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
144 }
145
146 static PyObject *
147 rpmfi_FSize(rpmfiObject * s, PyObject * args)
148         /*@*/
149 {
150     if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
151     return Py_BuildValue("i", rpmfiFSize(s->fi));
152 }
153
154 static PyObject *
155 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
156         /*@*/
157 {
158     if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
159     return Py_BuildValue("i", rpmfiFRdev(s->fi));
160 }
161
162 static PyObject *
163 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
164         /*@*/
165 {
166     if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
167     return Py_BuildValue("i", rpmfiFMtime(s->fi));
168 }
169
170 static PyObject *
171 rpmfi_FUser(rpmfiObject * s, PyObject * args)
172         /*@*/
173 {
174     if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
175     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
176 }
177
178 static PyObject *
179 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
180         /*@*/
181 {
182     if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
183     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
184 }
185
186 #if Py_TPFLAGS_HAVE_ITER
187 static PyObject *
188 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
189         /*@*/
190 {
191     Py_INCREF(s);
192     return (PyObject *)s;
193 }
194 #endif
195
196 static PyObject *
197 rpmfi_iternext(rpmfiObject * s)
198         /*@globals _Py_NoneStruct @*/
199         /*@modifies s, _Py_NoneStruct @*/
200 {
201     PyObject * result = NULL;
202
203     /* Reset loop indices on 1st entry. */
204     if (!s->active) {
205         rpmfiInit(s->fi, 0);
206         s->active = 1;
207     }
208
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);
223 /*@-shadow@*/
224         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
225 /*@=shadow@*/
226         char FMD5[2*16+1], *t = FMD5;
227         static const char hex[] = "0123456789abcdef";
228         int gotMD5, i;
229
230         gotMD5 = 0;
231         if (s)
232         for (i = 0; i < 16; i++) {
233             gotMD5 |= *s;
234             *t++ = hex[ (*s >> 4) & 0xf ];
235             *t++ = hex[ (*s++   ) & 0xf ];
236         }
237         *t = '\0';
238
239         result = PyTuple_New(13);
240         if (FN == NULL) {
241             Py_INCREF(Py_None);
242             PyTuple_SET_ITEM(result, 0, Py_None);
243         } else
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));
254         if (FUser == NULL) {
255             Py_INCREF(Py_None);
256             PyTuple_SET_ITEM(result, 10, Py_None);
257         } else
258             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
259         if (FGroup == NULL) {
260             Py_INCREF(Py_None);
261             PyTuple_SET_ITEM(result, 11, Py_None);
262         } else
263             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
264         if (!gotMD5) {
265             Py_INCREF(Py_None);
266             PyTuple_SET_ITEM(result, 12, Py_None);
267         } else
268             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
269
270     } else
271         s->active = 0;
272
273     return result;
274 }
275
276 static PyObject *
277 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
278         /*@globals _Py_NoneStruct @*/
279         /*@modifies s, _Py_NoneStruct @*/
280 {
281     PyObject * result = NULL;
282
283     result = rpmfi_iternext(s);
284
285     if (result == NULL) {
286         Py_INCREF(Py_None);
287         return Py_None;
288     }
289
290     return result;
291 }
292
293 #ifdef  NOTYET
294 static PyObject *
295 rpmfi_NextD(rpmfiObject * s, PyObject * args)
296         /*@*/
297 {
298         if (!PyArg_ParseTuple(args, ":NextD"))
299                 return NULL;
300         Py_INCREF(Py_None);
301         return Py_None;
302 }
303
304 static PyObject *
305 rpmfi_InitD(rpmfiObject * s, PyObject * args)
306         /*@*/
307 {
308         if (!PyArg_ParseTuple(args, ":InitD"))
309                 return NULL;
310         Py_INCREF(Py_None);
311         return Py_None;
312 }
313 #endif
314
315 /*@-fullinitblock@*/
316 /*@unchecked@*/ /*@observer@*/
317 static struct PyMethodDef rpmfi_methods[] = {
318  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS,
319         NULL},
320  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
321         NULL},
322  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
323         NULL},
324  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
325         NULL},
326  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
327         NULL},
328  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
329         NULL},
330  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
331         NULL},
332  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
333         NULL},
334  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
335         NULL},
336  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
337         NULL},
338  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
339         NULL},
340  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
341         NULL},
342  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
343         NULL},
344  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
345         NULL},
346  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
347         NULL},
348  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
349         NULL},
350  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
351         NULL},
352  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
353         NULL},
354  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
355         NULL},
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" },
359 #ifdef  NOTYET
360  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_VARARGS,
361         NULL},
362  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
363         NULL},
364 #endif
365  {NULL,         NULL}           /* sentinel */
366 };
367 /*@=fullinitblock@*/
368
369 /* ---------- */
370
371 static void
372 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
373         /*@modifies s @*/
374 {
375     if (s) {
376         s->fi = rpmfiFree(s->fi);
377         PyObject_Del(s);
378     }
379 }
380
381 static int
382 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
383         /*@globals fileSystem @*/
384         /*@modifies s, fp, fileSystem @*/
385 {
386     if (!(s && s->fi))
387         return -1;
388
389     rpmfiInit(s->fi, 0);
390     while (rpmfiNext(s->fi) >= 0)
391         fprintf(fp, "%s\n", rpmfiFN(s->fi));
392     return 0;
393 }
394
395 static PyObject *
396 rpmfi_getattr(rpmfiObject * s, char * name)
397         /*@*/
398 {
399     return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
400 }
401
402 static int
403 rpmfi_length(rpmfiObject * s)
404         /*@*/
405 {
406     return rpmfiFC(s->fi);
407 }
408
409 static PyObject *
410 rpmfi_subscript(rpmfiObject * s, PyObject * key)
411         /*@modifies s @*/
412 {
413     int ix;
414
415     if (!PyInt_Check(key)) {
416         PyErr_SetString(PyExc_TypeError, "integer expected");
417         return NULL;
418     }
419
420     ix = (int) PyInt_AsLong(key);
421     rpmfiSetFX(s->fi, ix);
422     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
423 }
424
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 */
430 };
431
432 /**
433  */
434 /*@unchecked@*/ /*@observer@*/
435 static char rpmfi_doc[] =
436 "";
437
438 /*@-fullinitblock@*/
439 PyTypeObject rpmfi_Type = {
440         PyObject_HEAD_INIT(&PyType_Type)
441         0,                              /* ob_size */
442         "rpm.fi",                       /* tp_name */
443         sizeof(rpmfiObject),            /* tp_basicsize */
444         0,                              /* tp_itemsize */
445         /* methods */
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 */
458         0,                              /* tp_getattro */
459         0,                              /* tp_setattro */
460         0,                              /* tp_as_buffer */
461         Py_TPFLAGS_DEFAULT,             /* tp_flags */
462         rpmfi_doc,                      /* tp_doc */
463 #if Py_TPFLAGS_HAVE_ITER
464         0,                              /* tp_traverse */
465         0,                              /* tp_clear */
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 */
471         0,                              /* tp_members */
472         0,                              /* tp_getset */
473         0,                              /* tp_base */
474         0,                              /* tp_dict */
475         0,                              /* tp_descr_get */
476         0,                              /* tp_descr_set */
477         0,                              /* tp_dictoffset */
478         0,                              /* tp_init */
479         0,                              /* tp_alloc */
480         0,                              /* tp_new */
481         0,                              /* tp_free */
482         0,                              /* tp_is_gc */
483 #endif
484 };
485 /*@=fullinitblock@*/
486
487 /* ---------- */
488
489 rpmfi fiFromFi(rpmfiObject * s)
490 {
491     return s->fi;
492 }
493
494 rpmfiObject *
495 rpmfi_Wrap(rpmfi fi)
496 {
497     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
498
499     if (s == NULL)
500         return NULL;
501     s->fi = fi;
502     s->active = 0;
503     return s;
504 }
505
506 rpmfiObject *
507 hdr_fiFromHeader(PyObject * s, PyObject * args)
508 {
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;
513     int scareMem = 0;
514
515     if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
516         return NULL;
517     if (to != NULL) {
518         tagN = tagNumFromPyObject(to);
519         if (tagN == -1) {
520             PyErr_SetString(PyExc_KeyError, "unknown header tag");
521             return NULL;
522         }
523     }
524     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );
525 }