00001
00004
00005
00006 #include "system.h"
00007
00008 #include <rpmio.h>
00009 #include <rpmiotypes.h>
00010 #include <rpmtypes.h>
00011 #include <rpmtag.h>
00012 #define _RPMPS_INTERNAL
00013
00014 #include "rpmdebug-py.c"
00015
00016 #include "rpmps-py.h"
00017
00018 #include "debug.h"
00019
00020
00021
00022
00023
00024 static PyObject *
00025 rpmps_iter(rpmpsObject * s)
00026
00027 {
00028 if (_rpmps_debug < 0)
00029 fprintf(stderr, "*** rpmps_iter(%p)\n", s);
00030 s->psi = rpmpsInitIterator(s->ps);
00031 Py_INCREF(s);
00032 return (PyObject *)s;
00033 }
00034
00035
00036 static PyObject *
00037 rpmps_iternext(rpmpsObject * s)
00038
00039 {
00040 PyObject * result = NULL;
00041
00042 if (_rpmps_debug < 0)
00043 fprintf(stderr, "*** rpmps_iternext(%p) ps %p psi %p\n", s, s->ps, s->psi);
00044
00045
00046 if (s->psi == NULL)
00047 s->psi = rpmpsInitIterator(s->ps);
00048
00049
00050 if (rpmpsNextIterator(s->psi) >= 0)
00051 result = Py_BuildValue("s", rpmProblemString(rpmpsProblem(s->psi)));
00052 else
00053 s->psi = rpmpsFreeIterator(s->psi);
00054
00055 return result;
00056 }
00057
00064
00065
00066 static PyObject *
00067 rpmps_Debug( rpmpsObject * s, PyObject * args,
00068 PyObject * kwds)
00069
00070
00071 {
00072 char * kwlist[] = {"debugLevel", NULL};
00073
00074 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmps_debug))
00075 return NULL;
00076
00077 Py_INCREF(Py_None);
00078 return Py_None;
00079 }
00080
00081 static int
00082 rpmps_Append(rpmpsObject * s, PyObject * value)
00083 {
00084 char *pkgNEVR, *altNEVR, *str1;
00085 unsigned long ulong1;
00086 int ignoreProblem;
00087 rpmProblemType type;
00088 fnpyKey key;
00089
00090 if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
00091 &pkgNEVR, &altNEVR, &key,
00092 &type, &ignoreProblem, &str1,
00093 &ulong1))
00094 {
00095 return -1;
00096 }
00097 rpmpsAppend(s->ps, type, pkgNEVR, key, str1, NULL, altNEVR, ulong1);
00098 return 0;
00099 }
00100
00103
00104
00105 static struct PyMethodDef rpmps_methods[] = {
00106 {"Debug", (PyCFunction)rpmps_Debug, METH_VARARGS|METH_KEYWORDS,
00107 NULL},
00108 {"Append", (PyCFunction)rpmps_Append, METH_VARARGS|METH_KEYWORDS,
00109 NULL},
00110 {NULL, NULL}
00111 };
00112
00113
00114
00115
00116 static void
00117 rpmps_dealloc(rpmpsObject * s)
00118
00119 {
00120 if (_rpmps_debug < 0)
00121 fprintf(stderr, "*** rpmps_dealloc(%p)\n", s);
00122 if (s) {
00123 s->ps = rpmpsFree(s->ps);
00124 PyObject_Del(s);
00125 }
00126 }
00127
00128 static int
00129 rpmps_print(rpmpsObject * s, FILE * fp, int flags)
00130
00131
00132 {
00133 if (_rpmps_debug < 0)
00134 fprintf(stderr, "*** rpmps_print(%p,%p,%x)\n", s, (void *)fp, flags);
00135 if (s && s->ps)
00136 rpmpsPrint(fp, s->ps);
00137 return 0;
00138 }
00139
00140 static PyObject * rpmps_getattro(PyObject * o, PyObject * n)
00141
00142 {
00143 if (_rpmps_debug < 0)
00144 fprintf(stderr, "*** rpmps_getattro(%p,%p)\n", o, n);
00145 return PyObject_GenericGetAttr(o, n);
00146 }
00147
00148 static int rpmps_setattro(PyObject * o, PyObject * n, PyObject * v)
00149
00150 {
00151 if (_rpmps_debug < 0)
00152 fprintf(stderr, "*** rpmps_setattro(%p,%p,%p)\n", o, n, v);
00153 return PyObject_GenericSetAttr(o, n, v);
00154 }
00155
00156 static int
00157 rpmps_length(rpmpsObject * s)
00158
00159 {
00160 int rc;
00161 rc = rpmpsNumProblems(s->ps);
00162 if (_rpmps_debug < 0)
00163 fprintf(stderr, "*** rpmps_length(%p) rc %d\n", s, rc);
00164 return rc;
00165 }
00166
00167
00168 static PyObject *
00169 rpmps_subscript(rpmpsObject * s, PyObject * key)
00170
00171 {
00172 PyObject * result = NULL;
00173 rpmpsi psi;
00174 int ix;
00175 int i;
00176
00177 if (!PyInt_Check(key)) {
00178 if (_rpmps_debug < 0)
00179 fprintf(stderr, "*** rpmps_subscript(%p[%s],%p[%s])\n", s, lbl(s), key, lbl(key));
00180 PyErr_SetString(PyExc_TypeError, "integer expected");
00181 return NULL;
00182 }
00183
00184 ix = (int) PyInt_AsLong(key);
00185
00186
00187 psi = rpmpsInitIterator(s->ps);
00188 while ((i = rpmpsNextIterator(psi)) >= 0) {
00189 if (i != ix)
00190 continue;
00191 result = Py_BuildValue("s", rpmProblemString(rpmpsProblem(psi)));
00192 if (_rpmps_debug < 0)
00193 fprintf(stderr, "*** rpmps_subscript(%p,%p) %s\n", s, key, PyString_AsString(result));
00194 break;
00195 }
00196 psi = rpmpsFreeIterator(psi);
00197
00198 return result;
00199 }
00200
00201 #define PERMIT_RPMPS_SUBSCRIPT
00202 #if defined(PERMIT_RPMPS_SUBSCRIPT)
00203 static int
00204 rpmps_ass_sub(rpmpsObject * s, PyObject * key, PyObject * value)
00205
00206 {
00207 rpmps ps;
00208 int ix;
00209
00210 if (!PyArg_Parse(key, "i:ass_sub", &ix)) {
00211 PyErr_SetString(PyExc_TypeError, "rpmps key type must be integer");
00212 return -1;
00213 }
00214
00215
00216 if (ix < 0) ix = -ix;
00217
00218 ps = s->ps;
00219
00220 if (_rpmps_debug < 0)
00221 fprintf(stderr, "*** rpmps_ass_sub(%p[%s],%p[%s],%p[%s]) ps %p[%d:%d:%d]\n", s, lbl(s), key, lbl(key), value, lbl(value), ps, ix, ps->numProblems, ps->numProblemsAlloced);
00222
00223 if (value == NULL) {
00224 if (ix < ps->numProblems) {
00225 rpmProblem op = ps->probs + ix;
00226
00227 op->pkgNEVR = _free(op->pkgNEVR);
00228 op->altNEVR = _free(op->altNEVR);
00229 op->str1 = _free(op->str1);
00230
00231 if ((ix+1) == ps->numProblems)
00232 memset(op, 0, sizeof(*op));
00233 else
00234 memmove(op, op+1, (ps->numProblems - ix) * sizeof(*op));
00235 if (ps->numProblems > 0)
00236 ps->numProblems--;
00237 }
00238 } else {
00239 rpmProblem p = memset(alloca(sizeof(*p)), 0, sizeof(*p));
00240 unsigned long ulong1 = p->ulong1;
00241
00242 if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
00243 &p->pkgNEVR, &p->altNEVR, &p->key,
00244 &p->type, &p->ignoreProblem, &p->str1,
00245 &ulong1))
00246 {
00247 return -1;
00248 }
00249
00250
00251 if (ix >= ps->numProblems) {
00252
00253 rpmpsAppend(s->ps, p->type, p->pkgNEVR, p->key,
00254 p->str1, NULL, p->altNEVR, ulong1);
00255 } else {
00256 rpmProblem op = ps->probs + ix;
00257
00258 op->pkgNEVR = _free(op->pkgNEVR);
00259 op->altNEVR = _free(op->altNEVR);
00260 op->str1 = _free(op->str1);
00261
00262 p->pkgNEVR = (p->pkgNEVR && *p->pkgNEVR ? xstrdup(p->pkgNEVR) : NULL);
00263 p->altNEVR = (p->altNEVR && *p->altNEVR ? xstrdup(p->altNEVR) : NULL);
00264 p->str1 = (p->str1 && *p->str1 ? xstrdup(p->str1) : NULL);
00265
00266 *op = *p;
00267 }
00268
00269 }
00270
00271 return 0;
00272 }
00273 #endif
00274
00275 static PyMappingMethods rpmps_as_mapping = {
00276 (lenfunc) rpmps_length,
00277 (binaryfunc) rpmps_subscript,
00278 #if defined(PERMIT_RPMPS_SUBSCRIPT)
00279 (objobjargproc) rpmps_ass_sub,
00280 #endif
00281 };
00282
00285 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
00286
00287 {
00288 char * kwlist[] = {NULL};
00289
00290 if (_rpmps_debug < 0)
00291 fprintf(stderr, "*** rpmps_init(%p,%p,%p)\n", s, args, kwds);
00292
00293 if (!PyArg_ParseTupleAndKeywords(args, kwds, ":rpmps_init", kwlist))
00294 return -1;
00295
00296 s->ps = rpmpsCreate();
00297 s->psi = NULL;
00298
00299 return 0;
00300 }
00301
00304 static void rpmps_free( rpmpsObject * s)
00305
00306 {
00307 if (_rpmps_debug)
00308 fprintf(stderr, "%p -- ps %p\n", s, s->ps);
00309 s->ps = rpmpsFree(s->ps);
00310
00311 PyObject_Del((PyObject *)s);
00312 }
00313
00316 static PyObject * rpmps_alloc(PyTypeObject * subtype, int nitems)
00317
00318 {
00319 PyObject * s = PyType_GenericAlloc(subtype, nitems);
00320
00321 if (_rpmps_debug < 0)
00322 fprintf(stderr, "*** rpmps_alloc(%p,%d) ret %p\n", subtype, nitems, s);
00323 return s;
00324 }
00325
00328
00329 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00330
00331 {
00332 rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
00333
00334
00335 if (rpmps_init(s, args, kwds) < 0) {
00336 rpmps_free(s);
00337 return NULL;
00338 }
00339
00340 if (_rpmps_debug)
00341 fprintf(stderr, "%p ++ ps %p\n", s, s->ps);
00342
00343 return (PyObject *)s;
00344 }
00345
00348
00349 static char rpmps_doc[] =
00350 "";
00351
00352
00353 PyTypeObject rpmps_Type = {
00354 PyObject_HEAD_INIT(&PyType_Type)
00355 0,
00356 "rpm.ps",
00357 sizeof(rpmpsObject),
00358 0,
00359
00360 (destructor) rpmps_dealloc,
00361 (printfunc) rpmps_print,
00362 (getattrfunc)0,
00363 (setattrfunc)0,
00364 (cmpfunc)0,
00365 (reprfunc)0,
00366 0,
00367 0,
00368 &rpmps_as_mapping,
00369 (hashfunc)0,
00370 (ternaryfunc)0,
00371 (reprfunc)0,
00372 (getattrofunc) rpmps_getattro,
00373 (setattrofunc) rpmps_setattro,
00374 0,
00375 Py_TPFLAGS_DEFAULT,
00376 rpmps_doc,
00377 #if Py_TPFLAGS_HAVE_ITER
00378 0,
00379 0,
00380 (richcmpfunc)0,
00381 0,
00382 (getiterfunc) rpmps_iter,
00383 (iternextfunc) rpmps_iternext,
00384 rpmps_methods,
00385 0,
00386 0,
00387 0,
00388 0,
00389 0,
00390 0,
00391 0,
00392 (initproc) rpmps_init,
00393 (allocfunc) rpmps_alloc,
00394 (newfunc) rpmps_new,
00395 (freefunc) rpmps_free,
00396 0,
00397 #endif
00398 };
00399
00400
00401
00402
00403 rpmps psFromPs(rpmpsObject * s)
00404 {
00405 return s->ps;
00406 }
00407
00408 rpmpsObject *
00409 rpmps_Wrap(rpmps ps)
00410 {
00411 rpmpsObject * s = PyObject_New(rpmpsObject, &rpmps_Type);
00412
00413 if (s == NULL)
00414 return NULL;
00415 s->ps = ps;
00416 s->psi = NULL;
00417 return s;
00418 }
00419