1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/** \ingroup python
* \file python/rpmdb-py.c
*/
#include "system.h"
#include "Python.h"
#include <rpmlib.h>
#include "rpmdb-py.h"
#include "rpmmi-py.h"
#include "header-py.h"
#include "debug.h"
/** \ingroup python
* \class Rpmdb
* \brief A python rpmdb object represents an RPM database.
*
* Instances of the rpmdb object provide access to the records of a
* RPM database. The records are accessed by index number. To
* retrieve the header data in the RPM database, the rpmdb object is
* subscripted as you would access members of a list.
*
* The rpmdb class contains the following methods:
*
* - firstkey() Returns the index of the first record in the database.
* @deprecated Use mi = ts.dbMatch() (or db.match()) instead.
*
* - nextkey(index) Returns the index of the next record after "index" in the
* database.
* @param index current rpmdb location
* @deprecated Use hdr = mi.next() instead.
*
* - findbyfile(file) Returns a list of the indexes to records that own file
* "file".
* @param file absolute path to file
* @deprecated Use mi = ts.dbMatch('basename') instead.
*
* - findbyname(name) Returns a list of the indexes to records for packages
* named "name".
* @param name package name
* @deprecated Use mi = ts.dbMatch('name') instead.
*
* - findbyprovides(dep) Returns a list of the indexes to records for packages
* that provide "dep".
* @param dep provided dependency string
* @deprecated Use mi = ts.dbMmatch('providename') instead.
*
* To obtain a db object explicitly, the opendb function in the rpm module
* can be called. The opendb function takes two optional arguments.
* The first optional argument is a boolean flag that specifies if the
* database is to be opened for read/write access or read-only access.
* The second argument specifies an alternate root directory for RPM
* to use.
*
* Note that in most cases, one is interested in querying the default
* database in /var/lib/rpm and an rpm.mi match iterator derived from
* an implicit open of the database from an rpm.ts transaction set object:
* \code
* import rpm
* ts = rpm.TransactionSet()
* mi = ts.dbMatch()
* ...
* \endcode
* is simpler than explicitly opening a database object:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match()
* \endcode
*
* An example of opening a database and retrieving the first header in
* the database, then printing the name of the package that the header
* represents:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match()
* if mi:
* h = mi.next()
* if h:
* print h['name']
* \endcode
*
* To print all of the packages in the database that match a package
* name, the code will look like this:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match('name', "foo")
* while mi:
* h = mi.next()
* if not h:
* break
* print "%s-%s-%s" % (h['name'], h['version'], h['release'])
* \endcode
*/
/** \ingroup python
* \name Class: rpm.db
*/
/*@{*/
/**
*/
static rpmmiObject *
rpmdb_Match (rpmdbObject * s, PyObject * args)
{
PyObject *TagN = NULL;
char *key = NULL;
int len = 0;
int tag = RPMDBI_PACKAGES;
if (!PyArg_ParseTuple(args, "|Ozi", &TagN, &key, &len))
return NULL;
if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
PyErr_SetString(PyExc_TypeError, "unknown tag type");
return NULL;
}
return rpmmi_Wrap( rpmdbInitIterator(s->db, tag, key, len) );
}
/**
*/
static struct PyMethodDef rpmdb_methods[] = {
{"match", (PyCFunction) rpmdb_Match, METH_VARARGS,
"db.match([TagN, [key, [len]]]) -> mi\n\
- Create an rpm db match iterator.\n" },
{NULL, NULL} /* sentinel */
};
/**
*/
static int
rpmdb_length(rpmdbObject * s)
{
rpmdbMatchIterator mi;
int count = 0;
mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, NULL, 0);
while (rpmdbNextIterator(mi) != NULL)
count++;
mi = rpmdbFreeIterator(mi);
return count;
}
/**
*/
static hdrObject *
rpmdb_subscript(rpmdbObject * s, PyObject * key)
{
int offset;
hdrObject * ho;
Header h;
rpmdbMatchIterator mi;
if (!PyInt_Check(key)) {
PyErr_SetString(PyExc_TypeError, "integer expected");
return NULL;
}
offset = (int) PyInt_AsLong(key);
mi = rpmdbInitIterator(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
if (!(h = rpmdbNextIterator(mi))) {
mi = rpmdbFreeIterator(mi);
PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
return NULL;
}
ho = hdr_Wrap(h);
h = headerFree(h, NULL);
return ho;
}
/**
*/
static PyMappingMethods rpmdb_as_mapping = {
(inquiry) rpmdb_length, /* mp_length */
(binaryfunc) rpmdb_subscript, /* mp_subscript */
(objobjargproc)0, /* mp_ass_subscript */
};
/**
*/
static void rpmdb_dealloc(rpmdbObject * s)
{
if (s->db)
rpmdbClose(s->db);
PyMem_DEL(s);
}
/**
*/
static PyObject * rpmdb_getattr(rpmdbObject * s, char * name)
{
return Py_FindMethod(rpmdb_methods, (PyObject * ) s, name);
}
/**
*/
static char rpmdb_doc[] =
"";
/**
*/
PyTypeObject rpmdb_Type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"rpm.db", /* tp_name */
sizeof(rpmdbObject), /* tp_size */
0, /* tp_itemsize */
(destructor) rpmdb_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc) rpmdb_getattr, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
&rpmdb_as_mapping, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
rpmdb_doc, /* tp_doc */
#if Py_TPFLAGS_HAVE_ITER
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
rpmdb_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
#endif
};
rpmdb dbFromDb(rpmdbObject * db)
{
return db->db;
}
/**
*/
rpmdbObject * rpmOpenDB(PyObject * self, PyObject * args) {
rpmdbObject * o;
char * root = "";
int forWrite = 0;
if (!PyArg_ParseTuple(args, "|is", &forWrite, &root)) return NULL;
o = PyObject_NEW(rpmdbObject, &rpmdb_Type);
o->db = NULL;
if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, 0644)) {
char * errmsg = "cannot open database in %s";
char * errstr = NULL;
int errsize;
Py_DECREF(o);
/* PyErr_SetString should take varargs... */
errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
errstr = alloca(errsize);
snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
PyErr_SetString(pyrpmError, errstr);
return NULL;
}
return o;
}
/**
*/
PyObject * rebuildDB (PyObject * self, PyObject * args)
{
char * root = "";
if (!PyArg_ParseTuple(args, "s", &root)) return NULL;
return Py_BuildValue("i", rpmdbRebuild(root));
}
/*@}*/
|