GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/package.c Lines: 111 156 71.2 %
Date: 2019-02-09 19:11:25 Branches: 28 86 32.6 %

Line Branch Exec Source
1
/**
2
 * package.c : wrapper class around alpm_pkg_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 <string.h>
25
#include <alpm.h>
26
#include <Python.h>
27
#include "db.h"
28
#include "util.h"
29
#include "handle.h"
30
#include "package.h"
31
32
PyTypeObject AlpmPackageType;
33
extern PyTypeObject AlpmHandleType;
34
35
2
int PyAlpmPkg_Check(PyObject *object) {
36

2
  return PyObject_TypeCheck(object, &AlpmPackageType);
37
}
38
39
1
static PyObject* pyalpm_pkg_repr(PyObject *rawself) {
40
1
  AlpmPackage *self = (AlpmPackage *)rawself;
41
1
  return PyUnicode_FromFormat("<alpm.Package(\"%s-%s-%s\") at %p>",
42
			      alpm_pkg_get_name(self->c_data),
43
			      alpm_pkg_get_version(self->c_data),
44
			      alpm_pkg_get_arch(self->c_data),
45
			      self);
46
}
47
48
1
static PyObject* pyalpm_pkg_str(PyObject *rawself) {
49
1
  AlpmPackage *self = (AlpmPackage *)rawself;
50
1
  return PyUnicode_FromFormat("alpm.Package(\"%s-%s-%s\")",
51
			      alpm_pkg_get_name(self->c_data),
52
			      alpm_pkg_get_version(self->c_data),
53
			      alpm_pkg_get_arch(self->c_data));
54
}
55
56
1
void pyalpm_pkg_unref(PyObject *object) {
57
1
  if (PyAlpmPkg_Check(object))
58
1
    ((AlpmPackage*)(object))->needs_free = 0;
59
1
}
60
61
6
static void pyalpm_package_dealloc(AlpmPackage *self) {
62
6
  if (self->needs_free)
63
    alpm_pkg_free(self->c_data);
64
6
  Py_TYPE(self)->tp_free((PyObject*)self);
65
6
}
66
67
/* Internal utility functions */
68
69
7
static PyObject* _pyobject_from_pmdepend(void* dep) {
70
7
  char *depstring = alpm_dep_compute_string((alpm_depend_t*)dep);
71
7
  PyObject *item = Py_BuildValue("s", depstring);
72
7
  free(depstring);
73
7
  return item;
74
};
75
76
6
PyObject *pyalpm_package_from_pmpkg(void* data) {
77
6
  AlpmPackage *self;
78
6
  alpm_pkg_t *p = (alpm_pkg_t*)data;
79
6
  self = (AlpmPackage*)AlpmPackageType.tp_alloc(&AlpmPackageType, 0);
80
6
  if (self == NULL) {
81
    PyErr_SetString(PyExc_RuntimeError, "unable to create package object");
82
    return NULL;
83
  }
84
85
6
  self->c_data = p;
86
6
  self->needs_free = 0;
87
6
  return (PyObject *)self;
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
5
static PyObject* _get_string_attribute(AlpmPackage *self, const char* getter(alpm_pkg_t*)) {
96
5
  const char *attr;
97
5
  if (! self->c_data) {
98
    PyErr_SetString(alpm_error, "data is not initialized");
99
    return NULL;
100
  }
101
5
  attr = getter(self->c_data);
102
5
  if (attr == NULL) Py_RETURN_NONE;
103
5
  return Py_BuildValue("s", attr);
104
}
105
106
struct list_getter {
107
  alpm_list_t *(*getter)(alpm_pkg_t *);
108
  PyObject *(*item_converter)(void *);
109
};
110
111
/** Returns a Python list attribute */
112
3
static PyObject* _get_list_attribute(AlpmPackage *self, const struct list_getter *closure) {
113
3
  alpm_list_t *result = NULL;
114
3
  CHECK_IF_INITIALIZED();
115
3
  result = closure->getter(self->c_data);
116
3
  return alpmlist_to_pylist(result, closure->item_converter);
117
}
118
119
2
alpm_pkg_t *pmpkg_from_pyalpm_pkg(PyObject *object) {
120
2
  AlpmPackage *self = (AlpmPackage*)object;
121
2
  CHECK_IF_INITIALIZED();
122
  return self->c_data;
123
}
124
125
/** Converts a Python list of packages to an alpm_list_t linked list.
126
 * return 0 on success, -1 on failure
127
 */
128
2
int pylist_pkg_to_alpmlist(PyObject *list, alpm_list_t **result) {
129
2
  alpm_list_t *ret = NULL;
130
2
  PyObject *iterator = PyObject_GetIter(list);
131
2
  PyObject *item;
132
133
2
  if(iterator == NULL) {
134
    PyErr_SetString(PyExc_TypeError, "object is not iterable");
135
    return -1;
136
  }
137
138
4
  while((item = PyIter_Next(iterator)))
139
  {
140

2
    if (PyObject_TypeCheck(item, &AlpmPackageType)) {
141
2
      ret = alpm_list_add(ret, ((AlpmPackage*)item)->c_data);
142
    } else {
143
      PyErr_SetString(PyExc_TypeError, "list must contain only Package objects");
144
      FREELIST(ret);
145
      return -1;
146
    }
147
2
    Py_DECREF(item);
148
  }
149
2
  Py_DECREF(iterator);
150
151
2
  *result = ret;
152
2
  return 0;
153
}
154
155
/* Python bindings */
156
157
PyObject *pyalpm_package_load(PyObject *self, PyObject *args, PyObject *kwargs) {
158
  alpm_handle_t *handle = ALPM_HANDLE(self);
159
  char *filename;
160
  int check_sig = ALPM_SIG_PACKAGE_OPTIONAL;
161
  char *kws[] = { "path", "check_sig", NULL };
162
  alpm_pkg_t *result;
163
  AlpmPackage *pyresult;
164
  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:load_pkg", kws, &filename, &check_sig)) {
165
    return NULL;
166
  }
167
168
  if ((alpm_pkg_load(handle, filename, 1, check_sig, &result) == -1) || !result) {
169
    RET_ERR("loading package failed", alpm_errno(handle), NULL);
170
  }
171
172
  pyresult = (AlpmPackage*)pyalpm_package_from_pmpkg(result);
173
  if (!pyresult) return NULL;
174
  pyresult->needs_free = 1;
175
  return (PyObject*)pyresult;
176
}
177
178
1
static PyObject* pyalpm_package_get_builddate(AlpmPackage *self, void *closure) {
179
1
  CHECK_IF_INITIALIZED();
180
1
  return PyLong_FromLongLong(alpm_pkg_get_builddate(self->c_data));
181
}
182
183
1
static PyObject* pyalpm_package_get_installdate(AlpmPackage *self, void *closure) {
184
1
  CHECK_IF_INITIALIZED();
185
1
  return PyLong_FromLongLong(alpm_pkg_get_installdate(self->c_data));
186
}
187
188
1
static PyObject* pyalpm_package_get_size(AlpmPackage *self, void *closure) {
189
1
  CHECK_IF_INITIALIZED();
190
1
  return PyLong_FromLongLong(alpm_pkg_get_size(self->c_data));
191
}
192
193
1
static PyObject* pyalpm_package_get_isize(AlpmPackage *self, void *closure) {
194
1
  CHECK_IF_INITIALIZED();
195
1
  return PyLong_FromLongLong(alpm_pkg_get_isize(self->c_data));
196
}
197
198
1
static PyObject* pyalpm_package_get_reason(AlpmPackage *self, void *closure) {
199
1
  CHECK_IF_INITIALIZED();
200
1
  return PyLong_FromLong(alpm_pkg_get_reason(self->c_data));
201
}
202
203
1
static PyObject* pyalpm_package_get_files(AlpmPackage *self, void *closure) {
204
1
  const alpm_filelist_t *flist = NULL;
205
1
  PyObject *result = NULL;
206
207
1
  CHECK_IF_INITIALIZED();
208
209
1
  flist = alpm_pkg_get_files(self->c_data);
210
1
  if (!flist)
211
    Py_RETURN_NONE;
212
  else {
213
1
    ssize_t i;
214
1
    result = PyList_New((Py_ssize_t)flist->count);
215
1
    for (i = 0; i < (ssize_t)flist->count; i++) {
216
      const alpm_file_t *file = flist->files + i;
217
      PyObject *filename = PyUnicode_DecodeFSDefault(file->name);
218
      PyObject *filesize = PyLong_FromLongLong(file->size);
219
      PyObject *filemode = PyLong_FromUnsignedLong(file->mode);
220
      PyObject *item = PyTuple_New(3);
221
      if (item && filename && filesize && filemode) {
222
        PyTuple_SET_ITEM(item, 0, filename);
223
        PyTuple_SET_ITEM(item, 1, filesize);
224
        PyTuple_SET_ITEM(item, 2, filemode);
225
        PyList_SET_ITEM(result, i, item);
226
      } else {
227
        Py_CLEAR(item);
228
        Py_CLEAR(filename);
229
        Py_CLEAR(filesize);
230
        Py_CLEAR(filemode);
231
        return NULL;
232
      }
233
    }
234
  }
235
  return result;
236
}
237
238
/** Convert alpm_backup_t to Python tuples
239
 * The resulting tuple is (filename, hexadecimal hash)
240
 */
