• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

python/rpmps-py.c

Go to the documentation of this file.
00001 
00004 /*@-modunconnomods -evalorderuncon @*/
00005 
00006 #include "system.h"
00007 
00008 #include <rpmio.h>
00009 #include <rpmiotypes.h>         /* XXX fnpyKey */
00010 #include <rpmtypes.h>
00011 #include <rpmtag.h>
00012 #define _RPMPS_INTERNAL /* XXX rpmps needs iterator */
00013 
00014 #include "rpmdebug-py.c"
00015 
00016 #include "rpmps-py.h"
00017 
00018 #include "debug.h"
00019 
00020 /*@access FILE @*/
00021 /*@access rpmps @*/
00022 /*@access rpmProblem @*/
00023 
00024 static PyObject *
00025 rpmps_iter(rpmpsObject * s)
00026         /*@modifies s @*/
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 /*@null@*/
00036 static PyObject *
00037 rpmps_iternext(rpmpsObject * s)
00038         /*@modifies s @*/
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     /* Reset loop indices on 1st entry. */
00046     if (s->psi == NULL)
00047         s->psi = rpmpsInitIterator(s->ps);
00048 
00049     /* If more to do, return a problem set string. */
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 /*@null@*/
00066 static PyObject *
00067 rpmps_Debug(/*@unused@*/ rpmpsObject * s, PyObject * args,
00068                 PyObject * kwds)
00069         /*@globals _Py_NoneStruct @*/
00070         /*@modifies _Py_NoneStruct @*/
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 /*@-fullinitblock@*/
00104 /*@unchecked@*/ /*@observer@*/
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}           /* sentinel */
00111 };
00112 /*@=fullinitblock@*/
00113 
00114 /* ---------- */
00115 
00116 static void
00117 rpmps_dealloc(rpmpsObject * s)
00118         /*@modifies s @*/
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, /*@unused@*/ int flags)
00130         /*@globals fileSystem @*/
00131         /*@modifies s, fp, fileSystem @*/
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 /*@null@*/
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     /* XXX range check */
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  /* XXX likely buggy */
00202 #if defined(PERMIT_RPMPS_SUBSCRIPT)
00203 static int
00204 rpmps_ass_sub(rpmpsObject * s, PyObject * key, PyObject * value)
00205         /*@modifies s @*/
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     /* XXX get rid of negative indices */
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 /*@-branchstate@*/
00251         if (ix >= ps->numProblems) {
00252             /* XXX force append for indices out of range. */
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;   /* structure assignment */
00267         }
00268 /*@=branchstate@*/
00269     }
00270 
00271     return 0;
00272 }
00273 #endif
00274 
00275 static PyMappingMethods rpmps_as_mapping = {
00276         (lenfunc) rpmps_length,         /* mp_length */
00277         (binaryfunc) rpmps_subscript,   /* mp_subscript */
00278 #if defined(PERMIT_RPMPS_SUBSCRIPT)
00279         (objobjargproc) rpmps_ass_sub,  /* mp_ass_subscript */
00280 #endif
00281 };
00282 
00285 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
00286         /*@modifies s @*/
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(/*@only@*/ rpmpsObject * s)
00305         /*@modifies s @*/
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 /*@null@*/
00329 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00330         /*@*/
00331 {
00332     rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
00333 
00334     /* Perform additional initialization. */
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 /*@unchecked@*/ /*@observer@*/
00349 static char rpmps_doc[] =
00350 "";
00351 
00352 /*@-fullinitblock@*/
00353 PyTypeObject rpmps_Type = {
00354         PyObject_HEAD_INIT(&PyType_Type)
00355         0,                              /* ob_size */
00356         "rpm.ps",                       /* tp_name */
00357         sizeof(rpmpsObject),            /* tp_basicsize */
00358         0,                              /* tp_itemsize */
00359         /* methods */
00360         (destructor) rpmps_dealloc,     /* tp_dealloc */
00361         (printfunc) rpmps_print,        /* tp_print */
00362         (getattrfunc)0,                 /* tp_getattr */
00363         (setattrfunc)0,                 /* tp_setattr */
00364         (cmpfunc)0,                     /* tp_compare */
00365         (reprfunc)0,                    /* tp_repr */
00366         0,                              /* tp_as_number */
00367         0,                              /* tp_as_sequence */
00368         &rpmps_as_mapping,              /* tp_as_mapping */
00369         (hashfunc)0,                    /* tp_hash */
00370         (ternaryfunc)0,                 /* tp_call */
00371         (reprfunc)0,                    /* tp_str */
00372         (getattrofunc) rpmps_getattro,  /* tp_getattro */
00373         (setattrofunc) rpmps_setattro,  /* tp_setattro */
00374         0,                              /* tp_as_buffer */
00375         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00376         rpmps_doc,                      /* tp_doc */
00377 #if Py_TPFLAGS_HAVE_ITER
00378         0,                              /* tp_traverse */
00379         0,                              /* tp_clear */
00380         (richcmpfunc)0,                 /* tp_richcompare */
00381         0,                              /* tp_weaklistoffset */
00382         (getiterfunc) rpmps_iter,       /* tp_iter */
00383         (iternextfunc) rpmps_iternext,  /* tp_iternext */
00384         rpmps_methods,                  /* tp_methods */
00385         0,                              /* tp_members */
00386         0,                              /* tp_getset */
00387         0,                              /* tp_base */
00388         0,                              /* tp_dict */
00389         0,                              /* tp_descr_get */
00390         0,                              /* tp_descr_set */
00391         0,                              /* tp_dictoffset */
00392         (initproc) rpmps_init,          /* tp_init */
00393         (allocfunc) rpmps_alloc,        /* tp_alloc */
00394         (newfunc) rpmps_new,            /* tp_new */
00395         (freefunc) rpmps_free,          /* tp_free */
00396         0,                              /* tp_is_gc */
00397 #endif
00398 };
00399 /*@=fullinitblock@*/
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 /*@=modunconnomods =evalorderuncon @*/

Generated on Fri Dec 3 2010 20:53:52 for rpm by  doxygen 1.7.2