python/rpmfi-py.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmlib.h>
00008 
00009 #include "header-py.h"
00010 #include "rpmfi-py.h"
00011 
00012 #include "debug.h"
00013 
00014 /*@access rpmfi @*/
00015 
00016 /*@null@*/
00017 static PyObject *
00018 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args, PyObject * kwds)
00019         /*@globals _Py_NoneStruct @*/
00020         /*@modifies _Py_NoneStruct @*/
00021 {
00022     char * kwlist[] = {"debugLevel", NULL};
00023 
00024     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
00025         return NULL;
00026 
00027     Py_INCREF(Py_None);
00028     return Py_None;
00029 }
00030 
00031 /*@null@*/
00032 static PyObject *
00033 rpmfi_FC(rpmfiObject * s)
00034         /*@*/
00035 {
00036     return Py_BuildValue("i", rpmfiFC(s->fi));
00037 }
00038 
00039 /*@null@*/
00040 static PyObject *
00041 rpmfi_FX(rpmfiObject * s)
00042         /*@*/
00043 {
00044     return Py_BuildValue("i", rpmfiFX(s->fi));
00045 }
00046 
00047 /*@null@*/
00048 static PyObject *
00049 rpmfi_DC(rpmfiObject * s)
00050         /*@*/
00051 {
00052     return Py_BuildValue("i", rpmfiDC(s->fi));
00053 }
00054 
00055 /*@null@*/
00056 static PyObject *
00057 rpmfi_DX(rpmfiObject * s)
00058         /*@*/
00059 {
00060     return Py_BuildValue("i", rpmfiDX(s->fi));
00061 }
00062 
00063 /*@null@*/
00064 static PyObject *
00065 rpmfi_BN(rpmfiObject * s)
00066         /*@*/
00067 {
00068     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
00069 }
00070 
00071 /*@null@*/
00072 static PyObject *
00073 rpmfi_DN(rpmfiObject * s)
00074         /*@*/
00075 {
00076     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
00077 }
00078 
00079 /*@null@*/
00080 static PyObject *
00081 rpmfi_FN(rpmfiObject * s)
00082         /*@modifies s @*/
00083 {
00084     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00085 }
00086 
00087 /*@null@*/
00088 static PyObject *
00089 rpmfi_FFlags(rpmfiObject * s)
00090         /*@*/
00091 {
00092     return Py_BuildValue("i", rpmfiFFlags(s->fi));
00093 }
00094 
00095 /*@null@*/
00096 static PyObject *
00097 rpmfi_VFlags(rpmfiObject * s)
00098         /*@*/
00099 {
00100     return Py_BuildValue("i", rpmfiVFlags(s->fi));
00101 }
00102 
00103 /*@null@*/
00104 static PyObject *
00105 rpmfi_FMode(rpmfiObject * s)
00106         /*@*/
00107 {
00108     return Py_BuildValue("i", rpmfiFMode(s->fi));
00109 }
00110 
00111 /*@null@*/
00112 static PyObject *
00113 rpmfi_FState(rpmfiObject * s)
00114         /*@*/
00115 {
00116     return Py_BuildValue("i", rpmfiFState(s->fi));
00117 }
00118 
00119 /* XXX rpmfiMD5 */
00120 /*@null@*/
00121 static PyObject *
00122 rpmfi_MD5(rpmfiObject * s)
00123         /*@*/
00124 {
00125     const unsigned char * MD5;
00126     char fmd5[33];
00127     char * t;
00128     int i;
00129 
00130     MD5 = rpmfiMD5(s->fi);
00131     t = fmd5;
00132     if (MD5 != NULL)
00133     for (i = 0; i < 16; i++, t += 2)
00134         sprintf(t, "%02x", MD5[i]);
00135     *t = '\0';
00136     return Py_BuildValue("s", xstrdup(fmd5));
00137 }
00138 
00139 /*@null@*/
00140 static PyObject *
00141 rpmfi_FLink(rpmfiObject * s)
00142         /*@*/
00143 {
00144     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
00145 }
00146 
00147 /*@null@*/
00148 static PyObject *
00149 rpmfi_FSize(rpmfiObject * s)
00150         /*@*/
00151 {
00152     return Py_BuildValue("i", rpmfiFSize(s->fi));
00153 }
00154 
00155 /*@null@*/
00156 static PyObject *
00157 rpmfi_FRdev(rpmfiObject * s)
00158         /*@*/
00159 {
00160     return Py_BuildValue("i", rpmfiFRdev(s->fi));
00161 }
00162 
00163 /*@null@*/
00164 static PyObject *
00165 rpmfi_FMtime(rpmfiObject * s)
00166         /*@*/
00167 {
00168     return Py_BuildValue("i", rpmfiFMtime(s->fi));
00169 }
00170 
00171 /*@null@*/
00172 static PyObject *
00173 rpmfi_FUser(rpmfiObject * s)
00174         /*@*/
00175 {
00176     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
00177 }
00178 
00179 /*@null@*/
00180 static PyObject *
00181 rpmfi_FGroup(rpmfiObject * s)
00182         /*@*/
00183 {
00184     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
00185 }
00186 
00187 /*@null@*/
00188 static PyObject *
00189 rpmfi_FColor(rpmfiObject * s)
00190         /*@*/
00191 {
00192     return Py_BuildValue("i", rpmfiFColor(s->fi));
00193 }
00194 
00195 /*@null@*/
00196 static PyObject *
00197 rpmfi_FClass(rpmfiObject * s)
00198         /*@*/
00199 {
00200     const char * FClass;
00201 
00202     if ((FClass = rpmfiFClass(s->fi)) == NULL)
00203         FClass = "";
00204     return Py_BuildValue("s", xstrdup(FClass));
00205 }
00206 
00207 #if Py_TPFLAGS_HAVE_ITER
00208 static PyObject *
00209 rpmfi_iter(rpmfiObject * s)
00210         /*@*/
00211 {
00212     Py_INCREF(s);
00213     return (PyObject *)s;
00214 }
00215 #endif
00216 
00217 /*@null@*/
00218 static PyObject *
00219 rpmfi_iternext(rpmfiObject * s)
00220         /*@globals _Py_NoneStruct @*/
00221         /*@modifies s, _Py_NoneStruct @*/
00222 {
00223     PyObject * result = NULL;
00224 
00225     /* Reset loop indices on 1st entry. */
00226     if (!s->active) {
00227         s->fi = rpmfiInit(s->fi, 0);
00228         s->active = 1;
00229     }
00230 
00231     /* If more to do, return the file tuple. */
00232     if (rpmfiNext(s->fi) >= 0) {
00233         const char * FN = rpmfiFN(s->fi);
00234         int FSize = rpmfiFSize(s->fi);
00235         int FMode = rpmfiFMode(s->fi);
00236         int FMtime = rpmfiFMtime(s->fi);
00237         int FFlags = rpmfiFFlags(s->fi);
00238         int FRdev = rpmfiFRdev(s->fi);
00239         int FInode = rpmfiFInode(s->fi);
00240         int FNlink = rpmfiFNlink(s->fi);
00241         int FState = rpmfiFState(s->fi);
00242         int VFlags = rpmfiVFlags(s->fi);
00243         const char * FUser = rpmfiFUser(s->fi);
00244         const char * FGroup = rpmfiFGroup(s->fi);
00245 /*@-shadow@*/
00246         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
00247 /*@=shadow@*/
00248         char FMD5[2*16+1], *t = FMD5;
00249         static const char hex[] = "0123456789abcdef";
00250         int gotMD5, i;
00251 
00252         gotMD5 = 0;
00253         if (s)
00254         for (i = 0; i < 16; i++) {
00255             gotMD5 |= *s;
00256             *t++ = hex[ (*s >> 4) & 0xf ];
00257             *t++ = hex[ (*s++   ) & 0xf ];
00258         }
00259         *t = '\0';
00260 
00261         result = PyTuple_New(13);
00262         if (FN == NULL) {
00263             Py_INCREF(Py_None);
00264             PyTuple_SET_ITEM(result, 0, Py_None);
00265         } else
00266             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
00267         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
00268         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
00269         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
00270         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
00271         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
00272         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
00273         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
00274         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
00275         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
00276         if (FUser == NULL) {
00277             Py_INCREF(Py_None);
00278             PyTuple_SET_ITEM(result, 10, Py_None);
00279         } else
00280             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
00281         if (FGroup == NULL) {
00282             Py_INCREF(Py_None);
00283             PyTuple_SET_ITEM(result, 11, Py_None);
00284         } else
00285             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
00286         if (!gotMD5) {
00287             Py_INCREF(Py_None);
00288             PyTuple_SET_ITEM(result, 12, Py_None);
00289         } else
00290             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
00291 
00292     } else
00293         s->active = 0;
00294 
00295     return result;
00296 }
00297 
00298 static PyObject *
00299 rpmfi_Next(rpmfiObject * s)
00300         /*@globals _Py_NoneStruct @*/
00301         /*@modifies s, _Py_NoneStruct @*/
00302 {
00303     PyObject * result = NULL;
00304 
00305     result = rpmfi_iternext(s);
00306 
00307     if (result == NULL) {
00308         Py_INCREF(Py_None);
00309         return Py_None;
00310     }
00311 
00312     return result;
00313 }
00314 
00315 #ifdef  NOTYET
00316 /*@null@*/
00317 static PyObject *
00318 rpmfi_NextD(rpmfiObject * s)
00319         /*@*/
00320 {
00321         Py_INCREF(Py_None);
00322         return Py_None;
00323 }
00324 
00325 /*@null@*/
00326 static PyObject *
00327 rpmfi_InitD(rpmfiObject * s)
00328         /*@*/
00329 {
00330         Py_INCREF(Py_None);
00331         return Py_None;
00332 }
00333 #endif
00334 
00335 /*@-fullinitblock@*/
00336 /*@unchecked@*/ /*@observer@*/
00337 static struct PyMethodDef rpmfi_methods[] = {
00338  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS|METH_KEYWORDS,
00339         NULL},
00340  {"FC",         (PyCFunction)rpmfi_FC,          METH_NOARGS,
00341         NULL},
00342  {"FX",         (PyCFunction)rpmfi_FX,          METH_NOARGS,
00343         NULL},
00344  {"DC",         (PyCFunction)rpmfi_DC,          METH_NOARGS,
00345         NULL},
00346  {"DX",         (PyCFunction)rpmfi_DX,          METH_NOARGS,
00347         NULL},
00348  {"BN",         (PyCFunction)rpmfi_BN,          METH_NOARGS,
00349         NULL},
00350  {"DN",         (PyCFunction)rpmfi_DN,          METH_NOARGS,
00351         NULL},
00352  {"FN",         (PyCFunction)rpmfi_FN,          METH_NOARGS,
00353         NULL},
00354  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_NOARGS,
00355         NULL},
00356  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_NOARGS,
00357         NULL},
00358  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_NOARGS,
00359         NULL},
00360  {"FState",     (PyCFunction)rpmfi_FState,      METH_NOARGS,
00361         NULL},
00362  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_NOARGS,
00363         NULL},
00364  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_NOARGS,
00365         NULL},
00366  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_NOARGS,
00367         NULL},
00368  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_NOARGS,
00369         NULL},
00370  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_NOARGS,
00371         NULL},
00372  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_NOARGS,
00373         NULL},
00374  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_NOARGS,
00375         NULL},
00376  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_NOARGS,
00377         NULL},
00378  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_NOARGS,
00379         NULL},
00380  {"next",       (PyCFunction)rpmfi_Next,        METH_NOARGS,
00381 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
00382 - Retrieve next file info tuple.\n" },
00383 #ifdef  NOTYET
00384  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_NOARGS,
00385         NULL},
00386  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_NOARGS,
00387         NULL},
00388 #endif
00389  {NULL,         NULL}           /* sentinel */
00390 };
00391 /*@=fullinitblock@*/
00392 
00393 /* ---------- */
00394 
00395 static void
00396 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
00397         /*@modifies s @*/
00398 {
00399     if (s) {
00400         s->fi = rpmfiFree(s->fi);
00401         PyObject_Del(s);
00402     }
00403 }
00404 
00405 static int
00406 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
00407         /*@globals fileSystem @*/
00408         /*@modifies s, fp, fileSystem @*/
00409 {
00410     if (!(s && s->fi))
00411         return -1;
00412 
00413     s->fi = rpmfiInit(s->fi, 0);
00414     while (rpmfiNext(s->fi) >= 0)
00415         fprintf(fp, "%s\n", rpmfiFN(s->fi));
00416     return 0;
00417 }
00418 
00419 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
00420         /*@*/
00421 {
00422     return PyObject_GenericGetAttr(o, n);
00423 }
00424 
00425 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
00426         /*@*/
00427 {
00428     return PyObject_GenericSetAttr(o, n, v);
00429 }
00430 
00431 static int
00432 rpmfi_length(rpmfiObject * s)
00433         /*@*/
00434 {
00435     return rpmfiFC(s->fi);
00436 }
00437 
00438 /*@null@*/
00439 static PyObject *
00440 rpmfi_subscript(rpmfiObject * s, PyObject * key)
00441         /*@modifies s @*/
00442 {
00443     int ix;
00444 
00445     if (!PyInt_Check(key)) {
00446         PyErr_SetString(PyExc_TypeError, "integer expected");
00447         return NULL;
00448     }
00449 
00450     ix = (int) PyInt_AsLong(key);
00451     rpmfiSetFX(s->fi, ix);
00452     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00453 }
00454 
00455 /*@unchecked@*/ /*@observer@*/
00456 static PyMappingMethods rpmfi_as_mapping = {
00457         (inquiry) rpmfi_length,         /* mp_length */
00458         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
00459         (objobjargproc)0,               /* mp_ass_subscript */
00460 };
00461 
00464 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
00465         /*@globals rpmGlobalMacroContext @*/
00466         /*@modifies s, rpmGlobalMacroContext @*/
00467 {
00468     hdrObject * ho = NULL;
00469     PyObject * to = NULL;
00470     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
00471     int tagN = RPMTAG_BASENAMES;
00472     int flags = 0;
00473     char * kwlist[] = {"header", "tag", "flags", NULL};
00474 
00475 if (_rpmfi_debug < 0)
00476 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
00477 
00478     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
00479             &hdr_Type, &ho, &to, &flags))
00480         return -1;
00481 
00482     if (to != NULL) {
00483         tagN = tagNumFromPyObject(to);
00484         if (tagN == -1) {
00485             PyErr_SetString(PyExc_KeyError, "unknown header tag");
00486             return -1;
00487         }
00488     }
00489     s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
00490     s->active = 0;
00491 
00492     return 0;
00493 }
00494 
00497 static void rpmfi_free(/*@only@*/ rpmfiObject * s)
00498         /*@modifies s @*/
00499 {
00500 if (_rpmfi_debug)
00501 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
00502     s->fi = rpmfiFree(s->fi);
00503 
00504     PyObject_Del((PyObject *)s);
00505 }
00506 
00509 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
00510         /*@*/
00511 {
00512     PyObject * s = PyType_GenericAlloc(subtype, nitems);
00513 
00514 if (_rpmfi_debug < 0)
00515 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
00516     return s;
00517 }
00518 
00521 /*@null@*/
00522 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00523         /*@globals rpmGlobalMacroContext @*/
00524         /*@modifies rpmGlobalMacroContext @*/
00525 {
00526     rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
00527 
00528     /* Perform additional initialization. */
00529     if (rpmfi_init(s, args, kwds) < 0) {
00530         rpmfi_free(s);
00531         return NULL;
00532     }
00533 
00534 if (_rpmfi_debug)
00535 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
00536 
00537     return (PyObject *)s;
00538 }
00539 
00542 /*@unchecked@*/ /*@observer@*/
00543 static char rpmfi_doc[] =
00544 "";
00545 
00546 /*@-fullinitblock@*/
00547 PyTypeObject rpmfi_Type = {
00548         PyObject_HEAD_INIT(&PyType_Type)
00549         0,                              /* ob_size */
00550         "rpm.fi",                       /* tp_name */
00551         sizeof(rpmfiObject),            /* tp_basicsize */
00552         0,                              /* tp_itemsize */
00553         /* methods */
00554         (destructor) rpmfi_dealloc,     /* tp_dealloc */
00555         (printfunc) rpmfi_print,        /* tp_print */
00556         (getattrfunc)0,                 /* tp_getattr */
00557         (setattrfunc)0,                 /* tp_setattr */
00558         (cmpfunc)0,                     /* tp_compare */
00559         (reprfunc)0,                    /* tp_repr */
00560         0,                              /* tp_as_number */
00561         0,                              /* tp_as_sequence */
00562         &rpmfi_as_mapping,              /* tp_as_mapping */
00563         (hashfunc)0,                    /* tp_hash */
00564         (ternaryfunc)0,                 /* tp_call */
00565         (reprfunc)0,                    /* tp_str */
00566         (getattrofunc) rpmfi_getattro,  /* tp_getattro */
00567         (setattrofunc) rpmfi_setattro,  /* tp_setattro */
00568         0,                              /* tp_as_buffer */
00569         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00570         rpmfi_doc,                      /* tp_doc */
00571 #if Py_TPFLAGS_HAVE_ITER
00572         0,                              /* tp_traverse */
00573         0,                              /* tp_clear */
00574         0,                              /* tp_richcompare */
00575         0,                              /* tp_weaklistoffset */
00576         (getiterfunc) rpmfi_iter,       /* tp_iter */
00577         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
00578         rpmfi_methods,                  /* tp_methods */
00579         0,                              /* tp_members */
00580         0,                              /* tp_getset */
00581         0,                              /* tp_base */
00582         0,                              /* tp_dict */
00583         0,                              /* tp_descr_get */
00584         0,                              /* tp_descr_set */
00585         0,                              /* tp_dictoffset */
00586         (initproc) rpmfi_init,          /* tp_init */
00587         (allocfunc) rpmfi_alloc,        /* tp_alloc */
00588         (newfunc) rpmfi_new,            /* tp_new */
00589         rpmfi_free,                     /* tp_free */
00590         0,                              /* tp_is_gc */
00591 #endif
00592 };
00593 /*@=fullinitblock@*/
00594 
00595 /* ---------- */
00596 
00597 rpmfi fiFromFi(rpmfiObject * s)
00598 {
00599     return s->fi;
00600 }
00601 
00602 rpmfiObject *
00603 rpmfi_Wrap(rpmfi fi)
00604 {
00605     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
00606 
00607     if (s == NULL)
00608         return NULL;
00609     s->fi = fi;
00610     s->active = 0;
00611     return s;
00612 }
00613 
00614 rpmfiObject *
00615 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
00616 {
00617     hdrObject * ho = (hdrObject *)s;
00618     PyObject * to = NULL;
00619     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
00620     rpmTag tagN = RPMTAG_BASENAMES;
00621     int flags = 0;
00622     char * kwlist[] = {"tag", "flags", NULL};
00623 
00624     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
00625             &to, &flags))
00626         return NULL;
00627 
00628     if (to != NULL) {
00629         tagN = tagNumFromPyObject(to);
00630         if (tagN == -1) {
00631             PyErr_SetString(PyExc_KeyError, "unknown header tag");
00632             return NULL;
00633         }
00634     }
00635     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );
00636 }

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2