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 #if Py_TPFLAGS_HAVE_ITER
    00187 static PyObject *
    00188 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
    00189         /*@modifies s @*/
    00190 {
    00191     Py_INCREF(s);
    00192     return (PyObject *)s;
    00193 }
    00194 #endif
    00195
    00196 static PyObject *
    00197 rpmfi_iternext(rpmfiObject * s)
    00198         /*@modifies s @*/
    00199 {
    00200     PyObject * result = NULL;
    00201
    00202     /* Reset loop indices on 1st entry. */
    00203     if (!s->active) {
    00204         rpmfiInit(s->fi, 0);
    00205         s->active = 1;
    00206     }
    00207
    00208     /* If more to do, return the file tuple. */
    00209     if (rpmfiNext(s->fi) >= 0) {
    00210         const char * FN = rpmfiFN(s->fi);
    00211         int FSize = rpmfiFSize(s->fi);
    00212         int FMode = rpmfiFMode(s->fi);
    00213         int FMtime = rpmfiFMtime(s->fi);
    00214         int FFlags = rpmfiFFlags(s->fi);
    00215         int FRdev = rpmfiFRdev(s->fi);
    00216         int FInode = rpmfiFInode(s->fi);
    00217         int FNlink = rpmfiFNlink(s->fi);
    00218         int FState = rpmfiFState(s->fi);
    00219         int VFlags = rpmfiVFlags(s->fi);
    00220         const char * FUser = rpmfiFUser(s->fi);
    00221         const char * FGroup = rpmfiFGroup(s->fi);
    00222         const unsigned char * md5 = rpmfiMD5(s->fi), *s = md5;
    00223         char FMD5[2*16+1], *t = FMD5;
    00224         static const char hex[] = "0123456789abcdef";
    00225         int gotmd5, i;
    00226
    00227         gotmd5 = 0;
    00228         if (s)
    00229         for (i = 0; i < 16; i++) {
    00230             gotmd5 |= *s;
    00231             *t++ = hex[ (*s >> 4) & 0xf ];
    00232             *t++ = hex[ (*s++   ) & 0xf ];
    00233         }
    00234         *t = '\0';
    00235
    00236         result = PyTuple_New(13);
    00237         if (FN == NULL) {
    00238             Py_INCREF(Py_None);
    00239             PyTuple_SET_ITEM(result, 0, Py_None);
    00240         } else
    00241             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
    00242         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
    00243         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
    00244         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
    00245         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
    00246         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
    00247         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
    00248         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
    00249         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
    00250         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
    00251         if (FUser == NULL) {
    00252             Py_INCREF(Py_None);
    00253             PyTuple_SET_ITEM(result, 10, Py_None);
    00254         } else
    00255             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
    00256         if (FGroup == NULL) {
    00257             Py_INCREF(Py_None);
    00258             PyTuple_SET_ITEM(result, 11, Py_None);
    00259         } else
    00260             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
    00261         if (!gotmd5) {
    00262             Py_INCREF(Py_None);
    00263             PyTuple_SET_ITEM(result, 12, Py_None);
    00264         } else
    00265             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
    00266
    00267     } else
    00268         s->active = 0;
    00269
    00270     return result;
    00271 }
    00272
    00273 static PyObject *
    00274 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
    00275         /*@modifies s @*/
    00276 {
    00277     PyObject * result = NULL;
    00278
    00279     result = rpmfi_iternext(s);
    00280
    00281     if (result == NULL) {
    00282         Py_INCREF(Py_None);
    00283         return Py_None;
    00284     }
    00285
    00286     return result;
    00287 }
    00288
    00289 #ifdef  NOTYET
    00290 static PyObject *
    00291 rpmfi_NextD(rpmfiObject * s, PyObject * args)
    00292         /*@*/
    00293 {
    00294         if (!PyArg_ParseTuple(args, ":NextD"))
    00295                 return NULL;
    00296         Py_INCREF(Py_None);
    00297         return Py_None;
    00298 }
    00299
    00300 static PyObject *
    00301 rpmfi_InitD(rpmfiObject * s, PyObject * args)
    00302         /*@*/
    00303 {
    00304         if (!PyArg_ParseTuple(args, ":InitD"))
    00305                 return NULL;
    00306         Py_INCREF(Py_None);
    00307         return Py_None;
    00308 }
    00309 #endif
    00310
    00311 /*@-fullinitblock@*/
    00312 /*@unchecked@*/ /*@observer@*/
    00313 static struct PyMethodDef rpmfi_methods[] = {
    00314  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS,
    00315         NULL},
    00316  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
    00317         NULL},
    00318  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
    00319         NULL},
    00320  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
    00321         NULL},
    00322  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
    00323         NULL},
    00324  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
    00325         NULL},
    00326  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
    00327         NULL},
    00328  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
    00329         NULL},
    00330  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
    00331         NULL},
    00332  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
    00333         NULL},
    00334  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
    00335         NULL},
    00336  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
    00337         NULL},
    00338  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
    00339         NULL},
    00340  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
    00341         NULL},
    00342  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
    00343         NULL},
    00344  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
    00345         NULL},
    00346  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
    00347         NULL},
    00348  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
    00349         NULL},
    00350  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
    00351         NULL},
    00352  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,
    00353 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
    00354 - Retrieve next file info tuple.\n" },
    00355 #ifdef  NOTYET
    00356  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_VARARGS,
    00357         NULL},
    00358  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
    00359         NULL},
    00360 #endif
    00361  {NULL,         NULL}           /* sentinel */
    00362 };
    00363 /*@=fullinitblock@*/
    00364
    00365 /* ---------- */
    00366
    00367 static void
    00368 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
    00369         /*@modifies s @*/
    00370 {
    00371     if (s) {
    00372         s->fi = rpmfiFree(s->fi);
    00373         PyMem_DEL(s);
    00374     }
    00375 }
    00376
    00377 static int
    00378 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
    00379         /*@globals fileSystem @*/
    00380         /*@modifies s, fp, fileSystem @*/
    00381 {
    00382     if (!(s && s->fi))
    00383         return -1;
    00384
    00385     rpmfiInit(s->fi, 0);
    00386     while (rpmfiNext(s->fi) >= 0)
    00387         fprintf(fp, "%s\n", rpmfiFN(s->fi));
    00388     return 0;
    00389 }
    00390
    00391 static PyObject *
    00392 rpmfi_getattr(rpmfiObject * s, char * name)
    00393         /*@*/
    00394 {
    00395     return Py_FindMethod(rpmfi_methods, (PyObject *)s, name);
    00396 }
    00397
    00398 static int
    00399 rpmfi_length(rpmfiObject * s)
    00400         /*@*/
    00401 {
    00402     return rpmfiFC(s->fi);
    00403 }
    00404
    00405 static PyObject *
    00406 rpmfi_subscript(rpmfiObject * s, PyObject * key)
    00407         /*@modifies s @*/
    00408 {
    00409     int ix;
    00410
    00411     if (!PyInt_Check(key)) {
    00412         PyErr_SetString(PyExc_TypeError, "integer expected");
    00413         return NULL;
    00414     }
    00415
    00416     ix = (int) PyInt_AsLong(key);
    00417     rpmfiSetFX(s->fi, ix);
    00418     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
    00419 }
    00420
    00421 /*@unchecked@*/ /*@observer@*/
    00422 static PyMappingMethods rpmfi_as_mapping = {
    00423         (inquiry) rpmfi_length,         /* mp_length */
    00424         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
    00425         (objobjargproc)0,               /* mp_ass_subscript */
    00426 };
    00427
    00430 /*@unchecked@*/ /*@observer@*/
    00431 static char rpmfi_doc[] =
    00432 "";
    00433
    00434 /*@-fullinitblock@*/
    00435 PyTypeObject rpmfi_Type = {
    00436         PyObject_HEAD_INIT(&PyType_Type)
    00437         0,                              /* ob_size */
    00438         "rpm.fi",                       /* tp_name */
    00439         sizeof(rpmfiObject),            /* tp_basicsize */
    00440         0,                              /* tp_itemsize */
    00441         /* methods */
    00442         (destructor)rpmfi_dealloc,      /* tp_dealloc */
    00443         (printfunc)rpmfi_print,         /* tp_print */
    00444         (getattrfunc)rpmfi_getattr,     /* tp_getattr */
    00445         (setattrfunc)0,                 /* tp_setattr */
    00446         (cmpfunc)0,                     /* tp_compare */
    00447         (reprfunc)0,                    /* tp_repr */
    00448         0,                              /* tp_as_number */
    00449         0,                              /* tp_as_sequence */
    00450         &rpmfi_as_mapping,              /* tp_as_mapping */
    00451         (hashfunc)0,                    /* tp_hash */
    00452         (ternaryfunc)0,                 /* tp_call */
    00453         (reprfunc)0,                    /* tp_str */
    00454         0,                              /* tp_getattro */
    00455         0,                              /* tp_setattro */
    00456         0,                              /* tp_as_buffer */
    00457         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00458         rpmfi_doc,                      /* tp_doc */
    00459 #if Py_TPFLAGS_HAVE_ITER
    00460         0,                              /* tp_traverse */
    00461         0,                              /* tp_clear */
    00462         0,                              /* tp_richcompare */
    00463         0,                              /* tp_weaklistoffset */
    00464         (getiterfunc)rpmfi_iter,        /* tp_iter */
    00465         (iternextfunc)rpmfi_iternext,   /* tp_iternext */
    00466         rpmfi_methods,                  /* tp_methods */
    00467         0,                              /* tp_members */
    00468         0,                              /* tp_getset */
    00469         0,                              /* tp_base */
    00470         0,                              /* tp_dict */
    00471         0,                              /* tp_descr_get */
    00472         0,                              /* tp_descr_set */
    00473         0,                              /* tp_dictoffset */
    00474         0,                              /* tp_init */
    00475         0,                              /* tp_alloc */
    00476         0,                              /* tp_new */
    00477         0,                              /* tp_free */
    00478         0,                              /* tp_is_gc */
    00479 #endif
    00480 };
    00481 /*@=fullinitblock@*/
    00482
    00483 /* ---------- */
    00484
    00485 rpmfi fiFromFi(rpmfiObject * s)
    00486 {
    00487     return s->fi;
    00488 }
    00489
    00490 rpmfiObject *
    00491 rpmfi_Wrap(rpmfi fi)
    00492 {
    00493     rpmfiObject *s = PyObject_NEW(rpmfiObject, &rpmfi_Type);
    00494
    00495     if (s == NULL)
    00496         return NULL;
    00497     s->fi = fi;
    00498     s->active = 0;
    00499     return s;
    00500 }
    00501
    00502 rpmfiObject *
    00503 hdr_fiFromHeader(PyObject * s, PyObject * args)
    00504 {
    00505     hdrObject * ho = (hdrObject *)s;
    00506     PyObject * to = NULL;
    00507     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
    00508     rpmTag tagN = RPMTAG_BASENAMES;
    00509     int scareMem = 0;
    00510
    00511     if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
    00512         return NULL;
    00513     if (to != NULL) {
    00514         tagN = tagNumFromPyObject(to);
    00515         if (tagN == -1) {
    00516             PyErr_SetString(PyExc_KeyError, "unknown header tag");
    00517             return NULL;
    00518         }
    00519     }
    00520     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );
    00521 }