python/rpmps-py.c

Go to the documentation of this file.
00001 
00004 /*@-modunconnomods -evalorderuncon @*/
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 /*@access FILE @*/
00017 /*@access rpmps @*/
00018 /*@access rpmProblem @*/
00019 
00020 /*@null@*/
00021 static PyObject *
00022 rpmps_Debug(/*@unused@*/ rpmpsObject * s, PyObject * args, PyObject * kwds)
00023         /*@globals _Py_NoneStruct @*/
00024         /*@modifies _Py_NoneStruct @*/
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         /*@modifies s @*/
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 /*@null@*/
00047 static PyObject *
00048 rpmps_iternext(rpmpsObject * s)
00049         /*@modifies s @*/
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     /* Reset loop indices on 1st entry. */
00058     if (!s->active) {
00059         s->ix = -1;
00060         s->active = 1;
00061     }
00062 
00063     /* If more to do, return a problem set string. */
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 /*@-fullinitblock@*/
00075 /*@unchecked@*/ /*@observer@*/
00076 static struct PyMethodDef rpmps_methods[] = {
00077  {"Debug",      (PyCFunction)rpmps_Debug,       METH_VARARGS|METH_KEYWORDS,
00078         NULL},
00079  {NULL,         NULL}           /* sentinel */
00080 };
00081 /*@=fullinitblock@*/
00082 
00083 /* ---------- */
00084 
00085 static void
00086 rpmps_dealloc(rpmpsObject * s)
00087         /*@modifies s @*/
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, /*@unused@*/ int flags)
00099         /*@globals fileSystem @*/
00100         /*@modifies s, fp, fileSystem @*/
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 /*@null@*/
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     /* XXX range check */
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         /*@modifies s @*/
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     /* XXX get rid of negative indices */
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 /*@-branchstate@*/
00211         if (ix >= ps->numProblems) {
00212             /* XXX force append for indices out of range. */
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;   /* structure assignment */
00227         }
00228 /*@=branchstate@*/
00229     }
00230 
00231     return 0;
00232 }
00233 
00234 static PyMappingMethods rpmps_as_mapping = {
00235         (inquiry) rpmps_length,         /* mp_length */
00236         (binaryfunc) rpmps_subscript,   /* mp_subscript */
00237         (objobjargproc) rpmps_ass_sub,  /* mp_ass_subscript */
00238 };
00239 
00242 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
00243         /*@modifies s @*/
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(/*@only@*/ rpmpsObject * s)
00263         /*@modifies s @*/
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 /*@null@*/
00287 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00288         /*@*/
00289 {
00290     rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
00291 
00292     /* Perform additional initialization. */
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 /*@unchecked@*/ /*@observer@*/
00307 static char rpmps_doc[] =
00308 "";
00309 
00310 /*@-fullinitblock@*/
00311 PyTypeObject rpmps_Type = {
00312         PyObject_HEAD_INIT(&PyType_Type)
00313         0,                              /* ob_size */
00314         "rpm.ps",                       /* tp_name */
00315         sizeof(rpmpsObject),            /* tp_basicsize */
00316         0,                              /* tp_itemsize */
00317         /* methods */
00318         (destructor) rpmps_dealloc,     /* tp_dealloc */
00319         (printfunc) rpmps_print,        /* tp_print */
00320         (getattrfunc)0,                 /* tp_getattr */
00321         (setattrfunc)0,                 /* tp_setattr */
00322         (cmpfunc)0,                     /* tp_compare */
00323         (reprfunc)0,                    /* tp_repr */
00324         0,                              /* tp_as_number */
00325         0,                              /* tp_as_sequence */
00326         &rpmps_as_mapping,              /* tp_as_mapping */
00327         (hashfunc)0,                    /* tp_hash */
00328         (ternaryfunc)0,                 /* tp_call */
00329         (reprfunc)0,                    /* tp_str */
00330         (getattrofunc) rpmps_getattro,  /* tp_getattro */
00331         (setattrofunc) rpmps_setattro,  /* tp_setattro */
00332         0,                              /* tp_as_buffer */
00333         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00334         rpmps_doc,                      /* tp_doc */
00335 #if Py_TPFLAGS_HAVE_ITER
00336         0,                              /* tp_traverse */
00337         0,                              /* tp_clear */
00338         (richcmpfunc)0,                 /* tp_richcompare */
00339         0,                              /* tp_weaklistoffset */
00340         (getiterfunc) rpmps_iter,       /* tp_iter */
00341         (iternextfunc) rpmps_iternext,  /* tp_iternext */
00342         rpmps_methods,                  /* tp_methods */
00343         0,                              /* tp_members */
00344         0,                              /* tp_getset */
00345         0,                              /* tp_base */
00346         0,                              /* tp_dict */
00347         0,                              /* tp_descr_get */
00348         0,                              /* tp_descr_set */
00349         0,                              /* tp_dictoffset */
00350         (initproc) rpmps_init,          /* tp_init */
00351         (allocfunc) rpmps_alloc,        /* tp_alloc */
00352         (newfunc) rpmps_new,            /* tp_new */
00353         rpmps_free,                     /* tp_free */
00354         0,                              /* tp_is_gc */
00355 #endif
00356 };
00357 /*@=fullinitblock@*/
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 /*@=modunconnomods =evalorderuncon @*/

Generated on Tue Feb 19 22:53:21 2008 for rpm by  doxygen 1.5.1