Main Page Modules Data Structures File List Data Fields Globals Related Pages

# python/rpmds-py.c

Go to the documentation of this file.

    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 /*@access rpmds @*/
    00021
    00022 static PyObject *
    00023 rpmds_Debug(/*@unused@*/ rpmdsObject * s, PyObject * args)
    00024         /*@globals _Py_NoneStruct @*/
    00025         /*@modifies _Py_NoneStruct @*/
    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 int
    00089 rpmds_compare(rpmdsObject * a, rpmdsObject * b)
    00090         /*@*/
    00091 {
    00092     return rpmdsCompare(a->ds, b->ds);
    00093 }
    00094
    00095 static PyObject *
    00096 rpmds_iter(rpmdsObject * s)
    00097         /*@modifies s @*/
    00098 {
    00099     Py_INCREF(s);
    00100     return (PyObject *)s;
    00101 }
    00102
    00103 static PyObject *
    00104 rpmds_iternext(rpmdsObject * s)
    00105         /*@globals _Py_NoneStruct @*/
    00106         /*@modifies s, _Py_NoneStruct @*/
    00107 {
    00108     PyObject * result = NULL;
    00109
    00110     /* Reset loop indices on 1st entry. */
    00111     if (!s->active) {
    00112         rpmdsInit(s->ds);
    00113         s->active = 1;
    00114     }
    00115
    00116     /* If more to do, return a (N, EVR, Flags) tuple. */
    00117     if (rpmdsNext(s->ds) >= 0) {
    00118         const char * N = rpmdsN(s->ds);
    00119         const char * EVR = rpmdsEVR(s->ds);
    00120         int Flags = rpmdsFlags(s->ds);
    00121
    00122         result = PyTuple_New(3);
    00123         PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", N));
    00124         if (EVR == NULL) {
    00125             Py_INCREF(Py_None);
    00126             PyTuple_SET_ITEM(result, 1, Py_None);
    00127             Py_INCREF(Py_None);
    00128             PyTuple_SET_ITEM(result, 2, Py_None);
    00129         } else {
    00130             PyTuple_SET_ITEM(result, 1, Py_BuildValue("s", EVR));
    00131             PyTuple_SET_ITEM(result, 2, PyInt_FromLong(Flags));
    00132         }
    00133
    00134     } else
    00135         s->active = 0;
    00136
    00137     return result;
    00138 }
    00139
    00140 static PyObject *
    00141 rpmds_Next(rpmdsObject * s, PyObject *args)
    00142         /*@globals _Py_NoneStruct @*/
    00143         /*@modifies s, _Py_NoneStruct @*/
    00144 {
    00145     PyObject * result;
    00146
    00147     if (!PyArg_ParseTuple(args, ":Next"))
    00148         return NULL;
    00149
    00150     result = rpmds_iternext(s);
    00151
    00152     if (result == NULL) {
    00153         Py_INCREF(Py_None);
    00154         return Py_None;
    00155     }
    00156     return result;
    00157 }
    00158
    00159 #ifdef  NOTYET
    00160 static PyObject *
    00161 rpmds_Notify(rpmdsObject * s, PyObject * args)
    00162         /*@*/
    00163 {
    00164     if (!PyArg_ParseTuple(args, ":Notify"))
    00165         return NULL;
    00166     Py_INCREF(Py_None);
    00167     return Py_None;
    00168 }
    00169
    00170 static PyObject *
    00171 rpmds_Problem(rpmdsObject * s, PyObject * args)
    00172         /*@*/
    00173 {
    00174     if (!PyArg_ParseTuple(args, ":Problem"))
    00175         return NULL;
    00176     Py_INCREF(Py_None);
    00177     return Py_None;
    00178 }
    00179 #endif
    00180
    00181 /*@-fullinitblock@*/
    00182 /*@unchecked@*/ /*@observer@*/
    00183 static struct PyMethodDef rpmds_methods[] = {
    00184  {"Debug",      (PyCFunction)rpmds_Debug,       METH_VARARGS,
    00185         NULL},
    00186  {"Count",      (PyCFunction)rpmds_Count,       METH_VARARGS,
    00187         "ds.Count -> Count      - Return no. of elements.\n" },
    00188  {"Ix",         (PyCFunction)rpmds_Ix,          METH_VARARGS,
    00189         "ds.Ix -> Ix            - Return current element index.\n" },
    00190  {"DNEVR",      (PyCFunction)rpmds_DNEVR,       METH_VARARGS,
    00191         "ds.DNEVR -> DNEVR      - Return current DNEVR.\n" },
    00192  {"N",          (PyCFunction)rpmds_N,           METH_VARARGS,
    00193         "ds.N -> N              - Return current N.\n" },
    00194  {"EVR",        (PyCFunction)rpmds_EVR,         METH_VARARGS,
    00195         "ds.EVR -> EVR          - Return current EVR.\n" },
    00196  {"Flags",      (PyCFunction)rpmds_Flags,       METH_VARARGS,
    00197         "ds.Flags -> Flags      - Return current Flags.\n" },
    00198  {"TagN",       (PyCFunction)rpmds_TagN,        METH_VARARGS,
    00199         "ds.TagN -> TagN        - Return current TagN.\n" },
    00200  {"next",       (PyCFunction)rpmds_Next,        METH_VARARGS,
    00201 "ds.next() -> (N, EVR, Flags)\n\
    00202 - Retrieve next dependency triple.\n" },
    00203 #ifdef  NOTYET
    00204  {"Notify",     (PyCFunction)rpmds_Notify,      METH_VARARGS,
    00205         NULL},
    00206  {"Problem",    (PyCFunction)rpmds_Problem,     METH_VARARGS,
    00207         NULL},
    00208 #endif
    00209  {NULL,         NULL}           /* sentinel */
    00210 };
    00211 /*@=fullinitblock@*/
    00212
    00213 /* ---------- */
    00214
    00215 static void
    00216 rpmds_dealloc(rpmdsObject * s)
    00217         /*@modifies s @*/
    00218 {
    00219     if (s) {
    00220         s->ds = rpmdsFree(s->ds);
    00221         PyMem_DEL(s);
    00222     }
    00223 }
    00224
    00225 static int
    00226 rpmds_print(rpmdsObject * s, FILE * fp, /*@unused@*/ int flags)
    00227         /*@globals fileSystem @*/
    00228         /*@modifies s, fp, fileSystem @*/
    00229 {
    00230     if (!(s && s->ds))
    00231         return -1;
    00232
    00233     rpmdsInit(s->ds);
    00234     while (rpmdsNext(s->ds) >= 0)
    00235         fprintf(fp, "%s\n", rpmdsDNEVR(s->ds));
    00236     return 0;
    00237 }
    00238
    00239 static PyObject *
    00240 rpmds_getattr(rpmdsObject * s, char * name)
    00241         /*@*/
    00242 {
    00243     return Py_FindMethod(rpmds_methods, (PyObject *)s, name);
    00244 }
    00245
    00246 static int
    00247 rpmds_length(rpmdsObject * s)
    00248         /*@*/
    00249 {
    00250     return rpmdsCount(s->ds);
    00251 }
    00252
    00253 static PyObject *
    00254 rpmds_subscript(rpmdsObject * s, PyObject * key)
    00255         /*@modifies s @*/
    00256 {
    00257     int ix;
    00258
    00259     if (!PyInt_Check(key)) {
    00260         PyErr_SetString(PyExc_TypeError, "integer expected");
    00261         return NULL;
    00262     }
    00263
    00264     ix = (int) PyInt_AsLong(key);
    00265     rpmdsSetIx(s->ds, ix);
    00266     return Py_BuildValue("s", rpmdsDNEVR(s->ds));
    00267 }
    00268
    00269 static PyMappingMethods rpmds_as_mapping = {
    00270         (inquiry) rpmds_length,         /* mp_length */
    00271         (binaryfunc) rpmds_subscript,   /* mp_subscript */
    00272         (objobjargproc)0,               /* mp_ass_subscript */
    00273 };
    00274
    00277 /*@unchecked@*/ /*@observer@*/
    00278 static char rpmds_doc[] =
    00279 "";
    00280
    00281 /*@-fullinitblock@*/
    00282 PyTypeObject rpmds_Type = {
    00283         PyObject_HEAD_INIT(&PyType_Type)
    00284         0,                              /* ob_size */
    00285         "rpm.ds",                       /* tp_name */
    00286         sizeof(rpmdsObject),            /* tp_basicsize */
    00287         0,                              /* tp_itemsize */
    00288         /* methods */
    00289         (destructor) rpmds_dealloc,     /* tp_dealloc */
    00290         (printfunc) rpmds_print,        /* tp_print */
    00291         (getattrfunc) rpmds_getattr,    /* tp_getattr */
    00292         (setattrfunc) 0,                /* tp_setattr */
    00293         (cmpfunc) rpmds_compare,        /* tp_compare */
    00294         (reprfunc) 0,                   /* tp_repr */
    00295         0,                              /* tp_as_number */
    00296         0,                              /* tp_as_sequence */
    00297         &rpmds_as_mapping,              /* tp_as_mapping */
    00298         (hashfunc) 0,                   /* tp_hash */
    00299         (ternaryfunc) 0,                /* tp_call */
    00300         (reprfunc) 0,                   /* tp_str */
    00301         0,                              /* tp_getattro */
    00302         0,                              /* tp_setattro */
    00303         0,                              /* tp_as_buffer */
    00304         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00305         rpmds_doc,                      /* tp_doc */
    00306 #if Py_TPFLAGS_HAVE_ITER
    00307         0,                              /* tp_traverse */
    00308         0,                              /* tp_clear */
    00309         0,                              /* tp_richcompare */
    00310         0,                              /* tp_weaklistoffset */
    00311         (getiterfunc) rpmds_iter,       /* tp_iter */
    00312         (iternextfunc) rpmds_iternext,  /* tp_iternext */
    00313         rpmds_methods,                  /* tp_methods */
    00314         0,                              /* tp_members */
    00315         0,                              /* tp_getset */
    00316         0,                              /* tp_base */
    00317         0,                              /* tp_dict */
    00318         0,                              /* tp_descr_get */
    00319         0,                              /* tp_descr_set */
    00320         0,                              /* tp_dictoffset */
    00321         0,                              /* tp_init */
    00322         0,                              /* tp_alloc */
    00323         0,                              /* tp_new */
    00324         0,                              /* tp_free */
    00325         0,                              /* tp_is_gc */
    00326 #endif
    00327 };
    00328 /*@=fullinitblock@*/
    00329
    00330 /* ---------- */
    00331
    00332 rpmds dsFromDs(rpmdsObject * s)
    00333 {
    00334     return s->ds;
    00335 }
    00336
    00337 rpmdsObject *
    00338 rpmds_Wrap(rpmds ds)
    00339 {
    00340     rpmdsObject * s = PyObject_NEW(rpmdsObject, &rpmds_Type);
    00341
    00342     if (s == NULL)
    00343         return NULL;
    00344     s->ds = ds;
    00345     s->active = 0;
    00346     return s;
    00347 }
    00348
    00349 rpmdsObject *
    00350 rpmds_Single(/*@unused@*/ PyObject * s, PyObject * args)
    00351 {
    00352     PyObject * to = NULL;
    00353     int tagN = RPMTAG_PROVIDENAME;
    00354     const char * N;
    00355     const char * EVR = NULL;
    00356     int Flags = 0;
    00357
    00358     if (!PyArg_ParseTuple(args, "Os|si:Single", &to, &N, &EVR, &Flags))
    00359         return NULL;
    00360     if (to != NULL) {
    00361         tagN = tagNumFromPyObject(to);
    00362         if (tagN == -1) {
    00363             PyErr_SetString(PyExc_KeyError, "unknown header tag");
    00364             return NULL;
    00365         }
    00366     }
    00367     return rpmds_Wrap( rpmdsSingle(tagN, N, EVR, Flags) );
    00368 }
    00369
    00370 rpmdsObject *
    00371 hdr_dsFromHeader(PyObject * s, PyObject * args)
    00372 {
    00373     hdrObject * ho = (hdrObject *)s;
    00374     PyObject * to = NULL;
    00375     rpmTag tagN = RPMTAG_REQUIRENAME;
    00376     int scareMem = 0;
    00377
    00378     if (!PyArg_ParseTuple(args, "|O:dsFromHeader", &to))
    00379         return NULL;
    00380     if (to != NULL) {
    00381         tagN = tagNumFromPyObject(to);
    00382         if (tagN == -1) {
    00383             PyErr_SetString(PyExc_KeyError, "unknown header tag");
    00384             return NULL;
    00385         }
    00386     }
    00387     return rpmds_Wrap( rpmdsNew(hdrGetHeader(ho), tagN, scareMem) );
    00388 }
    00389
    00390 rpmdsObject *
    00391 hdr_dsOfHeader(PyObject * s, PyObject * args)
    00392 {
    00393     hdrObject * ho = (hdrObject *)s;
    00394     int tagN = RPMTAG_PROVIDENAME;
    00395     int Flags = RPMSENSE_EQUAL;
    00396
    00397     if (!PyArg_ParseTuple(args, ":dsOfHeader"))
    00398         return NULL;
    00399     return rpmds_Wrap( rpmdsThis(hdrGetHeader(ho), tagN, Flags) );
    00400 }