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