GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/db.c Lines: 132 154 85.7 %
Date: 2019-02-09 19:11:25 Branches: 34 66 51.5 %

Line Branch Exec Source
1
/**
2
 * db.c : wrapper class around alpm_db_t
3
 *
4
 *  Copyright (c) 2011 Rémy Oudompheng <remy@archlinux.org>
5
 *
6
 *  This file is part of pyalpm.
7
 *
8
 *  pyalpm is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation, either version 3 of the License, or
11
 *  (at your option) any later version.
12
 *
13
 *  pyalpm is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with pyalpm.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
#include <pyconfig.h>
24
#include <alpm.h>
25
#include <Python.h>
26
#include "db.h"
27
#include "package.h"
28
#include "util.h"
29
30
typedef struct _AlpmDB {
31
  PyObject_HEAD
32
  alpm_db_t *c_data;
33
} AlpmDB;
34
35
#define ALPM_DB(self) (((AlpmDB*)self)->c_data)
36
37
static PyTypeObject AlpmDBType;
38
39
21
static void pyalpm_db_dealloc(AlpmDB *self) {
40
21
  Py_TYPE(self)->tp_free((PyObject*)self);
41
21
}
42
43
1
static PyObject* _pyobject_from_pmgrp(void *group) {
44
1
  const alpm_group_t* grp = (alpm_group_t*)group;
45
1
  if (!grp)
46
1
    Py_RETURN_NONE;
47
  else {
48
    PyObject *fst = PyUnicode_FromString(grp->name);
49
    PyObject *snd = alpmlist_to_pylist(grp->packages,
50
              pyalpm_package_from_pmpkg);
51
    PyObject *tuple = PyTuple_Pack(2, fst, snd);
52
    Py_DECREF(fst);
53
    Py_DECREF(snd);
54
    return tuple;
55
  }
56
}
57
58
/** Converts a Python list of databases to an alpm_list_t linked list.
59
 * return 0 on success, -1 on failure
60
 */
61
2
int pylist_db_to_alpmlist(PyObject *list, alpm_list_t **result) {
62
2
  alpm_list_t *ret = NULL;
63
2
  PyObject *iterator = PyObject_GetIter(list);
64
2
  PyObject *item;
65
66
2
  if(iterator == NULL) {
67
    PyErr_SetString(PyExc_TypeError, "object is not iterable");
68
    return -1;
69
  }
70
71
4
  while((item = PyIter_Next(iterator)))
72
  {
73

2
    if (PyObject_TypeCheck(item, &AlpmDBType)) {
74
2
      ret = alpm_list_add(ret, ((AlpmDB*)item)->c_data);
75
    } else {
76
      PyErr_SetString(PyExc_TypeError, "list must contain only Database objects");
77
      FREELIST(ret);
78
      Py_DECREF(item);
79
      Py_DECREF(iterator);
80
      return -1;
81
    }
82
2
    Py_DECREF(item);
83
  }
84
2
  Py_DECREF(iterator);
85
86
2
  *result = ret;
87
2
  return 0;
88
}
89
90
#define CHECK_IF_INITIALIZED() if (! self->c_data) { \
91
  PyErr_SetString(alpm_error, "data is not initialized"); \
92
  return NULL; \
93
  }
