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

# python/rpmdb-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 "rpmdb-py.h"
    00016 #include "rpmmi-py.h"
    00017 #include "header-py.h"
    00018
    00019 #include "debug.h"
    00020
    00021 /*@access Header @*/
    00022
    00111
    00114 static rpmmiObject *
    00115 rpmdb_Match (rpmdbObject * s, PyObject * args)
    00116         /*@globals _Py_NoneStruct @*/
    00117         /*@modifies s, _Py_NoneStruct @*/
    00118 {
    00119     PyObject *TagN = NULL;
    00120     char *key = NULL;
    00121     int len = 0;
    00122     int tag = RPMDBI_PACKAGES;
    00123
    00124     if (!PyArg_ParseTuple(args, "|Ozi", &TagN, &key, &len))
    00125         return NULL;
    00126
    00127     if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
    00128         PyErr_SetString(PyExc_TypeError, "unknown tag type");
    00129         return NULL;
    00130     }
    00131
    00132     return rpmmi_Wrap( rpmdbInitIterator(s->db, tag, key, len) );
    00133 }
    00134
    00137 /*@-fullinitblock@*/
    00138 /*@unchecked@*/ /*@observer@*/
    00139 static struct PyMethodDef rpmdb_methods[] = {
    00140     {"match",       (PyCFunction) rpmdb_Match,  METH_VARARGS,
    00141 "db.match([TagN, [key, [len]]]) -> mi\n\
    00142 - Create an rpm db match iterator.\n" },
    00143     {NULL,              NULL}           /* sentinel */
    00144 };
    00145 /*@=fullinitblock@*/
    00146
    00149 static int
    00150 rpmdb_length(rpmdbObject * s)
    00151         /*@modifies s @*/
    00152 {
    00153     rpmdbMatchIterator mi;
    00154     int count = 0;
    00155
    00156     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, NULL, 0);
    00157     while (rpmdbNextIterator(mi) != NULL)
    00158         count++;
    00159     mi = rpmdbFreeIterator(mi);
    00160
    00161     return count;
    00162 }
    00163
    00166 static hdrObject *
    00167 rpmdb_subscript(rpmdbObject * s, PyObject * key)
    00168         /*@modifies s @*/
    00169 {
    00170     int offset;
    00171     hdrObject * ho;
    00172     Header h;
    00173     rpmdbMatchIterator mi;
    00174
    00175     if (!PyInt_Check(key)) {
    00176         PyErr_SetString(PyExc_TypeError, "integer expected");
    00177         return NULL;
    00178     }
    00179
    00180     offset = (int) PyInt_AsLong(key);
    00181
    00182     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
    00183     if (!(h = rpmdbNextIterator(mi))) {
    00184         mi = rpmdbFreeIterator(mi);
    00185         PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
    00186         return NULL;
    00187     }
    00188
    00189     ho = hdr_Wrap(h);
    00190     h = headerFree(h);
    00191
    00192     return ho;
    00193 }
    00194
    00197 /*@unchecked@*/ /*@observer@*/
    00198 static PyMappingMethods rpmdb_as_mapping = {
    00199         (inquiry) rpmdb_length,         /* mp_length */
    00200         (binaryfunc) rpmdb_subscript,   /* mp_subscript */
    00201         (objobjargproc)0,               /* mp_ass_subscript */
    00202 };
    00203
    00206 static void rpmdb_dealloc(rpmdbObject * s)
    00207         /*@modifies s @*/
    00208 {
    00209     if (s->db)
    00210         rpmdbClose(s->db);
    00211     PyMem_DEL(s);
    00212 }
    00213
    00216 static PyObject * rpmdb_getattr(rpmdbObject * s, char * name)
    00217         /*@*/
    00218 {
    00219     return Py_FindMethod(rpmdb_methods, (PyObject * ) s, name);
    00220 }
    00221
    00224 /*@unchecked@*/ /*@observer@*/
    00225 static char rpmdb_doc[] =
    00226 "";
    00227
    00230 /*@-fullinitblock@*/
    00231 PyTypeObject rpmdb_Type = {
    00232         PyObject_HEAD_INIT(NULL)
    00233         0,                              /* ob_size */
    00234         "rpm.db",                       /* tp_name */
    00235         sizeof(rpmdbObject),            /* tp_size */
    00236         0,                              /* tp_itemsize */
    00237         (destructor) rpmdb_dealloc,     /* tp_dealloc */
    00238         0,                              /* tp_print */
    00239         (getattrfunc) rpmdb_getattr,    /* tp_getattr */
    00240         0,                              /* tp_setattr */
    00241         0,                              /* tp_compare */
    00242         0,                              /* tp_repr */
    00243         0,                              /* tp_as_number */
    00244         0,                              /* tp_as_sequence */
    00245         &rpmdb_as_mapping,              /* tp_as_mapping */
    00246         0,                              /* tp_hash */
    00247         0,                              /* tp_call */
    00248         0,                              /* tp_str */
    00249         0,                              /* tp_getattro */
    00250         0,                              /* tp_setattro */
    00251         0,                              /* tp_as_buffer */
    00252         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00253         rpmdb_doc,                      /* tp_doc */
    00254 #if Py_TPFLAGS_HAVE_ITER
    00255         0,                              /* tp_traverse */
    00256         0,                              /* tp_clear */
    00257         0,                              /* tp_richcompare */
    00258         0,                              /* tp_weaklistoffset */
    00259         0,                              /* tp_iter */
    00260         0,                              /* tp_iternext */
    00261         rpmdb_methods,                  /* tp_methods */
    00262         0,                              /* tp_members */
    00263         0,                              /* tp_getset */
    00264         0,                              /* tp_base */
    00265         0,                              /* tp_dict */
    00266         0,                              /* tp_descr_get */
    00267         0,                              /* tp_descr_set */
    00268         0,                              /* tp_dictoffset */
    00269         0,                              /* tp_init */
    00270         0,                              /* tp_alloc */
    00271         0,                              /* tp_new */
    00272         0,                              /* tp_free */
    00273         0,                              /* tp_is_gc */
    00274 #endif
    00275 };
    00276 /*@=fullinitblock@*/
    00277
    00278 #ifdef  _LEGACY_BINDINGS_TOO
    00279 rpmdb dbFromDb(rpmdbObject * db)
    00280 {
    00281     return db->db;
    00282 }
    00283
    00286 rpmdbObject * rpmOpenDB(/*@unused@*/ PyObject * self, PyObject * args) {
    00287     rpmdbObject * o;
    00288     char * root = "";
    00289     int forWrite = 0;
    00290
    00291     if (!PyArg_ParseTuple(args, "|is", &forWrite, &root)) return NULL;
    00292
    00293     o = PyObject_NEW(rpmdbObject, &rpmdb_Type);
    00294     o->db = NULL;
    00295
    00296     if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
    00297         char * errmsg = "cannot open database in %s";
    00298         char * errstr = NULL;
    00299         int errsize;
    00300
    00301         Py_DECREF(o);
    00302         /* PyErr_SetString should take varargs... */
    00303         errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
    00304         errstr = alloca(errsize);
    00305 /*@-formatconst@*/
    00306         snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
    00307 /*@=formatconst@*/
    00308         PyErr_SetString(pyrpmError, errstr);
    00309         return NULL;
    00310     }
    00311
    00312     return o;
    00313 }
    00314
    00318 PyObject * rebuildDB (/*@unused@*/ PyObject * self, PyObject * args)
    00319 {
    00320     char * rootDir = "";
    00321
    00322     if (!PyArg_ParseTuple(args, "s", &rootDir)) return NULL;
    00323
    00324     return Py_BuildValue("i", rpmdbRebuild(rootDir, NULL, NULL));
    00325 }
    00326 #endif
    00327