- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# python/rpmdb-py.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #include <rpmio.h>
    00008 #include <rpmcb.h>              /* XXX fnpyKey */
    00009 #include <rpmlib.h>
    00010
    00011 #include "rpmdb-py.h"
    00012 #include "rpmmi-py.h"
    00013 #include "header-py.h"
    00014
    00015 #include "debug.h"
    00016
    00017 /*@access Header @*/
    00018
    00107
    00110 /*@null@*/
    00111 static rpmmiObject *
    00112 rpmdb_Match (rpmdbObject * s, PyObject * args, PyObject * kwds)
    00113         /*@globals rpmGlobalMacroContext @*/
    00114         /*@modifies s, rpmGlobalMacroContext @*/
    00115 {
    00116     PyObject *TagN = NULL;
    00117     char *key = NULL;
    00118     int len = 0;
    00119     int tag = RPMDBI_PACKAGES;
    00120     char * kwlist[] = {"tagNumber", "key", "len", NULL};
    00121
    00122     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Ozi", kwlist,
    00123             &TagN, &key, &len))
    00124         return NULL;
    00125
    00126     if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
    00127         PyErr_SetString(PyExc_TypeError, "unknown tag type");
    00128         return NULL;
    00129     }
    00130
    00131     return rpmmi_Wrap( rpmdbInitIterator(s->db, tag, key, len) );
    00132 }
    00133
    00138 /*@-fullinitblock@*/
    00139 /*@unchecked@*/ /*@observer@*/
    00140 static struct PyMethodDef rpmdb_methods[] = {
    00141     {"match",       (PyCFunction) rpmdb_Match,  METH_VARARGS|METH_KEYWORDS,
    00142 "db.match([TagN, [key, [len]]]) -> mi\n\
    00143 - Create an rpm db match iterator.\n" },
    00144     {NULL,              NULL}           /* sentinel */
    00145 };
    00146 /*@=fullinitblock@*/
    00147
    00150 static int
    00151 rpmdb_length(rpmdbObject * s)
    00152         /*@globals rpmGlobalMacroContext @*/
    00153         /*@modifies s, rpmGlobalMacroContext @*/
    00154 {
    00155     rpmdbMatchIterator mi;
    00156     int count = 0;
    00157
    00158     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, NULL, 0);
    00159     while (rpmdbNextIterator(mi) != NULL)
    00160         count++;
    00161     mi = rpmdbFreeIterator(mi);
    00162
    00163     return count;
    00164 }
    00165
    00168 /*@null@*/
    00169 static hdrObject *
    00170 rpmdb_subscript(rpmdbObject * s, PyObject * key)
    00171         /*@globals rpmGlobalMacroContext @*/
    00172         /*@modifies s, rpmGlobalMacroContext @*/
    00173 {
    00174     int offset;
    00175     hdrObject * ho;
    00176     Header h;
    00177     rpmdbMatchIterator mi;
    00178
    00179     if (!PyInt_Check(key)) {
    00180         PyErr_SetString(PyExc_TypeError, "integer expected");
    00181         return NULL;
    00182     }
    00183
    00184     offset = (int) PyInt_AsLong(key);
    00185
    00186     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
    00187     if (!(h = rpmdbNextIterator(mi))) {
    00188         mi = rpmdbFreeIterator(mi);
    00189         PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
    00190         return NULL;
    00191     }
    00192
    00193     ho = hdr_Wrap(h);
    00194     h = headerFree(h);
    00195
    00196     return ho;
    00197 }
    00198
    00201 /*@unchecked@*/ /*@observer@*/
    00202 static PyMappingMethods rpmdb_as_mapping = {
    00203         (inquiry) rpmdb_length,         /* mp_length */
    00204         (binaryfunc) rpmdb_subscript,   /* mp_subscript */
    00205         (objobjargproc)0,               /* mp_ass_subscript */
    00206 };
    00207
    00210 static void rpmdb_dealloc(rpmdbObject * s)
    00211         /*@modifies s @*/
    00212 {
    00213     if (s->db != NULL)
    00214         rpmdbClose(s->db);
    00215     PyObject_Del(s);
    00216 }
    00217
    00218 static PyObject * rpmdb_getattro(PyObject * o, PyObject * n)
    00219         /*@*/
    00220 {
    00221     return PyObject_GenericGetAttr(o, n);
    00222 }
    00223
    00224 static int rpmdb_setattro(PyObject * o, PyObject * n, PyObject * v)
    00225         /*@*/
    00226 {
    00227     return PyObject_GenericSetAttr(o, n, v);
    00228 }
    00229
    00232 /*@unchecked@*/ /*@observer@*/
    00233 static char rpmdb_doc[] =
    00234 "";
    00235
    00238 /*@-fullinitblock@*/
    00239 PyTypeObject rpmdb_Type = {
    00240         PyObject_HEAD_INIT(&PyType_Type)
    00241         0,                              /* ob_size */
    00242         "rpm.db",                       /* tp_name */
    00243         sizeof(rpmdbObject),            /* tp_size */
    00244         0,                              /* tp_itemsize */
    00245         (destructor) rpmdb_dealloc,     /* tp_dealloc */
    00246         0,                              /* tp_print */
    00247         (getattrfunc)0,                 /* tp_getattr */
    00248         0,                              /* tp_setattr */
    00249         0,                              /* tp_compare */
    00250         0,                              /* tp_repr */
    00251         0,                              /* tp_as_number */
    00252         0,                              /* tp_as_sequence */
    00253         &rpmdb_as_mapping,              /* tp_as_mapping */
    00254         0,                              /* tp_hash */
    00255         0,                              /* tp_call */
    00256         0,                              /* tp_str */
    00257         (getattrofunc) rpmdb_getattro,  /* tp_getattro */
    00258         (setattrofunc) rpmdb_setattro,  /* tp_setattro */
    00259         0,                              /* tp_as_buffer */
    00260         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00261         rpmdb_doc,                      /* tp_doc */
    00262 #if Py_TPFLAGS_HAVE_ITER
    00263         0,                              /* tp_traverse */
    00264         0,                              /* tp_clear */
    00265         0,                              /* tp_richcompare */
    00266         0,                              /* tp_weaklistoffset */
    00267         0,                              /* tp_iter */
    00268         0,                              /* tp_iternext */
    00269         rpmdb_methods,                  /* tp_methods */
    00270         0,                              /* tp_members */
    00271         0,                              /* tp_getset */
    00272         0,                              /* tp_base */
    00273         0,                              /* tp_dict */
    00274         0,                              /* tp_descr_get */
    00275         0,                              /* tp_descr_set */
    00276         0,                              /* tp_dictoffset */
    00277         0,                              /* tp_init */
    00278         0,                              /* tp_alloc */
    00279         0,                              /* tp_new */
    00280         0,                              /* tp_free */
    00281         0,                              /* tp_is_gc */
    00282 #endif
    00283 };
    00284 /*@=fullinitblock@*/
    00285
    00286 #ifdef  _LEGACY_BINDINGS_TOO
    00287 rpmdb dbFromDb(rpmdbObject * db)
    00288 {
    00289     return db->db;
    00290 }
    00291
    00294 rpmdbObject *
    00295 rpmOpenDB(/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds) {
    00296     rpmdbObject * o;
    00297     char * root = "";
    00298     int forWrite = 0;
    00299     char * kwlist[] = {"forWrite", "rootdir", NULL};
    00300
    00301     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|is", kwlist,
    00302             &forWrite, &root))
    00303         return NULL;
    00304
    00305     o = PyObject_New(rpmdbObject, &rpmdb_Type);
    00306     o->db = NULL;
    00307
    00308     if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
    00309         char * errmsg = "cannot open database in %s";
    00310         char * errstr = NULL;
    00311         int errsize;
    00312
    00313         Py_DECREF(o);
    00314         /* PyErr_SetString should take varargs... */
    00315         errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
    00316         errstr = alloca(errsize);
    00317 /*@-formatconst@*/
    00318         snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
    00319 /*@=formatconst@*/
    00320         PyErr_SetString(pyrpmError, errstr);
    00321         return NULL;
    00322     }
    00323
    00324     return o;
    00325 }
    00326
    00330 PyObject *
    00331 rebuildDB (/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds)
    00332 {
    00333     char * rootDir = "";
    00334     char * kwlist[] = {"rootdir", NULL};
    00335
    00336     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &rootDir))
    00337         return NULL;
    00338
    00339     return Py_BuildValue("i", rpmdbRebuild(rootDir, NULL, NULL));
    00340 }
    00341 #endif