- add rpmds/rpmfi/rpmts methods to bindings.
[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
9 #include <rpmlib.h>
10 #include "rpmfi.h"
11
12 #include "header-py.h"
13 #include "rpmfi-py.h"
14
15 #include "debug.h"
16
17 static PyObject *
18 rpmfi_Debug(rpmfiObject * s, PyObject * args)
19 {
20     if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
21     Py_INCREF(Py_None);
22     return Py_None;
23 }
24
25 static PyObject *
26 rpmfi_FC(rpmfiObject * s, PyObject * args)
27 {
28     if (!PyArg_ParseTuple(args, "")) return NULL;
29     return Py_BuildValue("i", rpmfiFC(s->fi));
30 }
31
32 static PyObject *
33 rpmfi_FX(rpmfiObject * s, PyObject * args)
34 {
35     if (!PyArg_ParseTuple(args, "")) return NULL;
36     return Py_BuildValue("i", rpmfiFX(s->fi));
37 }
38
39 static PyObject *
40 rpmfi_DC(rpmfiObject * s, PyObject * args)
41 {
42     if (!PyArg_ParseTuple(args, "")) return NULL;
43     return Py_BuildValue("i", rpmfiDC(s->fi));
44 }
45
46 static PyObject *
47 rpmfi_DX(rpmfiObject * s, PyObject * args)
48 {
49     if (!PyArg_ParseTuple(args, "")) return NULL;
50     return Py_BuildValue("i", rpmfiDX(s->fi));
51 }
52
53 static PyObject *
54 rpmfi_BN(rpmfiObject * s, PyObject * args)
55 {
56     if (!PyArg_ParseTuple(args, "")) return NULL;
57     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
58 }
59
60 static PyObject *
61 rpmfi_DN(rpmfiObject * s, PyObject * args)
62 {
63     if (!PyArg_ParseTuple(args, "")) return NULL;
64     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
65 }
66
67 static PyObject *
68 rpmfi_FN(rpmfiObject * s, PyObject * args)
69 {
70     if (!PyArg_ParseTuple(args, "")) return NULL;
71     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
72 }
73
74 static PyObject *
75 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
76 {
77     if (!PyArg_ParseTuple(args, "")) return NULL;
78     return Py_BuildValue("i", rpmfiFFlags(s->fi));
79 }
80
81 static PyObject *
82 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
83 {
84     if (!PyArg_ParseTuple(args, "")) return NULL;
85     return Py_BuildValue("i", rpmfiVFlags(s->fi));
86 }
87
88 static PyObject *
89 rpmfi_FMode(rpmfiObject * s, PyObject * args)
90 {
91     if (!PyArg_ParseTuple(args, "")) return NULL;
92     return Py_BuildValue("i", rpmfiFMode(s->fi));
93 }
94
95 static PyObject *
96 rpmfi_FState(rpmfiObject * s, PyObject * args)
97 {
98     if (!PyArg_ParseTuple(args, "")) return NULL;
99     return Py_BuildValue("i", rpmfiFState(s->fi));
100 }
101
102 /* XXX rpmfiMD5 */
103 static PyObject *
104 rpmfi_MD5(rpmfiObject * s, PyObject * args)
105 {
106     const unsigned char * md5;
107     char fmd5[33];
108     char * t;
109     int i;
110     
111     if (!PyArg_ParseTuple(args, "")) return NULL;
112     md5 = rpmfiMD5(s->fi);
113     for (i = 0, t = fmd5; i < 16; i++, t += 2)
114         sprintf(t, "%02x", md5[i]);
115     *t = '\0';
116     return Py_BuildValue("s", xstrdup(fmd5));
117 }
118
119 static PyObject *
120 rpmfi_FLink(rpmfiObject * s, PyObject * args)
121 {
122     if (!PyArg_ParseTuple(args, "")) return NULL;
123     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
124 }
125
126 static PyObject *
127 rpmfi_FSize(rpmfiObject * s, PyObject * args)
128 {
129     if (!PyArg_ParseTuple(args, "")) return NULL;
130     return Py_BuildValue("i", rpmfiFSize(s->fi));
131 }
132
133 static PyObject *
134 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
135 {
136     if (!PyArg_ParseTuple(args, "")) return NULL;
137     return Py_BuildValue("i", rpmfiFRdev(s->fi));
138 }
139
140 static PyObject *
141 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
142 {
143     if (!PyArg_ParseTuple(args, "")) return NULL;
144     return Py_BuildValue("i", rpmfiFMtime(s->fi));
145 }
146
147 static PyObject *
148 rpmfi_FUser(rpmfiObject * s, PyObject * args)
149 {
150     if (!PyArg_ParseTuple(args, "")) return NULL;
151     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
152 }
153
154 static PyObject *
155 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
156 {
157     if (!PyArg_ParseTuple(args, "")) return NULL;
158     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
159 }
160
161 #if Py_TPFLAGS_HAVE_ITER
162 static PyObject *
163 rpmfi_Next(rpmfiObject * s, PyObject * args)
164 {
165     PyObject * result = NULL;
166
167     if (rpmfiNext(s->fi) >= 0) {
168         const char * FN = rpmfiFN(s->fi);
169         int FSize = rpmfiFSize(s->fi);
170         int FMode = rpmfiFMode(s->fi);
171         int FMtime = rpmfiFMtime(s->fi);
172         int FFlags = rpmfiFFlags(s->fi);
173
174         result = PyTuple_New(5);
175         PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
176         PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
177         PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
178         PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
179         PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
180
181 /* XXX FIXME: more to return */
182     }
183     return result;
184 }
185
186 static PyObject *
187 rpmfi_Iter(rpmfiObject * s, PyObject * args)
188 {
189     rpmfiInit(s->fi, 0);
190     Py_INCREF(s);
191     return (PyObject *)s;
192 }
193 #endif
194
195 #ifdef  NOTYET
196 static PyObject *
197 rpmfi_NextD(rpmfiObject * s, PyObject * args)
198 {
199         if (!PyArg_ParseTuple(args, ""))
200                 return NULL;
201         Py_INCREF(Py_None);
202         return Py_None;
203 }
204
205 static PyObject *
206 rpmfi_InitD(rpmfiObject * s, PyObject * args)
207 {
208         if (!PyArg_ParseTuple(args, ""))
209                 return NULL;
210         Py_INCREF(Py_None);
211         return Py_None;
212 }
213 #endif
214
215 static struct PyMethodDef rpmfi_methods[] = {
216  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS,   NULL},
217  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,   NULL},
218  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,   NULL},
219  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,   NULL},
220  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,   NULL},
221  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,   NULL},
222  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,   NULL},
223  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,   NULL},
224  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,   NULL},
225  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,   NULL},
226  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,   NULL},
227  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,   NULL},
228  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,   NULL},
229  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,   NULL},
230  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,   NULL},
231  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,   NULL},
232  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,   NULL},
233  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,   NULL},
234  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,   NULL},
235 #if Py_TPFLAGS_HAVE_ITER
236  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,   NULL},
237  {"iter",       (PyCFunction)rpmfi_Iter,        METH_VARARGS,   NULL},
238 #endif
239 #ifdef  NOTYET
240  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_VARARGS,   NULL},
241  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,   NULL},
242 #endif
243  {NULL,         NULL}           /* sentinel */
244 };
245
246 /* ---------- */
247
248 static void
249 rpmfi_dealloc(rpmfiObject * s)
250 {
251     if (s) {
252         s->fi = rpmfiFree(s->fi, 1);
253         PyMem_DEL(s);
254     }
255 }
256
257 static int
258 rpmfi_print(rpmfiObject * s, FILE * fp, int flags)
259 {
260     if (!(s && s->fi))
261         return -1;
262
263     rpmfiInit(s->fi, 0);
264     while (rpmfiNext(s->fi) >= 0)
265         fprintf(fp, "%s\n", rpmfiFN(s->fi));
266     return 0;
267 }
268
269 static PyObject *
270 rpmfi_getattr(rpmfiObject * s, char * name)
271 {
272     return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
273 }
274
275 static int
276 rpmfi_length(rpmfiObject * s)
277 {
278     return rpmfiFC(s->fi);
279 }
280
281 static PyObject *
282 rpmfi_subscript(rpmfiObject * s, PyObject * key)
283 {
284     int ix;
285
286     if (!PyInt_Check(key)) {
287         PyErr_SetString(PyExc_TypeError, "integer expected");
288         return NULL;
289     }
290
291     ix = (int) PyInt_AsLong(key);
292     rpmfiSetFX(s->fi, ix);
293     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
294 }
295
296 static PyMappingMethods rpmfi_as_mapping = {
297         (inquiry) rpmfi_length,         /* mp_length */
298         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
299         (objobjargproc)0,               /* mp_ass_subscript */
300 };
301
302 PyTypeObject rpmfi_Type = {
303         PyObject_HEAD_INIT(&PyType_Type)
304         0,                              /* ob_size */
305         "rpmfi",                        /* tp_name */
306         sizeof(rpmfiObject),            /* tp_basicsize */
307         0,                              /* tp_itemsize */
308         /* methods */
309         (destructor)rpmfi_dealloc,      /* tp_dealloc */
310         (printfunc)rpmfi_print,         /* tp_print */
311         (getattrfunc)rpmfi_getattr,     /* tp_getattr */
312         (setattrfunc)0,                 /* tp_setattr */
313         (cmpfunc)0,                     /* tp_compare */
314         (reprfunc)0,                    /* tp_repr */
315         0,                              /* tp_as_number */
316         0,                              /* tp_as_sequence */
317         &rpmfi_as_mapping,              /* tp_as_mapping */
318         (hashfunc)0,                    /* tp_hash */
319         (ternaryfunc)0,                 /* tp_call */
320         (reprfunc)0,                    /* tp_str */
321         0,                              /* tp_getattro */
322         0,                              /* tp_setattro */
323         0,                              /* tp_as_buffer */
324         Py_TPFLAGS_DEFAULT,             /* tp_flags */
325         NULL,                           /* tp_doc */
326 #if Py_TPFLAGS_HAVE_ITER
327         0,                              /* tp_traverse */
328         0,                              /* tp_clear */
329         0,                              /* tp_richcompare */
330         0,                              /* tp_weaklistoffset */
331         (getiterfunc)rpmfi_Iter,        /* tp_iter */
332         (iternextfunc)rpmfi_Next,       /* tp_iternext */
333         0,                              /* tp_methods */
334         0,                              /* tp_members */
335         0,                              /* tp_getset */
336         0,                              /* tp_base */
337         0,                              /* tp_dict */
338         0,                              /* tp_descr_get */
339         0,                              /* tp_descr_set */
340         0,                              /* tp_dictoffset */
341         0,                              /* tp_init */
342         0,                              /* tp_alloc */
343         0,                              /* tp_new */
344         0,                              /* tp_free */
345         0,                              /* tp_is_gc */
346 #endif
347 };
348
349 /* ---------- */
350
351 rpmfi fiFromFi(rpmfiObject * s)
352 {
353     return s->fi;
354 }
355
356 rpmfiObject *
357 rpmfi_Wrap(rpmfi fi)
358 {
359     rpmfiObject *s = PyObject_NEW(rpmfiObject, &rpmfi_Type);
360     if (s == NULL)
361         return NULL;
362     s->fi = fi;
363     return s;
364 }
365
366 rpmfiObject *
367 hdr_fiFromHeader(PyObject * s, PyObject * args)
368 {
369     hdrObject * ho;
370
371     if (!PyArg_ParseTuple(args, "O!", &hdr_Type, &ho))
372         return NULL;
373     return rpmfi_Wrap( rpmfiNew(NULL, NULL, hdrGetHeader(ho), RPMTAG_BASENAMES, 0) );
374 }