rpm 5.2.1

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

- File List
- Globals

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