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

python/rpmfi-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 "rpmfi-py.h"
00017 
00018 #include "debug.h"
00019 
00020 /*@access rpmfi @*/
00021 
00022 static PyObject *
00023 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args)
00024         /*@globals _Py_NoneStruct @*/
00025         /*@modifies _Py_NoneStruct @*/
00026 {
00027     if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
00028     Py_INCREF(Py_None);
00029     return Py_None;
00030 }
00031 
00032 static PyObject *
00033 rpmfi_FC(rpmfiObject * s, PyObject * args)
00034         /*@*/
00035 {
00036     if (!PyArg_ParseTuple(args, ":FC")) return NULL;
00037     return Py_BuildValue("i", rpmfiFC(s->fi));
00038 }
00039 
00040 static PyObject *
00041 rpmfi_FX(rpmfiObject * s, PyObject * args)
00042         /*@*/
00043 {
00044     if (!PyArg_ParseTuple(args, ":FX")) return NULL;
00045     return Py_BuildValue("i", rpmfiFX(s->fi));
00046 }
00047 
00048 static PyObject *
00049 rpmfi_DC(rpmfiObject * s, PyObject * args)
00050         /*@*/
00051 {
00052     if (!PyArg_ParseTuple(args, ":DC")) return NULL;
00053     return Py_BuildValue("i", rpmfiDC(s->fi));
00054 }
00055 
00056 static PyObject *
00057 rpmfi_DX(rpmfiObject * s, PyObject * args)
00058         /*@*/
00059 {
00060     if (!PyArg_ParseTuple(args, ":DX")) return NULL;
00061     return Py_BuildValue("i", rpmfiDX(s->fi));
00062 }
00063 
00064 static PyObject *
00065 rpmfi_BN(rpmfiObject * s, PyObject * args)
00066         /*@*/
00067 {
00068     if (!PyArg_ParseTuple(args, ":BN")) return NULL;
00069     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
00070 }
00071 
00072 static PyObject *
00073 rpmfi_DN(rpmfiObject * s, PyObject * args)
00074         /*@*/
00075 {
00076     if (!PyArg_ParseTuple(args, ":DN")) return NULL;
00077     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
00078 }
00079 
00080 static PyObject *
00081 rpmfi_FN(rpmfiObject * s, PyObject * args)
00082         /*@modifies s @*/
00083 {
00084     if (!PyArg_ParseTuple(args, ":FN")) return NULL;
00085     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00086 }
00087 
00088 static PyObject *
00089 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
00090         /*@*/
00091 {
00092     if (!PyArg_ParseTuple(args, ":FFlags")) return NULL;
00093     return Py_BuildValue("i", rpmfiFFlags(s->fi));
00094 }
00095 
00096 static PyObject *
00097 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
00098         /*@*/
00099 {
00100     if (!PyArg_ParseTuple(args, ":VFlags")) return NULL;
00101     return Py_BuildValue("i", rpmfiVFlags(s->fi));
00102 }
00103 
00104 static PyObject *
00105 rpmfi_FMode(rpmfiObject * s, PyObject * args)
00106         /*@*/
00107 {
00108     if (!PyArg_ParseTuple(args, ":FMode")) return NULL;
00109     return Py_BuildValue("i", rpmfiFMode(s->fi));
00110 }
00111 
00112 static PyObject *
00113 rpmfi_FState(rpmfiObject * s, PyObject * args)
00114         /*@*/
00115 {
00116     if (!PyArg_ParseTuple(args, ":FState")) return NULL;
00117     return Py_BuildValue("i", rpmfiFState(s->fi));
00118 }
00119 
00120 /* XXX rpmfiMD5 */
00121 static PyObject *
00122 rpmfi_MD5(rpmfiObject * s, PyObject * args)
00123         /*@*/
00124 {
00125     const unsigned char * MD5;
00126     char fmd5[33];
00127     char * t;
00128     int i;
00129     
00130     if (!PyArg_ParseTuple(args, ":MD5")) return NULL;
00131     MD5 = rpmfiMD5(s->fi);
00132     for (i = 0, t = fmd5; i < 16; i++, t += 2)
00133         sprintf(t, "%02x", MD5[i]);
00134     *t = '\0';
00135     return Py_BuildValue("s", xstrdup(fmd5));
00136 }
00137 
00138 static PyObject *
00139 rpmfi_FLink(rpmfiObject * s, PyObject * args)
00140         /*@*/
00141 {
00142     if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
00143     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
00144 }
00145 
00146 static PyObject *
00147 rpmfi_FSize(rpmfiObject * s, PyObject * args)
00148         /*@*/
00149 {
00150     if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
00151     return Py_BuildValue("i", rpmfiFSize(s->fi));
00152 }
00153 
00154 static PyObject *
00155 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
00156         /*@*/
00157 {
00158     if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
00159     return Py_BuildValue("i", rpmfiFRdev(s->fi));
00160 }
00161 
00162 static PyObject *
00163 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
00164         /*@*/
00165 {
00166     if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
00167     return Py_BuildValue("i", rpmfiFMtime(s->fi));
00168 }
00169 
00170 static PyObject *
00171 rpmfi_FUser(rpmfiObject * s, PyObject * args)
00172         /*@*/
00173 {
00174     if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
00175     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
00176 }
00177 
00178 static PyObject *
00179 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
00180         /*@*/
00181 {
00182     if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
00183     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
00184 }
00185 
00186 static PyObject *
00187 rpmfi_FColor(rpmfiObject * s, PyObject * args)
00188         /*@*/
00189 {
00190     if (!PyArg_ParseTuple(args, ":FColor")) return NULL;
00191     return Py_BuildValue("i", rpmfiFColor(s->fi));
00192 }
00193 
00194 static PyObject *
00195 rpmfi_FClass(rpmfiObject * s, PyObject * args)
00196         /*@*/
00197 {
00198     const char * FClass;
00199 
00200     if (!PyArg_ParseTuple(args, ":FClass")) return NULL;
00201     if ((FClass = rpmfiFClass(s->fi)) == NULL)
00202         FClass = "";
00203     return Py_BuildValue("s", xstrdup(FClass));
00204 }
00205 
00206 #if Py_TPFLAGS_HAVE_ITER
00207 static PyObject *
00208 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
00209         /*@*/
00210 {
00211     Py_INCREF(s);
00212     return (PyObject *)s;
00213 }
00214 #endif
00215 
00216 static PyObject *
00217 rpmfi_iternext(rpmfiObject * s)
00218         /*@globals _Py_NoneStruct @*/
00219         /*@modifies s, _Py_NoneStruct @*/
00220 {
00221     PyObject * result = NULL;
00222 
00223     /* Reset loop indices on 1st entry. */
00224     if (!s->active) {
00225         s->fi = rpmfiInit(s->fi, 0);
00226         s->active = 1;
00227     }
00228 
00229     /* If more to do, return the file tuple. */
00230     if (rpmfiNext(s->fi) >= 0) {
00231         const char * FN = rpmfiFN(s->fi);
00232         int FSize = rpmfiFSize(s->fi);
00233         int FMode = rpmfiFMode(s->fi);
00234         int FMtime = rpmfiFMtime(s->fi);
00235         int FFlags = rpmfiFFlags(s->fi);
00236         int FRdev = rpmfiFRdev(s->fi);
00237         int FInode = rpmfiFInode(s->fi);
00238         int FNlink = rpmfiFNlink(s->fi);
00239         int FState = rpmfiFState(s->fi);
00240         int VFlags = rpmfiVFlags(s->fi);
00241         const char * FUser = rpmfiFUser(s->fi);
00242         const char * FGroup = rpmfiFGroup(s->fi);
00243 /*@-shadow@*/
00244         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
00245 /*@=shadow@*/
00246         char FMD5[2*16+1], *t = FMD5;
00247         static const char hex[] = "0123456789abcdef";
00248         int gotMD5, i;
00249 
00250         gotMD5 = 0;
00251         if (s)
00252         for (i = 0; i < 16; i++) {
00253             gotMD5 |= *s;
00254             *t++ = hex[ (*s >> 4) & 0xf ];
00255             *t++ = hex[ (*s++   ) & 0xf ];
00256         }
00257         *t = '\0';
00258 
00259         result = PyTuple_New(13);
00260         if (FN == NULL) {
00261             Py_INCREF(Py_None);
00262             PyTuple_SET_ITEM(result, 0, Py_None);
00263         } else
00264             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
00265         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
00266         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
00267         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
00268         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
00269         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
00270         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
00271         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
00272         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
00273         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
00274         if (FUser == NULL) {
00275             Py_INCREF(Py_None);
00276             PyTuple_SET_ITEM(result, 10, Py_None);
00277         } else
00278             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
00279         if (FGroup == NULL) {
00280             Py_INCREF(Py_None);
00281             PyTuple_SET_ITEM(result, 11, Py_None);
00282         } else
00283             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
00284         if (!gotMD5) {
00285             Py_INCREF(Py_None);
00286             PyTuple_SET_ITEM(result, 12, Py_None);
00287         } else
00288             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
00289 
00290     } else
00291         s->active = 0;
00292 
00293     return result;
00294 }
00295 
00296 static PyObject *
00297 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
00298         /*@globals _Py_NoneStruct @*/
00299         /*@modifies s, _Py_NoneStruct @*/
00300 {
00301     PyObject * result = NULL;
00302 
00303     result = rpmfi_iternext(s);
00304 
00305     if (result == NULL) {
00306         Py_INCREF(Py_None);
00307         return Py_None;
00308     }
00309 
00310     return result;
00311 }
00312 
00313 #ifdef  NOTYET
00314 static PyObject *
00315 rpmfi_NextD(rpmfiObject * s, PyObject * args)
00316         /*@*/
00317 {
00318         if (!PyArg_ParseTuple(args, ":NextD"))
00319                 return NULL;
00320         Py_INCREF(Py_None);
00321         return Py_None;
00322 }
00323 
00324 static PyObject *
00325 rpmfi_InitD(rpmfiObject * s, PyObject * args)
00326         /*@*/
00327 {
00328         if (!PyArg_ParseTuple(args, ":InitD"))
00329                 return NULL;
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,
00339         NULL},
00340  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
00341         NULL},
00342  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
00343         NULL},
00344  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
00345         NULL},
00346  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
00347         NULL},
00348  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
00349         NULL},
00350  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
00351         NULL},
00352  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
00353         NULL},
00354  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
00355         NULL},
00356  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
00357         NULL},
00358  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
00359         NULL},
00360  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
00361         NULL},
00362  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
00363         NULL},
00364  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
00365         NULL},
00366  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
00367         NULL},
00368  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
00369         NULL},
00370  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
00371         NULL},
00372  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
00373         NULL},
00374  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
00375         NULL},
00376  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_VARARGS,
00377         NULL},
00378  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_VARARGS,
00379         NULL},
00380  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,
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_VARARGS,
00385         NULL},
00386  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
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 *
00420 rpmfi_getattr(rpmfiObject * s, char * name)
00421         /*@*/
00422 {
00423     return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
00424 }
00425 
00426 static int
00427 rpmfi_length(rpmfiObject * s)
00428         /*@*/
00429 {
00430     return rpmfiFC(s->fi);
00431 }
00432 
00433 static PyObject *
00434 rpmfi_subscript(rpmfiObject * s, PyObject * key)
00435         /*@modifies s @*/
00436 {
00437     int ix;
00438 
00439     if (!PyInt_Check(key)) {
00440         PyErr_SetString(PyExc_TypeError, "integer expected");
00441         return NULL;
00442     }
00443 
00444     ix = (int) PyInt_AsLong(key);
00445     rpmfiSetFX(s->fi, ix);
00446     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00447 }
00448 
00449 /*@unchecked@*/ /*@observer@*/
00450 static PyMappingMethods rpmfi_as_mapping = {
00451         (inquiry) rpmfi_length,         /* mp_length */
00452         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
00453         (objobjargproc)0,               /* mp_ass_subscript */
00454 };
00455 
00458 /*@unchecked@*/ /*@observer@*/
00459 static char rpmfi_doc[] =
00460 "";
00461 
00462 /*@-fullinitblock@*/
00463 PyTypeObject rpmfi_Type = {
00464         PyObject_HEAD_INIT(&PyType_Type)
00465         0,                              /* ob_size */
00466         "rpm.fi",                       /* tp_name */
00467         sizeof(rpmfiObject),            /* tp_basicsize */
00468         0,                              /* tp_itemsize */
00469         /* methods */
00470         (destructor) rpmfi_dealloc,     /* tp_dealloc */
00471         (printfunc) rpmfi_print,        /* tp_print */
00472         (getattrfunc) rpmfi_getattr,    /* tp_getattr */
00473         (setattrfunc)0,                 /* tp_setattr */
00474         (cmpfunc)0,                     /* tp_compare */
00475         (reprfunc)0,                    /* tp_repr */
00476         0,                              /* tp_as_number */
00477         0,                              /* tp_as_sequence */
00478         &rpmfi_as_mapping,              /* tp_as_mapping */
00479         (hashfunc)0,                    /* tp_hash */
00480         (ternaryfunc)0,                 /* tp_call */
00481         (reprfunc)0,                    /* tp_str */
00482         0,                              /* tp_getattro */
00483         0,                              /* tp_setattro */
00484         0,                              /* tp_as_buffer */
00485         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00486         rpmfi_doc,                      /* tp_doc */
00487 #if Py_TPFLAGS_HAVE_ITER
00488         0,                              /* tp_traverse */
00489         0,                              /* tp_clear */
00490         0,                              /* tp_richcompare */
00491         0,                              /* tp_weaklistoffset */
00492         (getiterfunc) rpmfi_iter,       /* tp_iter */
00493         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
00494         rpmfi_methods,                  /* tp_methods */
00495         0,                              /* tp_members */
00496         0,                              /* tp_getset */
00497         0,                              /* tp_base */
00498         0,                              /* tp_dict */
00499         0,                              /* tp_descr_get */
00500         0,                              /* tp_descr_set */
00501         0,                              /* tp_dictoffset */
00502         0,                              /* tp_init */
00503         0,                              /* tp_alloc */
00504         0,                              /* tp_new */
00505         0,                              /* tp_free */
00506         0,                              /* tp_is_gc */
00507 #endif
00508 };
00509 /*@=fullinitblock@*/
00510 
00511 /* ---------- */
00512 
00513 rpmfi fiFromFi(rpmfiObject * s)
00514 {
00515     return s->fi;
00516 }
00517 
00518 rpmfiObject *
00519 rpmfi_Wrap(rpmfi fi)
00520 {
00521     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
00522 
00523     if (s == NULL)
00524         return NULL;
00525     s->fi = fi;
00526     s->active = 0;
00527     return s;
00528 }
00529 
00530 rpmfiObject *
00531 hdr_fiFromHeader(PyObject * s, PyObject * args)
00532 {
00533     hdrObject * ho = (hdrObject *)s;
00534     PyObject * to = NULL;
00535     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
00536     rpmTag tagN = RPMTAG_BASENAMES;
00537     int scareMem = 0;
00538 
00539     if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
00540         return NULL;
00541     if (to != NULL) {
00542         tagN = tagNumFromPyObject(to);
00543         if (tagN == -1) {
00544             PyErr_SetString(PyExc_KeyError, "unknown header tag");
00545             return NULL;
00546         }
00547     }
00548     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );
00549 }

Generated on Sun Oct 26 13:02:02 2003 for rpm by doxygen1.2.18