Add rpmal bindings, Grand Renaming to rpmalFoo().
[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, ":FC")) 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, ":FX")) 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, ":DC")) 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, ":DX")) 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, ":BN")) 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, ":DN")) 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, ":FN")) 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, ":FFlags")) 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, ":VFlags")) 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, ":FMode")) 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, ":FState")) 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, ":MD5")) 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, ":FLink")) 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, ":FSize")) 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, ":FRdev")) 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, ":FMtime")) 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, ":FUser")) 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, ":FGroup")) 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, ":NextD"))
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, ":InitD"))
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,
217         NULL},
218  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
219         NULL},
220  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
221         NULL},
222  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
223         NULL},
224  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
225         NULL},
226  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
227         NULL},
228  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
229         NULL},
230  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
231         NULL},
232  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
233         NULL},
234  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
235         NULL},
236  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
237         NULL},
238  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
239         NULL},
240  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
241         NULL},
242  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
243         NULL},
244  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
245         NULL},
246  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
247         NULL},
248  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
249         NULL},
250  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
251         NULL},
252  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
253         NULL},
254 #if Py_TPFLAGS_HAVE_ITER
255  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,
256         NULL},
257  {"iter",       (PyCFunction)rpmfi_Iter,        METH_VARARGS,
258         NULL},
259 #endif
260 #ifdef  NOTYET
261  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_VARARGS,
262         NULL},
263  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
264         NULL},
265 #endif
266  {NULL,         NULL}           /* sentinel */
267 };
268
269 /* ---------- */
270
271 static void
272 rpmfi_dealloc(rpmfiObject * s)
273 {
274     if (s) {
275         s->fi = rpmfiFree(s->fi, 1);
276         PyMem_DEL(s);
277     }
278 }
279
280 static int
281 rpmfi_print(rpmfiObject * s, FILE * fp, int flags)
282 {
283     if (!(s && s->fi))
284         return -1;
285
286     rpmfiInit(s->fi, 0);
287     while (rpmfiNext(s->fi) >= 0)
288         fprintf(fp, "%s\n", rpmfiFN(s->fi));
289     return 0;
290 }
291
292 static PyObject *
293 rpmfi_getattr(rpmfiObject * s, char * name)
294 {
295     return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
296 }
297
298 static int
299 rpmfi_length(rpmfiObject * s)
300 {
301     return rpmfiFC(s->fi);
302 }
303
304 static PyObject *
305 rpmfi_subscript(rpmfiObject * s, PyObject * key)
306 {
307     int ix;
308
309     if (!PyInt_Check(key)) {
310         PyErr_SetString(PyExc_TypeError, "integer expected");
311         return NULL;
312     }
313
314     ix = (int) PyInt_AsLong(key);
315     rpmfiSetFX(s->fi, ix);
316     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
317 }
318
319 static PyMappingMethods rpmfi_as_mapping = {
320         (inquiry) rpmfi_length,         /* mp_length */
321         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
322         (objobjargproc)0,               /* mp_ass_subscript */
323 };
324
325 /**
326  */
327 static char rpmfi_doc[] =
328 "";
329
330 PyTypeObject rpmfi_Type = {
331         PyObject_HEAD_INIT(&PyType_Type)
332         0,                              /* ob_size */
333         "rpm.fi",                       /* tp_name */
334         sizeof(rpmfiObject),            /* tp_basicsize */
335         0,                              /* tp_itemsize */
336         /* methods */
337         (destructor)rpmfi_dealloc,      /* tp_dealloc */
338         (printfunc)rpmfi_print,         /* tp_print */
339         (getattrfunc)rpmfi_getattr,     /* tp_getattr */
340         (setattrfunc)0,                 /* tp_setattr */
341         (cmpfunc)0,                     /* tp_compare */
342         (reprfunc)0,                    /* tp_repr */
343         0,                              /* tp_as_number */
344         0,                              /* tp_as_sequence */
345         &rpmfi_as_mapping,              /* tp_as_mapping */
346         (hashfunc)0,                    /* tp_hash */
347         (ternaryfunc)0,                 /* tp_call */
348         (reprfunc)0,                    /* tp_str */
349         0,                              /* tp_getattro */
350         0,                              /* tp_setattro */
351         0,                              /* tp_as_buffer */
352         Py_TPFLAGS_DEFAULT,             /* tp_flags */
353         rpmfi_doc,                      /* tp_doc */
354 #if Py_TPFLAGS_HAVE_ITER
355         0,                              /* tp_traverse */
356         0,                              /* tp_clear */
357         0,                              /* tp_richcompare */
358         0,                              /* tp_weaklistoffset */
359         (getiterfunc)rpmfi_Iter,        /* tp_iter */
360         (iternextfunc)rpmfi_Next,       /* tp_iternext */
361         rpmfi_methods,                  /* tp_methods */
362         0,                              /* tp_members */
363         0,                              /* tp_getset */
364         0,                              /* tp_base */
365         0,                              /* tp_dict */
366         0,                              /* tp_descr_get */
367         0,                              /* tp_descr_set */
368         0,                              /* tp_dictoffset */
369         0,                              /* tp_init */
370         0,                              /* tp_alloc */
371         0,                              /* tp_new */
372         0,                              /* tp_free */
373         0,                              /* tp_is_gc */
374 #endif
375 };
376
377 /* ---------- */
378
379 rpmfi fiFromFi(rpmfiObject * s)
380 {
381     return s->fi;
382 }
383
384 rpmfiObject *
385 rpmfi_Wrap(rpmfi fi)
386 {
387     rpmfiObject *s = PyObject_NEW(rpmfiObject, &rpmfi_Type);
388     if (s == NULL)
389         return NULL;
390     s->fi = fi;
391     return s;
392 }
393
394 rpmfiObject *
395 hdr_fiFromHeader(PyObject * s, PyObject * args)
396 {
397     hdrObject * ho;
398
399     if (!PyArg_ParseTuple(args, "O!:fiFromHeader", &hdr_Type, &ho))
400         return NULL;
401     return rpmfi_Wrap( rpmfiNew(NULL, NULL, hdrGetHeader(ho), RPMTAG_BASENAMES, 0) );
402 }