94
95
/** Database properties */
96
97
5
static PyObject* pyalpm_db_get_name(AlpmDB* self, void* closure) {
98
5
  const char* name;
99
5
  CHECK_IF_INITIALIZED();
100
5
  name = alpm_db_get_name(self->c_data);
101
5
  if (!name)
102
    Py_RETURN_NONE;
103
5
  return PyUnicode_FromString(name);
104
}
105
106
1
static PyObject* pyalpm_db_get_servers(PyObject *self, void* closure) {
107
1
  alpm_db_t *db = ALPM_DB(self);
108
1
  return alpmlist_to_pylist(alpm_db_get_servers(db), pyobject_from_string);
109
}
110
111
5
static int pyalpm_db_set_servers(PyObject* self, PyObject* value, void* closure) {
112
5
  alpm_db_t *db = ALPM_DB(self);
113
5
  alpm_list_t *target;
114
5
  if (pylist_string_to_alpmlist(value, &target) == -1)
115
    return -1;
116
5
  if (alpm_db_set_servers(db, target) == -1)
117
    RET_ERR("unable to set servers", 0, -1);
118
  return 0;
119
}
120
121
1
static PyObject* pyalpm_db_get_pkgcache(AlpmDB* self, void* closure) {
122
1
  alpm_list_t *pkglist = alpm_db_get_pkgcache(self->c_data);
123
1
  return alpmlist_to_pylist(pkglist, pyalpm_package_from_pmpkg);
124
}
125
126
1
static PyObject* pyalpm_db_get_grpcache(AlpmDB* self, void* closure) {
127
1
  alpm_list_t *grplist = alpm_db_get_groupcache(self->c_data);
128
1
  return alpmlist_to_pylist(grplist, _pyobject_from_pmgrp);
129
}
130
131
/** Package get/set operations */
132
133
5
static PyObject* pyalpm_db_get_pkg(PyObject *rawself, PyObject* args) {
134
5
  char *pkgname;
135
5
  alpm_pkg_t *p;
136
5
  AlpmDB *self = (AlpmDB*)rawself;
137
138
5
  if(!PyArg_ParseTuple(args, "s", &pkgname))
139
  {
140
1
    PyErr_SetString(PyExc_TypeError, "get_pkg() takes a string argument");
141
1
    return NULL;
142
  }
143
144
4
  CHECK_IF_INITIALIZED();
145
146
4
  p = alpm_db_get_pkg(self->c_data, pkgname);
147
148
4
  if (p == NULL) {
149
1
    Py_RETURN_NONE;
150
  } else {
151
3
    PyObject *result;
152
3
    result = pyalpm_package_from_pmpkg(p);
153
3
    if (result == NULL) {
154
      return NULL;
155
    } else {
156
3
      return result;
157
    }
158
  }
159
}
160
161
2
static PyObject* pyalpm_db_get_group(PyObject* rawself, PyObject* args) {
162
2
  AlpmDB* self = (AlpmDB*)rawself;
163
2
  char *grpname;
164
2
  alpm_group_t *grp;
165
2
  if (!PyArg_ParseTuple(args, "s", &grpname)) {
166
1
    PyErr_SetString(PyExc_TypeError, "expected string argument");
167
1
    return NULL;
168
  }
169
170
1
  grp = alpm_db_get_group(self->c_data, grpname);
171
1
  return _pyobject_from_pmgrp(grp);
172
}
173
174
6
static PyObject *pyalpm_db_update(PyObject *rawself, PyObject *args, PyObject *kwargs) {
175
6
  AlpmDB* self = (AlpmDB*)rawself;
176
6
  char* keyword[] = {"force", NULL};
177
6
  int ret;
178
6
  PyObject *force;
179
6
  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", keyword, &PyBool_Type, &force))
180
    return NULL;
181
182
6
  ret = alpm_db_update((force == Py_True), self->c_data);
183
184

6
  switch(ret) {
185
  case -1:
186
    RET_ERR("unable to update database", 0, NULL);
187
5
  case 0:
188
5
    Py_RETURN_TRUE;
189
1
  case 1:
190
1
    Py_RETURN_FALSE;
191
  default:
192
    RET_ERR("invalid return code from alpm_db_update()", 0, NULL);
193
  }
194
}
195
196
2
static PyObject* pyalpm_db_search(PyObject *rawself, PyObject *args) {
197
2
  AlpmDB* self = (AlpmDB *)rawself;
198
2
  alpm_list_t* rawargs;
199
2
  alpm_list_t* result;
200
2
  int ok = pylist_string_to_alpmlist(args, &rawargs);
201
2
  if (ok == -1) return NULL;
202
203
2
  result = alpm_db_search(self->c_data, rawargs);
204
2
  return alpmlist_to_pylist(result, pyalpm_package_from_pmpkg);
205
}
206
207
static struct PyMethodDef db_methods[] = {
208
  { "get_pkg", pyalpm_db_get_pkg, METH_VARARGS,
209
    "get a package by name\n"
210
    "args: a package name (string)\n"
211
    "returns: a Package object or None if not found" },
212
  { "search", pyalpm_db_search, METH_VARARGS,
213
    "search for packages matching a list of regexps\n"
214
    "args: a variable number of regexps (strings)\n"
215
    "returns: packages matching all these regexps" },
216
  { "read_grp", pyalpm_db_get_group, METH_VARARGS,
217
    "get contents of a group\n"
218
    "args: a group name (string)\n"
219
    "returns: a tuple (group name, list of packages)" },
220
  { "update", (PyCFunction)pyalpm_db_update, METH_VARARGS | METH_KEYWORDS,
221
    "update a database from its url attribute\n"
222
    "args: force (update even if DB is up to date, boolean)\n"
223
    "returns: True if an update has been done" },
224
  { NULL },
225
};
226
227
struct PyGetSetDef db_getset[] = {
228
  /* description properties */
229
  { "name", (getter)pyalpm_db_get_name, 0,
230
    "database name (e.g. \"core\", \"extra\")", NULL } ,
231
  { "servers", (getter)pyalpm_db_get_servers, (setter)pyalpm_db_set_servers,
232
    "a list of URLs (for sync DBs)", NULL } ,
233
  { "pkgcache", (getter)pyalpm_db_get_pkgcache, 0, "(read only) list of packages", NULL } ,
234
  { "grpcache", (getter)pyalpm_db_get_grpcache, 0, "(read only) list of package groups", NULL } ,
235
  { NULL }
236
};
237
238
static PyTypeObject AlpmDBType = {
239
  PyVarObject_HEAD_INIT(NULL, 0)
240
  "alpm.DB",             /*tp_name*/
241
  sizeof(AlpmDB),        /*tp_basicsize*/
242
  0,                          /*tp_itemsize*/
243
  .tp_dealloc = (destructor)pyalpm_db_dealloc,
244
  .tp_flags = Py_TPFLAGS_DEFAULT,
245
  .tp_doc = "libalpm DB object",
246
  .tp_methods = db_methods,
247
  .tp_getset = db_getset,
248
};
249
250
/** Initializes Pb class in module */
251
1
void init_pyalpm_db(PyObject *module) {
252
1
  PyObject *type;
253
254
1
  if (PyType_Ready(&AlpmDBType) < 0)
255
    return;
256
1
  type = (PyObject*)&AlpmDBType;
257
1
  Py_INCREF(type);
258
1
  PyModule_AddObject(module, "DB", type);
259
260
  /* signature check levels */
261
1
  PyModule_AddIntConstant(module, "SIG_DATABASE", ALPM_SIG_DATABASE);
262
1
  PyModule_AddIntConstant(module, "SIG_DATABASE_OPTIONAL", ALPM_SIG_DATABASE_OPTIONAL);
263
1
  PyModule_AddIntConstant(module, "SIG_DATABASE_MARGINAL_OK", ALPM_SIG_DATABASE_MARGINAL_OK);
264
1
  PyModule_AddIntConstant(module, "SIG_DATABASE_UNKNOWN_OK", ALPM_SIG_DATABASE_UNKNOWN_OK);
265
}
266
267
268
21
PyObject *pyalpm_db_from_pmdb(void* data) {
269
21
  alpm_db_t *db = (alpm_db_t*)data;
270
21
  AlpmDB *self;
271
21
  self = (AlpmDB*)AlpmDBType.tp_alloc(&AlpmDBType, 0);
272
21
  if (self == NULL) {
273
    PyErr_SetString(PyExc_RuntimeError, "unable to create DB object");
274
    return NULL;
275
  }
276
277
21
  self->c_data = db;
278
21
  return (PyObject *)self;
279
}
280
281
/** non-class methods */
282
2
PyObject* pyalpm_find_grp_pkgs(PyObject* self, PyObject *args) {
283
2
  PyObject *dbs;
284
2
  char *grpname;
285
2
  alpm_list_t *db_list = NULL;
286
2
  alpm_list_t *pkg_list;
287
2
  PyObject *result;
288
289
2
  if (!PyArg_ParseTuple(args, "Os", &dbs, &grpname)) {
290
1
    PyErr_SetString(PyExc_TypeError, "expected arguments (list of dbs, group name)");
291
1
    return NULL;
292
  }
293
294
  {
295
1
    int ret = pylist_db_to_alpmlist(dbs, &db_list);
296
1
    if (ret == -1)
297
      return NULL;
298
  }
299
1
  pkg_list = alpm_find_group_pkgs(db_list, grpname);
300
1
  result = alpmlist_to_pylist(pkg_list, pyalpm_package_from_pmpkg);
301
1
  alpm_list_free(db_list);
302
1
  alpm_list_free(pkg_list);
303
1
  return result;
304
}
305
306
/** Finds an available upgrade for a package in a list of databases */
307
2
PyObject* pyalpm_sync_newversion(PyObject *self, PyObject* args) {
308
2
  PyObject *pkg;
309
2
  PyObject *dbs;
310
2
  alpm_list_t *db_list;
311
2
  alpm_pkg_t *result = NULL;
312
2
  if(!PyArg_ParseTuple(args, "OO", &pkg, &dbs)
313
1
      || !PyAlpmPkg_Check(pkg)
314
1
      || pylist_db_to_alpmlist(dbs, &db_list) == -1)
315
  {
316
1
    PyErr_SetString(PyExc_TypeError, "sync_newversion() takes a Package and a list of DBs");
317
1
    return NULL;
318
  }
319
320
  {
321
1
    alpm_pkg_t *rawpkg = pmpkg_from_pyalpm_pkg(pkg);
322
1
    if (rawpkg) {
323
1
      result = alpm_sync_newversion(rawpkg, db_list);
324
    }
325
1
    alpm_list_free(db_list);
326
  }
327
1
  if (!result)
328
1
    Py_RETURN_NONE;
329
  else
330
    return pyalpm_package_from_pmpkg(result);
331
}
332
333
/* vim: set ts=2 sw=2 et: */