python/rpmdb-py.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmlib.h>
00008 
00009 #include "rpmdb-py.h"
00010 #include "rpmmi-py.h"
00011 #include "header-py.h"
00012 
00013 #include "debug.h"
00014 
00015 /*@access Header @*/
00016 
00105 
00108 /*@null@*/
00109 static rpmmiObject *
00110 rpmdb_Match (rpmdbObject * s, PyObject * args, PyObject * kwds)
00111         /*@globals rpmGlobalMacroContext @*/
00112         /*@modifies s, rpmGlobalMacroContext @*/
00113 {
00114     PyObject *TagN = NULL;
00115     char *key = NULL;
00116     int len = 0;
00117     int tag = RPMDBI_PACKAGES;
00118     char * kwlist[] = {"tagNumber", "key", "len", NULL};
00119 
00120     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Ozi", kwlist,
00121             &TagN, &key, &len))
00122         return NULL;
00123 
00124     if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
00125         PyErr_SetString(PyExc_TypeError, "unknown tag type");
00126         return NULL;
00127     }
00128 
00129     return rpmmi_Wrap( rpmdbInitIterator(s->db, tag, key, len), (PyObject *)s);
00130 }
00131 
00134 /*@-fullinitblock@*/
00135 /*@unchecked@*/ /*@observer@*/
00136 static struct PyMethodDef rpmdb_methods[] = {
00137     {"match",       (PyCFunction) rpmdb_Match,  METH_VARARGS|METH_KEYWORDS,
00138 "db.match([TagN, [key, [len]]]) -> mi\n\
00139 - Create an rpm db match iterator.\n" },
00140     {NULL,              NULL}           /* sentinel */
00141 };
00142 /*@=fullinitblock@*/
00143 
00146 static int
00147 rpmdb_length(rpmdbObject * s)
00148         /*@globals rpmGlobalMacroContext @*/
00149         /*@modifies s, rpmGlobalMacroContext @*/
00150 {
00151     rpmdbMatchIterator mi;
00152     int count = 0;
00153 
00154     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, NULL, 0);
00155     while (rpmdbNextIterator(mi) != NULL)
00156         count++;
00157     mi = rpmdbFreeIterator(mi);
00158 
00159     return count;
00160 }
00161 
00164 /*@null@*/
00165 static hdrObject *
00166 rpmdb_subscript(rpmdbObject * s, PyObject * key)
00167         /*@globals rpmGlobalMacroContext @*/
00168         /*@modifies s, rpmGlobalMacroContext @*/
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 != NULL)
00210         rpmdbClose(s->db);
00211     PyObject_Del(s);
00212 }
00213 
00214 static PyObject * rpmdb_getattro(PyObject * o, PyObject * n)
00215         /*@*/
00216 {
00217     return PyObject_GenericGetAttr(o, n);
00218 }
00219 
00220 static int rpmdb_setattro(PyObject * o, PyObject * n, PyObject * v)
00221         /*@*/
00222 {
00223     return PyObject_GenericSetAttr(o, n, v);
00224 }
00225 
00228 /*@unchecked@*/ /*@observer@*/
00229 static char rpmdb_doc[] =
00230 "";
00231 
00234 /*@-fullinitblock@*/
00235 PyTypeObject rpmdb_Type = {
00236         PyObject_HEAD_INIT(&PyType_Type)
00237         0,                              /* ob_size */
00238         "rpm.db",                       /* tp_name */
00239         sizeof(rpmdbObject),            /* tp_size */
00240         0,                              /* tp_itemsize */
00241         (destructor) rpmdb_dealloc,     /* tp_dealloc */
00242         0,                              /* tp_print */
00243         (getattrfunc)0,                 /* tp_getattr */
00244         0,                              /* tp_setattr */
00245         0,                              /* tp_compare */
00246         0,                              /* tp_repr */
00247         0,                              /* tp_as_number */
00248         0,                              /* tp_as_sequence */
00249         &rpmdb_as_mapping,              /* tp_as_mapping */
00250         0,                              /* tp_hash */
00251         0,                              /* tp_call */
00252         0,                              /* tp_str */
00253         (getattrofunc) rpmdb_getattro,  /* tp_getattro */
00254         (setattrofunc) rpmdb_setattro,  /* tp_setattro */
00255         0,                              /* tp_as_buffer */
00256         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00257         rpmdb_doc,                      /* tp_doc */
00258 #if Py_TPFLAGS_HAVE_ITER
00259         0,                              /* tp_traverse */
00260         0,                              /* tp_clear */
00261         0,                              /* tp_richcompare */
00262         0,                              /* tp_weaklistoffset */
00263         0,                              /* tp_iter */
00264         0,                              /* tp_iternext */
00265         rpmdb_methods,                  /* tp_methods */
00266         0,                              /* tp_members */
00267         0,                              /* tp_getset */
00268         0,                              /* tp_base */
00269         0,                              /* tp_dict */
00270         0,                              /* tp_descr_get */
00271         0,                              /* tp_descr_set */
00272         0,                              /* tp_dictoffset */
00273         0,                              /* tp_init */
00274         0,                              /* tp_alloc */
00275         0,                              /* tp_new */
00276         0,                              /* tp_free */
00277         0,                              /* tp_is_gc */
00278 #endif
00279 };
00280 /*@=fullinitblock@*/
00281 
00282 #ifdef  _LEGACY_BINDINGS_TOO
00283 rpmdb dbFromDb(rpmdbObject * db)
00284 {
00285     return db->db;
00286 }
00287 
00290 rpmdbObject *
00291 rpmOpenDB(/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds) {
00292     rpmdbObject * o;
00293     char * root = "";
00294     int forWrite = 0;
00295     char * kwlist[] = {"forWrite", "rootdir", NULL};
00296 
00297     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|is", kwlist,
00298             &forWrite, &root))
00299         return NULL;
00300 
00301     o = PyObject_New(rpmdbObject, &rpmdb_Type);
00302     o->db = NULL;
00303 
00304     if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
00305         char * errmsg = "cannot open database in %s";
00306         char * errstr = NULL;
00307         int errsize;
00308 
00309         Py_DECREF(o);
00310         /* PyErr_SetString should take varargs... */
00311         errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
00312         errstr = alloca(errsize);
00313 /*@-formatconst@*/
00314         snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
00315 /*@=formatconst@*/
00316         PyErr_SetString(pyrpmError, errstr);
00317         return NULL;
00318     }
00319 
00320     return o;
00321 }
00322 
00326 PyObject *
00327 rebuildDB (/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds)
00328 {
00329     char * rootDir = "";
00330     char * kwlist[] = {"rootdir", NULL};
00331 
00332     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &rootDir))
00333         return NULL;
00334 
00335     return Py_BuildValue("i", rpmdbRebuild(rootDir, NULL, NULL));
00336 }
00337 #endif
00338 

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