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
00016
00105
00108
00109 static rpmmiObject *
00110 rpmdb_Match (rpmdbObject * s, PyObject * args, PyObject * kwds)
00111
00112
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
00135
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}
00141 };
00142
00143
00146 static int
00147 rpmdb_length(rpmdbObject * s)
00148
00149
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
00165 static hdrObject *
00166 rpmdb_subscript(rpmdbObject * s, PyObject * key)
00167
00168
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
00198 static PyMappingMethods rpmdb_as_mapping = {
00199 (inquiry) rpmdb_length,
00200 (binaryfunc) rpmdb_subscript,
00201 (objobjargproc)0,
00202 };
00203
00206 static void rpmdb_dealloc(rpmdbObject * s)
00207
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
00229 static char rpmdb_doc[] =
00230 "";
00231
00234
00235 PyTypeObject rpmdb_Type = {
00236 PyObject_HEAD_INIT(&PyType_Type)
00237 0,
00238 "rpm.db",
00239 sizeof(rpmdbObject),
00240 0,
00241 (destructor) rpmdb_dealloc,
00242 0,
00243 (getattrfunc)0,
00244 0,
00245 0,
00246 0,
00247 0,
00248 0,
00249 &rpmdb_as_mapping,
00250 0,
00251 0,
00252 0,
00253 (getattrofunc) rpmdb_getattro,
00254 (setattrofunc) rpmdb_setattro,
00255 0,
00256 Py_TPFLAGS_DEFAULT,
00257 rpmdb_doc,
00258 #if Py_TPFLAGS_HAVE_ITER
00259 0,
00260 0,
00261 0,
00262 0,
00263 0,
00264 0,
00265 rpmdb_methods,
00266 0,
00267 0,
00268 0,
00269 0,
00270 0,
00271 0,
00272 0,
00273 0,
00274 0,
00275 0,
00276 0,
00277 0,
00278 #endif
00279 };
00280
00281
00282 #ifdef _LEGACY_BINDINGS_TOO
00283 rpmdb dbFromDb(rpmdbObject * db)
00284 {
00285 return db->db;
00286 }
00287
00290 rpmdbObject *
00291 rpmOpenDB( 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
00311 errsize = strlen(errmsg) + *root == '\0' ? 15 : strlen(root);
00312 errstr = alloca(errsize);
00313
00314 snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
00315
00316 PyErr_SetString(pyrpmError, errstr);
00317 return NULL;
00318 }
00319
00320 return o;
00321 }
00322
00326 PyObject *
00327 rebuildDB ( 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