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 rpmGlobalMacroContext @*/
00117         /*@modifies s, rpmGlobalMacroContext @*/
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         /*@globals rpmGlobalMacroContext @*/
00152         /*@modifies s, rpmGlobalMacroContext @*/
00153 {
00154     rpmdbMatchIterator mi;
00155     int count = 0;
00156 
00157     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, NULL, 0);
00158     while (rpmdbNextIterator(mi) != NULL)
00159         count++;
00160     mi = rpmdbFreeIterator(mi);
00161 
00162     return count;
00163 }
00164 
00167 static hdrObject *
00168 rpmdb_subscript(rpmdbObject * s, PyObject * key)
00169         /*@globals rpmGlobalMacroContext @*/
00170         /*@modifies s, rpmGlobalMacroContext @*/
00171 {
00172     int offset;
00173     hdrObject * ho;
00174     Header h;
00175     rpmdbMatchIterator mi;
00176 
00177     if (!PyInt_Check(key)) {
00178         PyErr_SetString(PyExc_TypeError, "integer expected");
00179         return NULL;
00180     }
00181 
00182     offset = (int) PyInt_AsLong(key);
00183 
00184     mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
00185     if (!(h = rpmdbNextIterator(mi))) {
00186         mi = rpmdbFreeIterator(mi);
00187         PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
00188         return NULL;
00189     }
00190 
00191     ho = hdr_Wrap(h);
00192     h = headerFree(h);
00193 
00194     return ho;
00195 }
00196 
00199 /*@unchecked@*/ /*@observer@*/
00200 static PyMappingMethods rpmdb_as_mapping = {
00201         (inquiry) rpmdb_length,         /* mp_length */
00202         (binaryfunc) rpmdb_subscript,   /* mp_subscript */
00203         (objobjargproc)0,               /* mp_ass_subscript */
00204 };
00205 
00208 static void rpmdb_dealloc(rpmdbObject * s)
00209         /*@modifies s @*/
00210 {
00211     if (s->db)
00212         rpmdbClose(s->db);
00213     PyObject_Del(s);
00214 }
00215 
00218 static PyObject * rpmdb_getattr(rpmdbObject * s, char * name)
00219         /*@*/
00220 {
00221     return Py_FindMethod(rpmdb_methods, (PyObject * ) s, name);
00222 }
00223 
00226 /*@unchecked@*/ /*@observer@*/
00227 static char rpmdb_doc[] =
00228 "";
00229 
00232 /*@-fullinitblock@*/
00233 PyTypeObject rpmdb_Type = {
00234         PyObject_HEAD_INIT(&PyType_Type)
00235         0,                              /* ob_size */
00236         "rpm.db",                       /* tp_name */
00237         sizeof(rpmdbObject),            /* tp_size */
00238         0,                              /* tp_itemsize */
00239         (destructor) rpmdb_dealloc,     /* tp_dealloc */
00240         0,                              /* tp_print */
00241         (getattrfunc) rpmdb_getattr,    /* tp_getattr */
00242         0,                              /* tp_setattr */
00243         0,                              /* tp_compare */
00244         0,                              /* tp_repr */
00245         0,                              /* tp_as_number */
00246         0,                              /* tp_as_sequence */
00247         &rpmdb_as_mapping,              /* tp_as_mapping */
00248         0,                              /* tp_hash */
00249         0,                              /* tp_call */
00250         0,                              /* tp_str */
00251         0,                              /* tp_getattro */
00252         0,                              /* tp_setattro */
00253         0,                              /* tp_as_buffer */
00254         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00255         rpmdb_doc,                      /* tp_doc */
00256 #if Py_TPFLAGS_HAVE_ITER
00257         0,                              /* tp_traverse */
00258         0,                              /* tp_clear */
00259         0,                              /* tp_richcompare */
00260         0,                              /* tp_weaklistoffset */
00261         0,                              /* tp_iter */
00262         0,                              /* tp_iternext */
00263         rpmdb_methods,                  /* tp_methods */
00264         0,                              /* tp_members */
00265         0,                              /* tp_getset */
00266         0,                              /* tp_base */
00267         0,                              /* tp_dict */
00268         0,                              /* tp_descr_get */
00269         0,                              /* tp_descr_set */
00270         0,                              /* tp_dictoffset */
00271         0,                              /* tp_init */
00272         0,                              /* tp_alloc */
00273         0,                              /* tp_new */
00274         0,                              /* tp_free */
00275         0,                              /* tp_is_gc */
00276 #endif
00277 };
00278 /*@=fullinitblock@*/
00279 
00280 #ifdef  _LEGACY_BINDINGS_TOO
00281 rpmdb dbFromDb(rpmdbObject * db)
00282 {
00283     return db->db;
00284 }
00285 
00288 rpmdbObject * rpmOpenDB(/*@unused@*/ PyObject * self, PyObject * args) {
00289     rpmdbObject * o;
00290     char * root = "";
00291     int forWrite = 0;
00292 
00293     if (!PyArg_ParseTuple(args, "|is", &forWrite, &root)) return NULL;
00294 
00295     o = PyObject_New(rpmdbObject, &rpmdb_Type);
00296     o->db = NULL;
00297 
00298     if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
00299         char * errmsg = "cannot open database in %s";
00300         char * errstr = NULL;
00301         int errsize;
00302 
00303         Py_DECREF(o);
00304         /* PyErr_SetString should take varargs... */
00305         errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
00306         errstr = alloca(errsize);
00307 /*@-formatconst@*/
00308         snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
00309 /*@=formatconst@*/
00310         PyErr_SetString(pyrpmError, errstr);
00311         return NULL;
00312     }
00313 
00314     return o;
00315 }
00316 
00320 PyObject * rebuildDB (/*@unused@*/ PyObject * self, PyObject * args)
00321 {
00322     char * rootDir = "";
00323 
00324     if (!PyArg_ParseTuple(args, "s", &rootDir)) return NULL;
00325 
00326     return Py_BuildValue("i", rpmdbRebuild(rootDir, NULL, NULL));
00327 }
00328 #endif
00329 

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