241
static PyObject* pyobject_from_alpm_backup(void* data) {
242
  alpm_backup_t* item = (alpm_backup_t*)data;
243
  PyObject* tuple = Py_BuildValue("(ss)", item->name, item->hash);
244
  return tuple;
245
}
246
247
1
static PyObject* pyalpm_package_get_db(AlpmPackage *self, void *closure) {
248
1
  alpm_db_t* db;
249
1
  CHECK_IF_INITIALIZED();
250
1
  db = alpm_pkg_get_db(self->c_data);
251
1
  if (db)
252
1
    return pyalpm_db_from_pmdb(db);
253
  else
254
    Py_RETURN_NONE;
255
}
256
257
1
static PyObject* pyalpm_pkg_has_scriptlet(AlpmPackage *self, void *closure) {
258
1
  CHECK_IF_INITIALIZED();
259
1
  return PyBool_FromLong(alpm_pkg_has_scriptlet(self->c_data));
260
}
261
262
1
static PyObject* pyalpm_pkg_download_size(AlpmPackage *self, void *closure) {
263
1
  CHECK_IF_INITIALIZED();
264
1
  return PyLong_FromLongLong(alpm_pkg_download_size(self->c_data));
265
}
266
267
1
static PyObject* pyalpm_pkg_compute_requiredby(PyObject *rawself, PyObject *args) {
268
1
  AlpmPackage *self = (AlpmPackage*)rawself;
269
1
  PyObject *pyresult;
270
1
  CHECK_IF_INITIALIZED();
271
  {
272
1
    alpm_list_t *result = alpm_pkg_compute_requiredby(self->c_data);
273
1
    pyresult = alpmlist_to_pylist(result, pyobject_from_string);
274
1
    FREELIST(result);
275
  }
276
1
  return pyresult;
277
}
278
279
struct list_getter get_licenses = { alpm_pkg_get_licenses, pyobject_from_string };
280
struct list_getter get_groups   = { alpm_pkg_get_groups, pyobject_from_string };
281
struct list_getter get_backup   = { alpm_pkg_get_backup, pyobject_from_alpm_backup };
282
struct list_getter get_deltas   = { alpm_pkg_get_deltas, pyobject_from_string };
283
struct list_getter get_depends  = { alpm_pkg_get_depends, _pyobject_from_pmdepend };
284
struct list_getter get_optdepends = { alpm_pkg_get_optdepends, _pyobject_from_pmdepend };
285
struct list_getter get_replaces   = { alpm_pkg_get_replaces, _pyobject_from_pmdepend };
286
struct list_getter get_provides   = { alpm_pkg_get_provides, _pyobject_from_pmdepend };
287
struct list_getter get_conflicts  = { alpm_pkg_get_conflicts, _pyobject_from_pmdepend };
288
289
static struct PyGetSetDef AlpmPackageGetSet[] = {
290
  { "db", (getter)pyalpm_package_get_db, 0, "the database from which the package comes from, or None", NULL } ,
291
  /* description properties */
292
  { "name",    (getter)_get_string_attribute, 0, "package name",    alpm_pkg_get_name } ,
293
  { "version", (getter)_get_string_attribute, 0, "package version", alpm_pkg_get_version } ,
294
  { "desc",    (getter)_get_string_attribute, 0, "package desc",    alpm_pkg_get_desc } ,
295
  { "url",     (getter)_get_string_attribute, 0, "package URL",     alpm_pkg_get_url } ,
296
  { "arch",    (getter)_get_string_attribute, 0, "target architecture", alpm_pkg_get_arch } ,
297
  { "licenses", (getter)_get_list_attribute, 0, "list of licenses", &get_licenses } ,
298
  { "groups",   (getter)_get_list_attribute, 0, "list of package groups", &get_groups } ,
299
  /* package properties */
300
  { "packager",   (getter)_get_string_attribute, 0, "packager name", alpm_pkg_get_packager } ,
301
  { "md5sum",     (getter)_get_string_attribute, 0, "package md5sum", alpm_pkg_get_md5sum } ,
302
  { "sha256sum",  (getter)_get_string_attribute, 0, "package sha256sum as hexadecimal digits", alpm_pkg_get_sha256sum } ,
303
  { "base64_sig", (getter)_get_string_attribute, 0, "GPG signature encoded as base64", alpm_pkg_get_base64_sig } ,
304
  { "filename",   (getter)_get_string_attribute, 0, "package filename", alpm_pkg_get_filename } ,
305
  { "base", (getter)_get_string_attribute, 0, "package base name", alpm_pkg_get_base },
306
  { "size", (getter)pyalpm_package_get_size, 0, "package size", NULL } ,
307
  { "isize", (getter)pyalpm_package_get_isize, 0, "installed size", NULL } ,
308
  { "reason", (getter)pyalpm_package_get_reason, 0, "install reason (0 = explicit, 1 = depend)", NULL } ,
309
  { "builddate", (getter)pyalpm_package_get_builddate, 0, "building time", NULL } ,
310
  { "installdate", (getter)pyalpm_package_get_installdate, 0, "install time", NULL } ,
311
  { "files",  (getter)pyalpm_package_get_files, 0, "list of installed files", NULL } ,
312
  { "backup", (getter)_get_list_attribute, 0, "list of tuples (filename, md5sum)", &get_backup } ,
313
  { "deltas", (getter)_get_list_attribute, 0, "list of available deltas", &get_deltas } ,
314
  /* dependency information */
315
  { "depends",    (getter)_get_list_attribute, 0, "list of dependencies", &get_depends } ,
316
  { "optdepends", (getter)_get_list_attribute, 0, "list of optional dependencies", &get_optdepends } ,
317
  { "conflicts",  (getter)_get_list_attribute, 0, "list of conflicts", &get_conflicts } ,
318
  { "provides",   (getter)_get_list_attribute, 0, "list of provided package names", &get_provides } ,
319
  { "replaces",   (getter)_get_list_attribute, 0, "list of replaced packages", &get_replaces } ,
320
  /* miscellaneous information */
321
  { "has_scriptlet", (getter)pyalpm_pkg_has_scriptlet, 0, "True if the package has an install script", NULL },
322
  { "download_size", (getter)pyalpm_pkg_download_size, 0, "predicted download size for this package", NULL },
323
  { NULL }
324
};
325
326
static struct PyMethodDef pyalpm_pkg_methods[] = {
327
  { "compute_requiredby", pyalpm_pkg_compute_requiredby, METH_NOARGS,
328
      "computes the list of packages requiring this package" },
329
  { NULL }
330
};
331
332
PyTypeObject AlpmPackageType = {
333
  PyVarObject_HEAD_INIT(NULL, 0)
334
  "alpm.Package",             /*tp_name*/
335
  sizeof(AlpmPackage),        /*tp_basicsize*/
336
  0,                          /*tp_itemsize*/
337
  .tp_dealloc = (destructor)pyalpm_package_dealloc,
338
  .tp_repr = pyalpm_pkg_repr,
339
  .tp_str = pyalpm_pkg_str,
340
  .tp_flags = Py_TPFLAGS_DEFAULT,
341
  .tp_doc = "Package object",
342
  .tp_methods = pyalpm_pkg_methods,
343
  .tp_getset = AlpmPackageGetSet,
344
};
345
346
/** Initializes Package class in module */
347
1
void init_pyalpm_package(PyObject *module) {
348
1
  PyObject *type;
349
350
1
  if (PyType_Ready(&AlpmPackageType) < 0)
351
    return;
352
1
  type = (PyObject*)&AlpmPackageType;
353
1
  Py_INCREF(type);
354
1
  PyModule_AddObject(module, "Package", type);
355
356
  /* package reasons */
357
1
  PyModule_AddIntConstant(module, "PKG_REASON_EXPLICIT", ALPM_PKG_REASON_EXPLICIT);
358
1
  PyModule_AddIntConstant(module, "PKG_REASON_DEPEND", ALPM_PKG_REASON_DEPEND);
359
360
  /* signature check levels */
361
1
  PyModule_AddIntConstant(module, "SIG_PACKAGE", ALPM_SIG_PACKAGE);
362
1
  PyModule_AddIntConstant(module, "SIG_PACKAGE_OPTIONAL", ALPM_SIG_PACKAGE_OPTIONAL);
363
1
  PyModule_AddIntConstant(module, "SIG_PACKAGE_MARGINAL_OK", ALPM_SIG_PACKAGE_MARGINAL_OK);
364
1
  PyModule_AddIntConstant(module, "SIG_PACKAGE_UNKNOWN_OK", ALPM_SIG_PACKAGE_UNKNOWN_OK);
365
}
366
367
/* vim: set ts=2 sw=2 et: */