GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/options.c Lines: 152 199 76.4 %
Date: 2019-02-09 19:11:25 Branches: 32 82 39.0 %

Line Branch Exec Source
1
/**
2
 * options.c : options module for pyalpm
3
 *
4
 *  Copyright 2008 Imanol Celaya <ilcra1989@gmail.com>
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 <Python.h>
24
#include <alpm.h>
25
#include "handle.h"
26
#include "options.h"
27
#include "util.h"
28
29
static int PyLong_to_int(PyObject *value, int overflow_val)
30
{
31
2
  int overflow;
32
4
  long lval = PyLong_AsLongAndOverflow(value, &overflow);
33

2
  if (overflow != 0) return overflow_val;
34

2
  if (lval > INT_MAX || lval < INT_MIN) return overflow_val;
35
2
  return (int)lval;
36
}
37
38
/** Boolean options
39
 */
40
/*
41
receives and returns an int type
42
1 = enabled
43
0 = disabled
44
*/
45
1
PyObject * option_get_usesyslog_alpm(PyObject *self, void* closure)
46
{
47
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
48
1
  int ret = alpm_option_get_usesyslog(handle);
49
50
1
  if(ret == -1)
51
  {
52
    RET_ERR("failed getting usesyslog", alpm_errno(handle), NULL);
53
  }
54
  else
55
1
    return PyLong_FromLong(ret);
56
}
57
58
1
int option_set_usesyslog_alpm(PyObject *self, PyObject *value, void* closure)
59
{
60
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
61
1
  if(!PyLong_Check(value))
62
  {
63
    PyErr_SetString(PyExc_TypeError, "wrong arguments");
64
    return -1;
65
  }
66
67
1
  alpm_option_set_usesyslog(handle, PyLong_to_int(value, -1));
68
1
  return 0;
69
}
70
71
1
PyObject* option_get_deltaratio_alpm(PyObject *self, void* closure) {
72
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
73
1
  double ret = alpm_option_get_deltaratio(handle);
74
1
  if (ret == -1) {
75
    RET_ERR("failed getting deltaratio", alpm_errno(handle), NULL);
76
  } else
77
1
    return PyFloat_FromDouble(ret);
78
}
79
80
int option_set_deltaratio_alpm(PyObject *self, PyObject *value, void* closure)
81
{
82
  alpm_handle_t *handle = ALPM_HANDLE(self);
83
  double fval = PyFloat_AsDouble(value);
84
85
  if(PyErr_Occurred()) {
86
    return -1;
87
  }
88
89
  alpm_option_set_deltaratio(handle, fval);
90
  return 0;
91
}
92
93
2
PyObject* option_get_checkspace_alpm(PyObject *self, void* closure) {
94
2
  alpm_handle_t *handle = ALPM_HANDLE(self);
95
2
  int ret = alpm_option_get_checkspace(handle);
96
2
  if (ret == -1) {
97
    RET_ERR("failed getting checkspace", alpm_errno(handle), NULL);
98
  } else
99
2
    return PyLong_FromLong(ret);
100
}
101
102
1
int option_set_checkspace_alpm(PyObject *self, PyObject *value, void* closure)
103
{
104
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
105
1
  if(!PyLong_Check(value))
106
  {
107
    PyErr_SetString(PyExc_TypeError, "wrong arguments");
108
    return -1;
109
  }
110
1
  alpm_option_set_checkspace(handle, PyLong_to_int(value, -1));
111
1
  return 0;
112
}
113
114
/**
115
 * List options
116
 * in addition to getters/setters, these have add/remove methods
117
 */
