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