GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/options.c Lines: 172 188 91.5 %
Date: 2020-02-19 21:21:49 Branches: 43 78 55.1 %

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
6
static int PyLong_to_int(PyObject *value, int overflow_val)
30
{
31
6
  int overflow;
32
12
  long lval = PyLong_AsLongAndOverflow(value, &overflow);
33

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

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

27
  Py_CLEAR(result);
323
27
  if (ret != -1) free(log);
324
27
}
325
326
9
void pyalpm_dlcb(const char *filename, off_t xfered, off_t total) {
327
9
  PyObject *result;
328
9
  result = PyObject_CallFunction(global_py_callbacks[CB_DOWNLOAD], "sii", filename, xfered, total);
329
9
  if (!result) PyErr_Print();
330

9
  Py_CLEAR(result);
331
9
}
332
333
void pyalpm_totaldlcb(off_t total) {
334
  PyObject *result;
335
  result = PyObject_CallFunction(global_py_callbacks[CB_TOTALDL], "i", total);
336
  if (!result) PyErr_Print();
337
  Py_CLEAR(result);
338
}
339
340
int pyalpm_fetchcb(const char *url, const char *localpath, int force) {
341
  PyObject *result;
342
  result = PyObject_CallFunction(global_py_callbacks[CB_FETCH], "ssi", url, localpath, force);
343
  if (!result) return -1;
344
  if (!PyLong_Check(result)) {
345
    return -1;
346
  } else {
347
    int ret = PyLong_to_int(result, -1);
348
    Py_DECREF(result);
349
    return ret;
350
  }
351
}
352
353
/* vim: set ts=2 sw=2 et: */