rpm 5.2.1

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

- File List
- Globals

rpmdb-py.c
Go to the documentation of this file.
1
5 #include "system.h"
6
7 #include <rpmio.h>
8 #include <rpmcb.h> /* XXX fnpyKey */
9 #include <rpmtypes.h>
10 #include <rpmtag.h>
11
12 #include "rpmdb-py.h"
13 #include "rpmmi-py.h"
14 #include "header-py.h"
15
16 #include "debug.h"
17
18 /*@access Header @*/
19
108
111 /*@null@*/
112 static rpmmiObject *
113 rpmdb_Match (rpmdbObject * s, PyObject * args, PyObject * kwds)
114 /*@globals rpmGlobalMacroContext @*/
115 /*@modifies s, rpmGlobalMacroContext @*/
116 {
117 PyObject *TagN = NULL;
118 char *key = NULL;
119 int len = 0;
120 int tag = RPMDBI_PACKAGES;
121 char * kwlist[] = {"tagNumber", "key", "len", NULL};
122
123 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Ozi", kwlist,
124 &TagN, &key, &len))
125 return NULL;
126
127 if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
128 PyErr_SetString(PyExc_TypeError, "unknown tag type");
129 return NULL;
130 }
131
132 return rpmmi_Wrap( rpmmiInit(s->db, tag, key, len) );
133 }
134
139 /*@-fullinitblock@*/
140 /*@unchecked@*/ /*@observer@*/
141 static struct PyMethodDef rpmdb_methods[] = {
142 {"match", (PyCFunction) rpmdb_Match, METH_VARARGS|METH_KEYWORDS,
143 "db.match([TagN, [key, [len]]]) -> mi\n\
144 - Create an rpm db match iterator.\n" },
145 {NULL, NULL} /* sentinel */
146 };
147 /*@=fullinitblock@*/
148
151 static int
152 rpmdb_length(rpmdbObject * s)
153 /*@globals rpmGlobalMacroContext @*/
154 /*@modifies s, rpmGlobalMacroContext @*/
155 {
156 rpmmi mi;
157 int count = 0;
158
159 mi = rpmmiInit(s->db, RPMDBI_PACKAGES, NULL, 0);
160 while (rpmmiNext(mi) != NULL)
161 count++;
162 mi = rpmmiFree(mi);
163
164 return count;
165 }
166
169 /*@null@*/
170 static hdrObject *
171 rpmdb_subscript(rpmdbObject * s, PyObject * key)
172 /*@globals rpmGlobalMacroContext @*/
173 /*@modifies s, rpmGlobalMacroContext @*/
174 {
175 int offset;
176 hdrObject * ho;
177 Header h;
178 rpmmi mi;
179
180 if (!PyInt_Check(key)) {
181 PyErr_SetString(PyExc_TypeError, "integer expected");
182 return NULL;
183 }
184
185 offset = (int) PyInt_AsLong(key);
186
187 mi = rpmmiInit(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
188 if (!(h = rpmmiNext(mi))) {
189 mi = rpmmiFree(mi);
190 PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
191 return NULL;
192 }
193
194 ho = hdr_Wrap(h);
195 (void)headerFree(h);
196 h = NULL;
197
198 return ho;
199 }
200
203 /*@unchecked@*/ /*@observer@*/
204 static PyMappingMethods rpmdb_as_mapping = {
205 (lenfunc) rpmdb_length, /* mp_length */
206 (binaryfunc) rpmdb_subscript, /* mp_subscript */
207 (objobjargproc)0, /* mp_ass_subscript */
208 };
209
212 static void rpmdb_dealloc(rpmdbObject * s)
213 /*@modifies s @*/
214 {
215 if (s->db != NULL)
216 rpmdbClose(s->db);
217 PyObject_Del(s);
218 }
219
220 static PyObject * rpmdb_getattro(PyObject * o, PyObject * n)
221 /*@*/
222 {
223 return PyObject_GenericGetAttr(o, n);
224 }
225
226 static int rpmdb_setattro(PyObject * o, PyObject * n, PyObject * v)
227 /*@*/
228 {
229 return PyObject_GenericSetAttr(o, n, v);
230 }
231
234 /*@unchecked@*/ /*@observer@*/
235 static char rpmdb_doc[] =
236 "";
237
240 /*@-fullinitblock@*/
241 PyTypeObject rpmdb_Type = {
242 PyObject_HEAD_INIT(&PyType_Type)
243 0, /* ob_size */
244 "rpm.db", /* tp_name */
245 sizeof(rpmdbObject), /* tp_size */
246 0, /* tp_itemsize */
247 (destructor) rpmdb_dealloc, /* tp_dealloc */
248 0, /* tp_print */
249 (getattrfunc)0, /* tp_getattr */
250 0, /* tp_setattr */
251 0, /* tp_compare */
252 0, /* tp_repr */
253 0, /* tp_as_number */
254 0, /* tp_as_sequence */
255 &rpmdb_as_mapping, /* tp_as_mapping */
256 0, /* tp_hash */
257 0, /* tp_call */
258 0, /* tp_str */
259 (getattrofunc) rpmdb_getattro, /* tp_getattro */
260 (setattrofunc) rpmdb_setattro, /* tp_setattro */
261 0, /* tp_as_buffer */
262 Py_TPFLAGS_DEFAULT, /* tp_flags */
263 rpmdb_doc, /* tp_doc */
264 #if Py_TPFLAGS_HAVE_ITER
265 0, /* tp_traverse */
266 0, /* tp_clear */
267 0, /* tp_richcompare */
268 0, /* tp_weaklistoffset */
269 0, /* tp_iter */
270 0, /* tp_iternext */
271 rpmdb_methods, /* tp_methods */
272 0, /* tp_members */
273 0, /* tp_getset */
274 0, /* tp_base */
275 0, /* tp_dict */
276 0, /* tp_descr_get */
277 0, /* tp_descr_set */
278 0, /* tp_dictoffset */
279 0, /* tp_init */
280 0, /* tp_alloc */
281 0, /* tp_new */
282 0, /* tp_free */
283 0, /* tp_is_gc */
284 #endif
285 };
286 /*@=fullinitblock@*/
287
288 #ifdef _LEGACY_BINDINGS_TOO
289 rpmdb dbFromDb(rpmdbObject * db)
290 {
291 return db->db;
292 }
293
296 rpmdbObject *
297 rpmOpenDB(/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds) {
298 rpmdbObject * o;
299 char * root = "";
300 int forWrite = 0;
301 char * kwlist[] = {"forWrite", "rootdir", NULL};
302
303 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|is", kwlist,
304 &forWrite, &root))
305 return NULL;
306
307 o = PyObject_New(rpmdbObject, &rpmdb_Type);
308 o->db = NULL;
309
310 if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
311 char * errmsg = "cannot open database in %s";
312 char * errstr = NULL;
313 int errsize;
314
315 Py_DECREF(o);
316 /* PyErr_SetString should take varargs... */
317 errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
318 errstr = alloca(errsize);
319 /*@-formatconst@*/
320 snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
321 /*@=formatconst@*/
322 PyErr_SetString(pyrpmError, errstr);
323 return NULL;
324 }
325
326 return o;
327 }
328
332 PyObject *
333 rebuildDB (/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds)
334 {
335 char * rootDir = "";
336 char * kwlist[] = {"rootdir", NULL};
337
338 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &rootDir))
339 return NULL;
340
341 return Py_BuildValue("i", rpmdbRebuild(rootDir, NULL, NULL));
342 }
343 #endif