rpmlib.h mass eviction
[platform/upstream/rpm.git] / python / rpmps-py.c
1 /** \ingroup py_c
2  * \file python/rpmps-py.c
3  */
4
5 #include "system.h"
6
7
8 #include "rpmps-py.h"
9
10 #include "debug.h"
11
12
13 static PyObject *
14 rpmps_Debug(rpmpsObject * s, PyObject * args, PyObject * kwds)
15 {
16     char * kwlist[] = {"debugLevel", NULL};
17     
18     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmps_debug))
19         return NULL;
20
21     Py_INCREF(Py_None);
22     return Py_None;
23 }
24
25 static int
26 rpmps_append(rpmpsObject * s, PyObject * value)
27 {
28     char *pkgNEVR, *altNEVR, *str1;
29     unsigned long ulong1;
30     int ignoreProblem;
31     rpmProblemType type;
32     fnpyKey key;
33
34     if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
35                         &pkgNEVR, &altNEVR, &key,
36                         &type, &ignoreProblem, &str1,
37                         &ulong1))
38     {
39         return -1;
40     }
41     rpmpsAppend(s->ps, type, pkgNEVR, key, str1, NULL, altNEVR, ulong1);
42     return 0;
43 }
44
45 static PyObject *
46 rpmps_iter(rpmpsObject * s)
47 {
48 if (_rpmps_debug < 0)
49 fprintf(stderr, "*** rpmps_iter(%p)\n", s);
50     s->psi = rpmpsInitIterator(s->ps);
51     Py_INCREF(s);
52     return (PyObject *)s;
53 }
54
55 static PyObject *
56 rpmps_iternext(rpmpsObject * s)
57 {
58     PyObject * result = NULL;
59
60 if (_rpmps_debug < 0)
61 fprintf(stderr, "*** rpmps_iternext(%p) ps %p psi %p\n", s, s->ps, s->psi);
62
63     /* Reset loop indices on 1st entry. */
64     if (s->psi == NULL) {
65         s->psi = rpmpsInitIterator(s->ps);
66     }
67
68     /* If more to do, return a problem set string. */
69     if (rpmpsNextIterator(s->psi) >= 0) {
70         char * ps = rpmProblemString(rpmpsGetProblem(s->psi));
71         result = Py_BuildValue("s", ps);
72         free(ps);
73     } else {
74         s->psi = rpmpsFreeIterator(s->psi);
75     }
76
77     return result;
78 }
79
80 static struct PyMethodDef rpmps_methods[] = {
81  {"Debug",      (PyCFunction)rpmps_Debug,       METH_VARARGS|METH_KEYWORDS,
82         NULL},
83   {"append",    (PyCFunction)rpmps_append,      METH_VARARGS, NULL},
84  {NULL,         NULL}           /* sentinel */
85 };
86
87 /* ---------- */
88
89 static void
90 rpmps_dealloc(rpmpsObject * s)
91 {
92 if (_rpmps_debug < 0)
93 fprintf(stderr, "*** rpmps_dealloc(%p)\n", s);
94     if (s) {
95         s->ps = rpmpsFree(s->ps);
96         PyObject_Del(s);
97     }
98 }
99
100 static int
101 rpmps_print(rpmpsObject * s, FILE * fp, int flags)
102 {
103 if (_rpmps_debug < 0)
104 fprintf(stderr, "*** rpmps_print(%p,%p,%x)\n", s, (void *)fp, flags);
105     if (s && s->ps)
106         rpmpsPrint(fp, s->ps);
107     return 0;
108 }
109
110 static PyObject * rpmps_getattro(PyObject * o, PyObject * n)
111 {
112 if (_rpmps_debug < 0)
113 fprintf(stderr, "*** rpmps_getattro(%p,%p)\n", o, n);
114     return PyObject_GenericGetAttr(o, n);
115 }
116
117 static int rpmps_setattro(PyObject * o, PyObject * n, PyObject * v)
118 {
119 if (_rpmps_debug < 0)
120 fprintf(stderr, "*** rpmps_setattro(%p,%p,%p)\n", o, n, v);
121     return PyObject_GenericSetAttr(o, n, v);
122 }
123
124 static int
125 rpmps_length(rpmpsObject * s)
126 {
127     int rc;
128     rc = rpmpsNumProblems(s->ps);
129 if (_rpmps_debug < 0)
130 fprintf(stderr, "*** rpmps_length(%p) rc %d\n", s, rc);
131     return rc;
132 }
133
134 static PyObject *
135 rpmps_subscript(rpmpsObject * s, PyObject * key)
136 {
137     PyObject * result = NULL;
138     rpmpsi psi;
139     int ix, i;
140
141     if (!PyInt_Check(key)) {
142         PyErr_SetString(PyExc_TypeError, "integer expected");
143         return NULL;
144     }
145
146     ix = (int) PyInt_AsLong(key);
147     /* XXX range check */
148
149     psi = rpmpsInitIterator(s->ps);
150     while ((i = rpmpsNextIterator(psi)) >= 0) {
151         if (i == ix) {
152             char * ps = rpmProblemString(rpmpsGetProblem(psi));
153             result = Py_BuildValue("s", ps);
154             free(ps);
155             break;
156         }
157     }
158     psi = rpmpsFreeIterator(psi);
159
160 if (_rpmps_debug < 0)
161 fprintf(stderr, "*** rpmps_subscript(%p,%p) %s\n", s, key, PyString_AsString(result));
162
163     return result;
164 }
165
166 static PyMappingMethods rpmps_as_mapping = {
167         (lenfunc) rpmps_length,         /* mp_length */
168         (binaryfunc) rpmps_subscript,   /* mp_subscript */
169 };
170
171 /** \ingroup py_c
172  */
173 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
174 {
175     char * kwlist[] = {NULL};
176
177 if (_rpmps_debug < 0)
178 fprintf(stderr, "*** rpmps_init(%p,%p,%p)\n", s, args, kwds);
179
180     if (!PyArg_ParseTupleAndKeywords(args, kwds, ":rpmps_init", kwlist))
181         return -1;
182
183     s->ps = rpmpsCreate();
184     s->psi = NULL;
185
186     return 0;
187 }
188
189 /** \ingroup py_c
190  */
191 static void rpmps_free(rpmpsObject * s)
192 {
193 if (_rpmps_debug)
194 fprintf(stderr, "%p -- ps %p\n", s, s->ps);
195     s->ps = rpmpsFree(s->ps);
196
197     PyObject_Del((PyObject *)s);
198 }
199
200 /** \ingroup py_c
201  */
202 static PyObject * rpmps_alloc(PyTypeObject * subtype, int nitems)
203 {
204     PyObject * s = PyType_GenericAlloc(subtype, nitems);
205
206 if (_rpmps_debug < 0)
207 fprintf(stderr, "*** rpmps_alloc(%p,%d) ret %p\n", subtype, nitems, s);
208     return s;
209 }
210
211 /** \ingroup py_c
212  */
213 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
214 {
215     rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
216
217     /* Perform additional initialization. */
218     if (rpmps_init(s, args, kwds) < 0) {
219         rpmps_free(s);
220         return NULL;
221     }
222
223 if (_rpmps_debug)
224 fprintf(stderr, "%p ++ ps %p\n", s, s->ps);
225
226     return (PyObject *)s;
227 }
228
229 /**
230  */
231 static char rpmps_doc[] =
232 "";
233
234 PyTypeObject rpmps_Type = {
235         PyObject_HEAD_INIT(&PyType_Type)
236         0,                              /* ob_size */
237         "rpm.ps",                       /* tp_name */
238         sizeof(rpmpsObject),            /* tp_basicsize */
239         0,                              /* tp_itemsize */
240         /* methods */
241         (destructor) rpmps_dealloc,     /* tp_dealloc */
242         (printfunc) rpmps_print,        /* tp_print */
243         (getattrfunc)0,                 /* tp_getattr */
244         (setattrfunc)0,                 /* tp_setattr */
245         (cmpfunc)0,                     /* tp_compare */
246         (reprfunc)0,                    /* tp_repr */
247         0,                              /* tp_as_number */
248         0,                              /* tp_as_sequence */
249         &rpmps_as_mapping,              /* tp_as_mapping */
250         (hashfunc)0,                    /* tp_hash */
251         (ternaryfunc)0,                 /* tp_call */
252         (reprfunc)0,                    /* tp_str */
253         (getattrofunc) rpmps_getattro,  /* tp_getattro */
254         (setattrofunc) rpmps_setattro,  /* tp_setattro */
255         0,                              /* tp_as_buffer */
256         Py_TPFLAGS_DEFAULT,             /* tp_flags */
257         rpmps_doc,                      /* tp_doc */
258 #if Py_TPFLAGS_HAVE_ITER
259         0,                              /* tp_traverse */
260         0,                              /* tp_clear */
261         (richcmpfunc)0,                 /* tp_richcompare */
262         0,                              /* tp_weaklistoffset */
263         (getiterfunc) rpmps_iter,       /* tp_iter */
264         (iternextfunc) rpmps_iternext,  /* tp_iternext */
265         rpmps_methods,                  /* tp_methods */
266         0,                              /* tp_members */
267         0,                              /* tp_getset */
268         0,                              /* tp_base */
269         0,                              /* tp_dict */
270         0,                              /* tp_descr_get */
271         0,                              /* tp_descr_set */
272         0,                              /* tp_dictoffset */
273         (initproc) rpmps_init,          /* tp_init */
274         (allocfunc) rpmps_alloc,        /* tp_alloc */
275         (newfunc) rpmps_new,            /* tp_new */
276         (freefunc) rpmps_free,          /* tp_free */
277         0,                              /* tp_is_gc */
278 #endif
279 };
280
281 /* ---------- */
282
283 rpmps psFromPs(rpmpsObject * s)
284 {
285     return s->ps;
286 }
287
288 rpmpsObject *
289 rpmps_Wrap(rpmps ps)
290 {
291     rpmpsObject * s = PyObject_New(rpmpsObject, &rpmps_Type);
292
293     if (s == NULL)
294         return NULL;
295     s->ps = ps;
296     s->psi = NULL;
297     return s;
298 }