00001
00005 #include "system.h"
00006
00007 #include "Python.h"
00008 #ifdef __LCLINT__
00009 #undef PyObject_HEAD
00010 #define PyObject_HEAD int _PyObjectHead;
00011 #endif
00012
00013 #include <rpmlib.h>
00014
00015 #include "header-py.h"
00016 #include "rpmds-py.h"
00017
00018 #include "debug.h"
00019
00020
00021
00022 static PyObject *
00023 rpmds_Debug( rpmdsObject * s, PyObject * args)
00024
00025
00026 {
00027 if (!PyArg_ParseTuple(args, "i", &_rpmds_debug)) return NULL;
00028 Py_INCREF(Py_None);
00029 return Py_None;
00030 }
00031
00032 static PyObject *
00033 rpmds_Count(rpmdsObject * s, PyObject * args)
00034
00035 {
00036 if (!PyArg_ParseTuple(args, ":Count")) return NULL;
00037 return Py_BuildValue("i", rpmdsCount(s->ds));
00038 }
00039
00040 static PyObject *
00041 rpmds_Ix(rpmdsObject * s, PyObject * args)
00042
00043 {
00044 if (!PyArg_ParseTuple(args, ":Ix")) return NULL;
00045 return Py_BuildValue("i", rpmdsIx(s->ds));
00046 }
00047
00048 static PyObject *
00049 rpmds_DNEVR(rpmdsObject * s, PyObject * args)
00050
00051 {
00052 if (!PyArg_ParseTuple(args, ":DNEVR")) return NULL;
00053 return Py_BuildValue("s", rpmdsDNEVR(s->ds));
00054 }
00055
00056 static PyObject *
00057 rpmds_N(rpmdsObject * s, PyObject * args)
00058
00059 {
00060 if (!PyArg_ParseTuple(args, ":N")) return NULL;
00061 return Py_BuildValue("s", rpmdsN(s->ds));
00062 }
00063
00064 static PyObject *
00065 rpmds_EVR(rpmdsObject * s, PyObject * args)
00066
00067 {
00068 if (!PyArg_ParseTuple(args, ":EVR")) return NULL;
00069 return Py_BuildValue("s", rpmdsEVR(s->ds));
00070 }
00071
00072 static PyObject *
00073 rpmds_Flags(rpmdsObject * s, PyObject * args)
00074
00075 {
00076 if (!PyArg_ParseTuple(args, ":Flags")) return NULL;
00077 return Py_BuildValue("i", rpmdsFlags(s->ds));
00078 }
00079
00080 static PyObject *
00081 rpmds_TagN(rpmdsObject * s, PyObject * args)
00082
00083 {
00084 if (!PyArg_ParseTuple(args, ":TagN")) return NULL;
00085 return Py_BuildValue("i", rpmdsTagN(s->ds));
00086 }
00087
00088 static PyObject *
00089 rpmds_Color(rpmdsObject * s, PyObject * args)
00090
00091 {
00092 if (!PyArg_ParseTuple(args, ":Color")) return NULL;
00093 return Py_BuildValue("i", rpmdsColor(s->ds));
00094 }
00095
00096 static PyObject *
00097 rpmds_Refs(rpmdsObject * s, PyObject * args)
00098
00099 {
00100 if (!PyArg_ParseTuple(args, ":Refs")) return NULL;
00101 return Py_BuildValue("i", rpmdsRefs(s->ds));
00102 }
00103
00104 static int
00105 rpmds_compare(rpmdsObject * a, rpmdsObject * b)
00106
00107 {
00108 return rpmdsCompare(a->ds, b->ds);
00109 }
00110
00111 static PyObject *
00112 rpmds_iter(rpmdsObject * s)
00113
00114 {
00115 Py_INCREF(s);
00116 return (PyObject *)s;
00117 }
00118
00119 static PyObject *
00120 rpmds_iternext(rpmdsObject * s)
00121
00122
00123 {
00124 PyObject * result = NULL;
00125
00126
00127 if (!s->active) {
00128 s->ds = rpmdsInit(s->ds);
00129 s->active = 1;
00130 }
00131
00132
00133 if (rpmdsNext(s->ds) >= 0) {
00134 const char * N = rpmdsN(s->ds);
00135 const char * EVR = rpmdsEVR(s->ds);
00136 int Flags = rpmdsFlags(s->ds);
00137
00138 result = PyTuple_New(3);
00139 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", N));
00140 if (EVR == NULL) {
00141 Py_INCREF(Py_None);
00142 PyTuple_SET_ITEM(result, 1, Py_None);
00143 Py_INCREF(Py_None);
00144 PyTuple_SET_ITEM(result, 2, Py_None);
00145 } else {
00146 PyTuple_SET_ITEM(result, 1, Py_BuildValue("s", EVR));
00147 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(Flags));
00148 }
00149
00150 } else
00151 s->active = 0;
00152
00153 return result;
00154 }
00155
00156 static PyObject *
00157 rpmds_Next(rpmdsObject * s, PyObject *args)
00158
00159
00160 {
00161 PyObject * result;
00162
00163 if (!PyArg_ParseTuple(args, ":Next"))
00164 return NULL;
00165
00166 result = rpmds_iternext(s);
00167
00168 if (result == NULL) {
00169 Py_INCREF(Py_None);
00170 return Py_None;
00171 }
00172 return result;
00173 }
00174
00175 static PyObject *
00176 rpmds_SetNoPromote(rpmdsObject * s, PyObject * args)
00177
00178 {
00179 int nopromote;
00180
00181 if (!PyArg_ParseTuple(args, "i:SetNoPromote", &nopromote))
00182 return NULL;
00183 return Py_BuildValue("i", rpmdsSetNoPromote(s->ds, nopromote));
00184 }
00185
00186 static PyObject *
00187 rpmds_Notify(rpmdsObject * s, PyObject * args)
00188
00189
00190 {
00191 const char * where;
00192 int rc;
00193
00194 if (!PyArg_ParseTuple(args, "si:Notify", &where, &rc))
00195 return NULL;
00196 rpmdsNotify(s->ds, where, rc);
00197 Py_INCREF(Py_None);
00198 return Py_None;
00199 }
00200
00201 #ifdef NOTYET
00202 static PyObject *
00203 rpmds_Problem(rpmdsObject * s, PyObject * args)
00204
00205 {
00206 if (!PyArg_ParseTuple(args, ":Problem"))
00207 return NULL;
00208 Py_INCREF(Py_None);
00209 return Py_None;
00210 }
00211 #endif
00212
00213
00214
00215 static struct PyMethodDef rpmds_methods[] = {
00216 {"Debug", (PyCFunction)rpmds_Debug, METH_VARARGS,
00217 NULL},
00218 {"Count", (PyCFunction)rpmds_Count, METH_VARARGS,
00219 "ds.Count -> Count - Return no. of elements.\n" },
00220 {"Ix", (PyCFunction)rpmds_Ix, METH_VARARGS,
00221 "ds.Ix -> Ix - Return current element index.\n" },
00222 {"DNEVR", (PyCFunction)rpmds_DNEVR, METH_VARARGS,
00223 "ds.DNEVR -> DNEVR - Return current DNEVR.\n" },
00224 {"N", (PyCFunction)rpmds_N, METH_VARARGS,
00225 "ds.N -> N - Return current N.\n" },
00226 {"EVR", (PyCFunction)rpmds_EVR, METH_VARARGS,
00227 "ds.EVR -> EVR - Return current EVR.\n" },
00228 {"Flags", (PyCFunction)rpmds_Flags, METH_VARARGS,
00229 "ds.Flags -> Flags - Return current Flags.\n" },
00230 {"TagN", (PyCFunction)rpmds_TagN, METH_VARARGS,
00231 "ds.TagN -> TagN - Return current TagN.\n" },
00232 {"Color", (PyCFunction)rpmds_Color, METH_VARARGS,
00233 "ds.Color -> Color - Return current Color.\n" },
00234 {"Refs", (PyCFunction)rpmds_Refs, METH_VARARGS,
00235 "ds.Refs -> Refs - Return current Refs.\n" },
00236 {"next", (PyCFunction)rpmds_Next, METH_VARARGS,
00237 "ds.next() -> (N, EVR, Flags)\n\
00238 - Retrieve next dependency triple.\n" },
00239 {"SetNoPromote",(PyCFunction)rpmds_SetNoPromote, METH_VARARGS,
00240 NULL},
00241 {"Notify", (PyCFunction)rpmds_Notify, METH_VARARGS,
00242 NULL},
00243 #ifdef NOTYET
00244 {"Problem", (PyCFunction)rpmds_Problem, METH_VARARGS,
00245 NULL},
00246 #endif
00247 {NULL, NULL}
00248 };
00249
00250
00251
00252
00253 static void
00254 rpmds_dealloc(rpmdsObject * s)
00255
00256 {
00257 if (s) {
00258 s->ds = rpmdsFree(s->ds);
00259 PyObject_Del(s);
00260 }
00261 }
00262
00263 static int
00264 rpmds_print(rpmdsObject * s, FILE * fp, int flags)
00265
00266
00267 {
00268 if (!(s && s->ds))
00269 return -1;
00270
00271 s->ds = rpmdsInit(s->ds);
00272 while (rpmdsNext(s->ds) >= 0)
00273 fprintf(fp, "%s\n", rpmdsDNEVR(s->ds));
00274 return 0;
00275 }
00276
00277 static PyObject *
00278 rpmds_getattr(rpmdsObject * s, char * name)
00279
00280 {
00281 return Py_FindMethod(rpmds_methods, (PyObject *)s, name);
00282 }
00283
00284 static int
00285 rpmds_length(rpmdsObject * s)
00286
00287 {
00288 return rpmdsCount(s->ds);
00289 }
00290
00291 static PyObject *
00292 rpmds_subscript(rpmdsObject * s, PyObject * key)
00293
00294 {
00295 int ix;
00296
00297 if (!PyInt_Check(key)) {
00298 PyErr_SetString(PyExc_TypeError, "integer expected");
00299 return NULL;
00300 }
00301
00302 ix = (int) PyInt_AsLong(key);
00303
00304 rpmdsSetIx(s->ds, ix-1);
00305 (void) rpmdsNext(s->ds);
00306 return Py_BuildValue("s", rpmdsDNEVR(s->ds));
00307 }
00308
00309 static PyMappingMethods rpmds_as_mapping = {
00310 (inquiry) rpmds_length,
00311 (binaryfunc) rpmds_subscript,
00312 (objobjargproc)0,
00313 };
00314
00317
00318 static char rpmds_doc[] =
00319 "";
00320
00321
00322 PyTypeObject rpmds_Type = {
00323 PyObject_HEAD_INIT(&PyType_Type)
00324 0,
00325 "rpm.ds",
00326 sizeof(rpmdsObject),
00327 0,
00328
00329 (destructor) rpmds_dealloc,
00330 (printfunc) rpmds_print,
00331 (getattrfunc) rpmds_getattr,
00332 (setattrfunc) 0,
00333 (cmpfunc) rpmds_compare,
00334 (reprfunc) 0,
00335 0,
00336 0,
00337 &rpmds_as_mapping,
00338 (hashfunc) 0,
00339 (ternaryfunc) 0,
00340 (reprfunc) 0,
00341 0,
00342 0,
00343 0,
00344 Py_TPFLAGS_DEFAULT,
00345 rpmds_doc,
00346 #if Py_TPFLAGS_HAVE_ITER
00347 0,
00348 0,
00349 0,
00350 0,
00351 (getiterfunc) rpmds_iter,
00352 (iternextfunc) rpmds_iternext,
00353 rpmds_methods,
00354 0,
00355 0,
00356 0,
00357 0,
00358 0,
00359 0,
00360 0,
00361 0,
00362 0,
00363 0,
00364 0,
00365 0,
00366 #endif
00367 };
00368
00369
00370
00371
00372 rpmds dsFromDs(rpmdsObject * s)
00373 {
00374 return s->ds;
00375 }
00376
00377 rpmdsObject *
00378 rpmds_Wrap(rpmds ds)
00379 {
00380 rpmdsObject * s = PyObject_New(rpmdsObject, &rpmds_Type);
00381
00382 if (s == NULL)
00383 return NULL;
00384 s->ds = ds;
00385 s->active = 0;
00386 return s;
00387 }
00388
00389 rpmdsObject *
00390 rpmds_Single( PyObject * s, PyObject * args)
00391 {
00392 PyObject * to = NULL;
00393 int tagN = RPMTAG_PROVIDENAME;
00394 const char * N;
00395 const char * EVR = NULL;
00396 int Flags = 0;
00397
00398 if (!PyArg_ParseTuple(args, "Os|si:Single", &to, &N, &EVR, &Flags))
00399 return NULL;
00400 if (to != NULL) {
00401 tagN = tagNumFromPyObject(to);
00402 if (tagN == -1) {
00403 PyErr_SetString(PyExc_KeyError, "unknown header tag");
00404 return NULL;
00405 }
00406 }
00407 return rpmds_Wrap( rpmdsSingle(tagN, N, EVR, Flags) );
00408 }
00409
00410 rpmdsObject *
00411 hdr_dsFromHeader(PyObject * s, PyObject * args)
00412 {
00413 hdrObject * ho = (hdrObject *)s;
00414 PyObject * to = NULL;
00415 rpmTag tagN = RPMTAG_REQUIRENAME;
00416 int scareMem = 0;
00417
00418 if (!PyArg_ParseTuple(args, "|O:dsFromHeader", &to))
00419 return NULL;
00420 if (to != NULL) {
00421 tagN = tagNumFromPyObject(to);
00422 if (tagN == -1) {
00423 PyErr_SetString(PyExc_KeyError, "unknown header tag");
00424 return NULL;
00425 }
00426 }
00427 return rpmds_Wrap( rpmdsNew(hdrGetHeader(ho), tagN, scareMem) );
00428 }
00429
00430 rpmdsObject *
00431 hdr_dsOfHeader(PyObject * s, PyObject * args)
00432 {
00433 hdrObject * ho = (hdrObject *)s;
00434 int tagN = RPMTAG_PROVIDENAME;
00435 int Flags = RPMSENSE_EQUAL;
00436
00437 if (!PyArg_ParseTuple(args, ":dsOfHeader"))
00438 return NULL;
00439 return rpmds_Wrap( rpmdsThis(hdrGetHeader(ho), tagN, Flags) );
00440 }