rpm
4.5
|
00001 00004 /*@-modunconnomods -evalorderuncon @*/ 00005 00006 #include "system.h" 00007 00008 #define _RPMPS_INTERNAL /* XXX rpmps needs iterator */ 00009 #include <rpmlib.h> 00010 00011 #include "rpmdebug-py.c" 00012 00013 #include "rpmps-py.h" 00014 00015 #include "debug.h" 00016 00017 /*@access FILE @*/ 00018 /*@access rpmps @*/ 00019 /*@access rpmProblem @*/ 00020 00021 static PyObject * 00022 rpmps_iter(rpmpsObject * s) 00023 /*@modifies s @*/ 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 /*@null@*/ 00033 static PyObject * 00034 rpmps_iternext(rpmpsObject * s) 00035 /*@modifies s @*/ 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 /* Reset loop indices on 1st entry. */ 00044 if (!s->active) { 00045 s->ix = -1; 00046 s->active = 1; 00047 } 00048 00049 /* If more to do, return a problem set string. */ 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 /*@null@*/ 00068 static PyObject * 00069 rpmps_Debug(/*@unused@*/ rpmpsObject * s, PyObject * args, PyObject * kwds) 00070 /*@globals _Py_NoneStruct @*/ 00071 /*@modifies _Py_NoneStruct @*/ 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 /*@-fullinitblock@*/ 00084 /*@unchecked@*/ /*@observer@*/ 00085 static struct PyMethodDef rpmps_methods[] = { 00086 {"Debug", (PyCFunction)rpmps_Debug, METH_VARARGS|METH_KEYWORDS, 00087 NULL}, 00088 {NULL, NULL} /* sentinel */ 00089 }; 00090 /*@=fullinitblock@*/ 00091 00092 /* ---------- */ 00093 00094 static void 00095 rpmps_dealloc(rpmpsObject * s) 00096 /*@modifies s @*/ 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, /*@unused@*/ int flags) 00108 /*@globals fileSystem @*/ 00109 /*@modifies s, fp, fileSystem @*/ 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 /*@null@*/ 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 /* XXX range check */ 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 /*@modifies s @*/ 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 /* XXX get rid of negative indices */ 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 /*@-branchstate@*/ 00221 if (ix >= ps->numProblems) { 00222 /* XXX force append for indices out of range. */ 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; /* structure assignment */ 00237 } 00238 /*@=branchstate@*/ 00239 } 00240 00241 return 0; 00242 } 00243 00244 static PyMappingMethods rpmps_as_mapping = { 00245 (inquiry) rpmps_length, /* mp_length */ 00246 (binaryfunc) rpmps_subscript, /* mp_subscript */ 00247 (objobjargproc) rpmps_ass_sub, /* mp_ass_subscript */ 00248 }; 00249 00252 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds) 00253 /*@modifies s @*/ 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(/*@only@*/ rpmpsObject * s) 00273 /*@modifies s @*/ 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 /*@null@*/ 00297 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds) 00298 /*@*/ 00299 { 00300 rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype); 00301 00302 /* Perform additional initialization. */ 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 /*@unchecked@*/ /*@observer@*/ 00317 static char rpmps_doc[] = 00318 ""; 00319 00320 /*@-fullinitblock@*/ 00321 PyTypeObject rpmps_Type = { 00322 PyObject_HEAD_INIT(&PyType_Type) 00323 0, /* ob_size */ 00324 "rpm.ps", /* tp_name */ 00325 sizeof(rpmpsObject), /* tp_basicsize */ 00326 0, /* tp_itemsize */ 00327 /* methods */ 00328 (destructor) rpmps_dealloc, /* tp_dealloc */ 00329 (printfunc) rpmps_print, /* tp_print */ 00330 (getattrfunc)0, /* tp_getattr */ 00331 (setattrfunc)0, /* tp_setattr */ 00332 (cmpfunc)0, /* tp_compare */ 00333 (reprfunc)0, /* tp_repr */ 00334 0, /* tp_as_number */ 00335 0, /* tp_as_sequence */ 00336 &rpmps_as_mapping, /* tp_as_mapping */ 00337 (hashfunc)0, /* tp_hash */ 00338 (ternaryfunc)0, /* tp_call */ 00339 (reprfunc)0, /* tp_str */ 00340 (getattrofunc) rpmps_getattro, /* tp_getattro */ 00341 (setattrofunc) rpmps_setattro, /* tp_setattro */ 00342 0, /* tp_as_buffer */ 00343 Py_TPFLAGS_DEFAULT, /* tp_flags */ 00344 rpmps_doc, /* tp_doc */ 00345 #if Py_TPFLAGS_HAVE_ITER 00346 0, /* tp_traverse */ 00347 0, /* tp_clear */ 00348 (richcmpfunc)0, /* tp_richcompare */ 00349 0, /* tp_weaklistoffset */ 00350 (getiterfunc) rpmps_iter, /* tp_iter */ 00351 (iternextfunc) rpmps_iternext, /* tp_iternext */ 00352 rpmps_methods, /* tp_methods */ 00353 0, /* tp_members */ 00354 0, /* tp_getset */ 00355 0, /* tp_base */ 00356 0, /* tp_dict */ 00357 0, /* tp_descr_get */ 00358 0, /* tp_descr_set */ 00359 0, /* tp_dictoffset */ 00360 (initproc) rpmps_init, /* tp_init */ 00361 (allocfunc) rpmps_alloc, /* tp_alloc */ 00362 (newfunc) rpmps_new, /* tp_new */ 00363 rpmps_free, /* tp_free */ 00364 0, /* tp_is_gc */ 00365 #endif 00366 }; 00367 /*@=fullinitblock@*/ 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 /*@=modunconnomods =evalorderuncon @*/