118
119
3
PyObject* option_get_cachedirs_alpm(PyObject *self, void* closure) {
120
3
  alpm_handle_t *handle = ALPM_HANDLE(self);
121
3
  return alpmlist_to_pylist(alpm_option_get_cachedirs(handle), pyobject_from_string);
122
}
123
124
1
int option_set_cachedirs_alpm(PyObject *self, PyObject *value, void *closure)
125
{
126
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
127
1
  alpm_list_t *target;
128
1
  if (pylist_string_to_alpmlist(value, &target) == -1)
129
    return -1;
130
131
1
  alpm_option_set_cachedirs(handle, target);
132
1
  return 0;
133
}
134
135
4
PyObject* option_get_noupgrades_alpm(PyObject *self, void* closure) {
136
4
  alpm_handle_t *handle = ALPM_HANDLE(self);
137
4
  return alpmlist_to_pylist(alpm_option_get_noupgrades(handle), pyobject_from_string);
138
}
139
140
1
int option_set_noupgrades_alpm(PyObject *self, PyObject *value, void *closure)
141
{
142
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
143
1
  alpm_list_t *target;
144
1
  if (pylist_string_to_alpmlist(value, &target) == -1)
145
    return -1;
146
147
1
  alpm_option_set_noupgrades(handle, target);
148
1
  return 0;
149
}
150
151
3
PyObject* option_get_noextracts_alpm(PyObject *self, void* closure) {
152
3
  alpm_handle_t *handle = ALPM_HANDLE(self);
153
3
  return alpmlist_to_pylist(alpm_option_get_noextracts(handle), pyobject_from_string);
154
}
155
156
1
int option_set_noextracts_alpm(PyObject *self, PyObject *value, void *closure)
157
{
158
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
159
1
  alpm_list_t *target;
160
1
  if (pylist_string_to_alpmlist(value, &target) == -1)
161
    return -1;
162
163
1
  alpm_option_set_noextracts(handle, target);
164
1
  return 0;
165
}
166
167
3
PyObject* option_get_ignorepkgs_alpm(PyObject *self, void* closure) {
168
3
  alpm_handle_t *handle = ALPM_HANDLE(self);
169
3
  return alpmlist_to_pylist(alpm_option_get_ignorepkgs(handle), pyobject_from_string);
170
}
171
172
1
int option_set_ignorepkgs_alpm(PyObject *self, PyObject *value, void *closure)
173
{
174
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
175
1
  alpm_list_t *target;
176
1
  if (pylist_string_to_alpmlist(value, &target) == -1)
177
    return -1;
178
179
1
  alpm_option_set_ignorepkgs(handle, target);
180
1
  return 0;
181
}
182
183
3
PyObject* option_get_ignoregrps_alpm(PyObject *self, void* closure) {
184
3
  alpm_handle_t *handle = ALPM_HANDLE(self);
185
3
  return alpmlist_to_pylist(alpm_option_get_ignoregroups(handle), pyobject_from_string);
186
}
187
188
1
int option_set_ignoregrps_alpm(PyObject *self, PyObject *value, void *closure)
189
{
190
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
191
1
  alpm_list_t *target;
192
1
  if (pylist_string_to_alpmlist(value, &target) == -1)
193
    return -1;
194
195
1
  alpm_option_set_ignoregroups(handle, target);
196
1
  return 0;
197
}
198
199
/* list options modifiers : add/remove */
200
201
1
PyObject* option_add_noupgrade_alpm(PyObject *self, PyObject *args)
202
{
203
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
204
1
  const char *str;
205
206
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
207
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
208
    return NULL;
209
  }
210
1
  alpm_option_add_noupgrade(handle, str);
211
1
  Py_RETURN_NONE;
212
}
213
214
1
PyObject* option_remove_noupgrade_alpm(PyObject *self, PyObject *args)
215
{
216
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
217
1
  const char *str;
218
219
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
220
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
221
    return NULL;
222
  }
223
1
  alpm_option_remove_noupgrade(handle, str);
224
1
  Py_RETURN_NONE;
225
}
226
227
1
PyObject* option_add_cachedir_alpm(PyObject *self, PyObject *args)
228
{
229
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
230
1
  const char *str;
231
232
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
233
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
234
    return NULL;
235
  }
236
1
  alpm_option_add_cachedir(handle, str);
237
1
  Py_RETURN_NONE;
238
}
239
240
1
PyObject* option_remove_cachedir_alpm(PyObject *self, PyObject *args)
241
{
242
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
243
1
  const char *str;
244
245
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
246
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
247
    return NULL;
248
  }
249
1
  alpm_option_remove_cachedir(handle, str);
250
1
  Py_RETURN_NONE;
251
}
252
253
1
PyObject* option_add_noextract_alpm(PyObject *self, PyObject *args)
254
{
255
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
256
1
  const char *str;
257
258
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
259
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
260
    return NULL;
261
  }
262
1
  alpm_option_add_noextract(handle, str);
263
1
  Py_RETURN_NONE;
264
}
265
266
1
PyObject* option_remove_noextract_alpm(PyObject *self, PyObject *args)
267
{
268
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
269
1
  const char *str;
270
271
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
272
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
273
    return NULL;
274
  }
275
1
  alpm_option_remove_noextract(handle, str);
276
1
  Py_RETURN_NONE;
277
}
278
279
1
PyObject* option_add_ignorepkg_alpm(PyObject *self, PyObject *args)
280
{
281
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
282
1
  const char *str;
283
284
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
285
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
286
    return NULL;
287
  }
288
1
  alpm_option_add_ignorepkg(handle, str);
289
1
  Py_RETURN_NONE;
290
}
291
292
1
PyObject* option_remove_ignorepkg_alpm(PyObject *self, PyObject *args)
293
{
294
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
295
1
  const char *str;
296
297
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
298
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
299
    return NULL;
300
  }
301
1
  alpm_option_remove_ignorepkg(handle, str);
302
1
  Py_RETURN_NONE;
303
}
304
305
1
PyObject* option_add_ignoregrp_alpm(PyObject *self, PyObject *args)
306
{
307
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
308
1
  const char *str;
309
310
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
311
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
312
    return NULL;
313
  }
314
1
  alpm_option_add_ignoregroup(handle, str);
315
1
  Py_RETURN_NONE;
316
}
317
318
1
PyObject* option_remove_ignoregrp_alpm(PyObject *self, PyObject *args)
319
{
320
1
  alpm_handle_t *handle = ALPM_HANDLE(self);
321
1
  const char *str;
322
323
1
  if(!PyArg_ParseTuple(args, "s", &str)) {
324
    PyErr_SetString(PyExc_TypeError, "expecting a string argument");
325
    return NULL;
326
  }
327
1
  alpm_option_remove_ignoregroup(handle, str);
328
1
  Py_RETURN_NONE;
329
}
330
331
/** Callback wrappers */
332
extern PyObject *global_py_callbacks[N_CALLBACKS];
333
334
9
void pyalpm_logcb(alpm_loglevel_t level, const char *fmt, va_list va_args) {
335
9
  char *log;
336
9
  PyObject *result;
337
9
  int ret;
338
339
9
  ret = vasprintf(&log, fmt, va_args);
340
9
  if(ret == -1)
341
    log = "pyalpm_logcb: could not allocate memory";
342
9
  result = PyObject_CallFunction(global_py_callbacks[CB_LOG], "is", level, log);
343
9
  if (!result) PyErr_Print();
344

9
  Py_CLEAR(result);
345
9
  if (ret != -1) free(log);
346
9
}
347
348
112
void pyalpm_dlcb(const char *filename, off_t xfered, off_t total) {
349
112
  PyObject *result;
350
112
  result = PyObject_CallFunction(global_py_callbacks[CB_DOWNLOAD], "sii", filename, xfered, total);
351
112
  if (!result) PyErr_Print();
352

112
  Py_CLEAR(result);
353
112
}
354
355
void pyalpm_totaldlcb(off_t total) {
356
  PyObject *result;
357
  result = PyObject_CallFunction(global_py_callbacks[CB_TOTALDL], "i", total);
358
  if (!result) PyErr_Print();
359
  Py_CLEAR(result);
360
}
361
362
int pyalpm_fetchcb(const char *url, const char *localpath, int force) {
363
  PyObject *result;
364
  result = PyObject_CallFunction(global_py_callbacks[CB_FETCH], "ssi", url, localpath, force);
365
  if (!result) return -1;
366
  if (!PyLong_Check(result)) {
367
    return -1;
368
  } else {
369
    int ret = PyLong_to_int(result, -1);
370
    Py_DECREF(result);
371
    return ret;
372
  }
373
}
374
375
/* vim: set ts=2 sw=2 et: */