]>
Commit | Line | Data |
---|---|---|
6e46b18e MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2017 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <Python.h> | |
22 | ||
23 | #include <pakfire/pakfire.h> | |
ad62bb07 | 24 | #include <pakfire/key.h> |
843fcc66 | 25 | #include <pakfire/repo.h> |
6e46b18e | 26 | |
ad62bb07 | 27 | #include "key.h" |
6e46b18e | 28 | #include "pakfire.h" |
843fcc66 | 29 | #include "repo.h" |
f989dacd | 30 | #include "util.h" |
6e46b18e MT |
31 | |
32 | static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
33 | PakfireObject* self = (PakfireObject *)type->tp_alloc(type, 0); | |
34 | if (self) { | |
35 | self->pakfire = NULL; | |
36 | } | |
37 | ||
38 | return (PyObject *)self; | |
39 | } | |
40 | ||
41 | static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { | |
42 | const char* path = NULL; | |
43 | const char* arch = NULL; | |
44 | ||
980ccfe6 | 45 | if (!PyArg_ParseTuple(args, "s|s", &path, &arch)) |
6e46b18e MT |
46 | return -1; |
47 | ||
48 | self->pakfire = pakfire_create(path, arch); | |
49 | if (!self->pakfire) | |
50 | return -1; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static void Pakfire_dealloc(PakfireObject* self) { | |
56 | if (self->pakfire) | |
57 | pakfire_unref(self->pakfire); | |
58 | ||
59 | Py_TYPE(self)->tp_free((PyObject *)self); | |
60 | } | |
61 | ||
62 | static PyObject* Pakfire_repr(PakfireObject* self) { | |
63 | const char* path = pakfire_get_path(self->pakfire); | |
64 | const char* arch = pakfire_get_arch(self->pakfire); | |
65 | ||
66 | return PyUnicode_FromFormat("<_pakfire.Pakfire %s (%s)>", path, arch); | |
67 | } | |
68 | ||
69 | static PyObject* Pakfire_get_path(PakfireObject* self) { | |
70 | const char* path = pakfire_get_path(self->pakfire); | |
71 | ||
72 | return PyUnicode_FromString(path); | |
73 | } | |
74 | ||
75 | static PyObject* Pakfire_get_arch(PakfireObject* self) { | |
76 | const char* arch = pakfire_get_arch(self->pakfire); | |
77 | ||
78 | return PyUnicode_FromString(arch); | |
79 | } | |
80 | ||
843fcc66 MT |
81 | static PyObject* Pakfire_get_installed_repo(PakfireObject* self) { |
82 | PakfireRepo repo = pakfire_get_installed_repo(self->pakfire); | |
83 | if (!repo) | |
84 | Py_RETURN_NONE; | |
85 | ||
86 | PyObject* obj = new_repo(self, pakfire_repo_get_name(repo)); | |
87 | Py_XINCREF(obj); | |
88 | ||
89 | return obj; | |
90 | } | |
91 | ||
92 | static int Pakfire_set_installed_repo(PakfireObject* self, PyObject* value) { | |
93 | #if 0 | |
94 | if (PyObject_Not(value)) { | |
95 | pakfire_pool_set_installed_repo(self->pool, NULL); | |
96 | return 0; | |
97 | } | |
98 | #endif | |
99 | ||
100 | if (!PyObject_TypeCheck(value, &RepoType)) { | |
101 | PyErr_SetString(PyExc_ValueError, "Argument must be a _pakfire.Repo object"); | |
102 | return -1; | |
103 | } | |
104 | ||
105 | RepoObject* repo = (RepoObject *)value; | |
106 | pakfire_set_installed_repo(self->pakfire, repo->repo); | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
7b526dd2 | 111 | static PyObject* _import_keylist(PakfireObject* pakfire, PakfireKey* keys) { |
ad62bb07 MT |
112 | PyObject* list = PyList_New(0); |
113 | ||
ad62bb07 MT |
114 | while (keys && *keys) { |
115 | PakfireKey key = *keys++; | |
116 | ||
7b526dd2 | 117 | PyObject* object = new_key(pakfire, key); |
ad62bb07 | 118 | PyList_Append(list, object); |
7b526dd2 MT |
119 | |
120 | // Drop reference to the Python object | |
ad62bb07 MT |
121 | Py_DECREF(object); |
122 | ||
7b526dd2 MT |
123 | // Drop reference to the key object |
124 | pakfire_key_unref(key); | |
ad62bb07 MT |
125 | } |
126 | ||
127 | return list; | |
128 | } | |
129 | ||
7b526dd2 MT |
130 | static PyObject* Pakfire_get_keys(PakfireObject* self) { |
131 | PakfireKey* keys = pakfire_key_list(self->pakfire); | |
132 | ||
133 | return _import_keylist(self, keys); | |
134 | } | |
135 | ||
ad62bb07 MT |
136 | static PyObject* Pakfire_get_key(PakfireObject* self, PyObject* args) { |
137 | const char* pattern = NULL; | |
138 | ||
139 | if (!PyArg_ParseTuple(args, "s", &pattern)) | |
140 | return NULL; | |
141 | ||
142 | PakfireKey key = pakfire_key_get(self->pakfire, pattern); | |
143 | if (!key) | |
144 | Py_RETURN_NONE; | |
145 | ||
146 | return new_key(self, key); | |
147 | } | |
148 | ||
149 | static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args) { | |
150 | const char* userid = NULL; | |
151 | ||
152 | if (!PyArg_ParseTuple(args, "s", &userid)) | |
153 | return NULL; | |
154 | ||
155 | PakfireKey key = pakfire_key_generate(self->pakfire, userid); | |
156 | assert(key); | |
157 | ||
158 | return new_key(self, key); | |
159 | } | |
160 | ||
7b526dd2 MT |
161 | static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) { |
162 | const char* data = NULL; | |
163 | ||
164 | if (!PyArg_ParseTuple(args, "s", &data)) | |
165 | return NULL; | |
166 | ||
167 | PakfireKey* keys = pakfire_key_import(self->pakfire, data); | |
168 | if (!keys) | |
169 | return NULL; // TODO Raise error from errno | |
170 | ||
171 | return _import_keylist(self, keys); | |
172 | } | |
173 | ||
f989dacd MT |
174 | static PyObject* Pakfire_whatprovides(PakfireObject* self, PyObject* args, PyObject* kwds) { |
175 | char* kwlist[] = {"provides", "glob", "icase", "name_only", NULL}; | |
176 | ||
177 | const char* provides; | |
178 | int glob = 0; | |
179 | int icase = 0; | |
180 | int name_only = 0; | |
181 | ||
182 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|iii", kwlist, &provides, &glob, &icase, &name_only)) | |
183 | return NULL; | |
184 | ||
185 | int flags = 0; | |
186 | if (glob) | |
187 | flags |= PAKFIRE_GLOB; | |
188 | if (icase) | |
189 | flags |= PAKFIRE_ICASE; | |
190 | if (name_only) | |
191 | flags |= PAKFIRE_NAME_ONLY; | |
192 | ||
193 | PakfirePackageList list = pakfire_whatprovides(self->pakfire, provides, flags); | |
194 | ||
195 | return PyList_FromPackageList(self, list); | |
196 | } | |
197 | ||
198 | static PyObject* Pakfire_search(PakfireObject* self, PyObject* args) { | |
199 | const char* what; | |
200 | ||
201 | if (!PyArg_ParseTuple(args, "s", &what)) | |
202 | return NULL; | |
203 | ||
204 | PakfirePackageList list = pakfire_search(self->pakfire, what, 0); | |
205 | return PyList_FromPackageList(self, list); | |
206 | } | |
207 | ||
208 | static PyObject* Pakfire_version_compare(PakfireObject* self, PyObject* args) { | |
209 | const char* evr1 = NULL; | |
210 | const char* evr2 = NULL; | |
211 | ||
212 | if (!PyArg_ParseTuple(args, "ss", &evr1, &evr2)) | |
213 | return NULL; | |
214 | ||
215 | int cmp = pakfire_version_compare(self->pakfire, evr1, evr2); | |
216 | ||
217 | return PyLong_FromLong(cmp); | |
218 | } | |
219 | ||
86671603 MT |
220 | static PyObject* Pakfire_get_installonly(PakfireObject* self) { |
221 | const char** installonly = pakfire_get_installonly(self->pakfire); | |
222 | ||
223 | PyObject* list = PyList_New(0); | |
224 | const char* name; | |
225 | ||
226 | while ((name = *installonly++) != NULL) { | |
227 | PyObject* item = PyUnicode_FromString(name); | |
228 | PyList_Append(list, item); | |
229 | ||
230 | Py_DECREF(item); | |
231 | } | |
232 | ||
233 | Py_INCREF(list); | |
234 | return list; | |
235 | } | |
236 | ||
237 | static int Pakfire_set_installonly(PakfireObject* self, PyObject* value) { | |
238 | if (!PySequence_Check(value)) { | |
239 | PyErr_SetString(PyExc_AttributeError, "Expected a sequence."); | |
240 | return -1; | |
241 | } | |
242 | ||
243 | const int length = PySequence_Length(value); | |
244 | const char* installonly[length + 1]; | |
245 | ||
246 | for (int i = 0; i < length; i++) { | |
247 | PyObject* item = PySequence_GetItem(value, i); | |
248 | ||
249 | installonly[i] = PyUnicode_AsUTF8(item); | |
250 | Py_DECREF(item); | |
251 | } | |
252 | installonly[length] = NULL; | |
253 | ||
254 | pakfire_set_installonly(self->pakfire, installonly); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
6e46b18e | 259 | static struct PyMethodDef Pakfire_methods[] = { |
ad62bb07 MT |
260 | { |
261 | "generate_key", | |
262 | (PyCFunction)Pakfire_generate_key, | |
263 | METH_VARARGS, | |
264 | NULL | |
265 | }, | |
266 | { | |
267 | "get_key", | |
268 | (PyCFunction)Pakfire_get_key, | |
269 | METH_VARARGS, | |
270 | NULL | |
271 | }, | |
7b526dd2 MT |
272 | { |
273 | "import_key", | |
274 | (PyCFunction)Pakfire_import_key, | |
275 | METH_VARARGS, | |
276 | NULL | |
277 | }, | |
f989dacd MT |
278 | { |
279 | "search", | |
280 | (PyCFunction)Pakfire_search, | |
281 | METH_VARARGS, | |
282 | NULL | |
283 | }, | |
284 | { | |
285 | "version_compare", | |
286 | (PyCFunction)Pakfire_version_compare, | |
287 | METH_VARARGS, | |
288 | NULL | |
289 | }, | |
290 | { | |
291 | "whatprovides", | |
292 | (PyCFunction)Pakfire_whatprovides, | |
293 | METH_VARARGS|METH_KEYWORDS, | |
294 | NULL | |
295 | }, | |
6e46b18e MT |
296 | { NULL }, |
297 | }; | |
298 | ||
299 | static struct PyGetSetDef Pakfire_getsetters[] = { | |
300 | { | |
301 | "arch", | |
302 | (getter)Pakfire_get_arch, | |
303 | NULL, | |
304 | NULL, | |
305 | NULL | |
306 | }, | |
843fcc66 MT |
307 | { |
308 | "installed_repo", | |
309 | (getter)Pakfire_get_installed_repo, | |
310 | (setter)Pakfire_set_installed_repo, | |
311 | NULL, | |
312 | NULL | |
313 | }, | |
86671603 MT |
314 | { |
315 | "installonly", | |
316 | (getter)Pakfire_get_installonly, | |
317 | (setter)Pakfire_set_installonly, | |
318 | NULL, | |
319 | NULL | |
320 | }, | |
ad62bb07 MT |
321 | { |
322 | "keys", | |
323 | (getter)Pakfire_get_keys, | |
324 | NULL, | |
325 | NULL, | |
326 | NULL | |
327 | }, | |
6e46b18e MT |
328 | { |
329 | "path", | |
330 | (getter)Pakfire_get_path, | |
331 | NULL, | |
332 | NULL, | |
333 | NULL | |
334 | }, | |
335 | { NULL }, | |
336 | }; | |
337 | ||
338 | PyTypeObject PakfireType = { | |
339 | PyVarObject_HEAD_INIT(NULL, 0) | |
340 | tp_name: "_pakfire.Pakfire", | |
341 | tp_basicsize: sizeof(PakfireObject), | |
342 | tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
343 | tp_new: Pakfire_new, | |
344 | tp_dealloc: (destructor)Pakfire_dealloc, | |
345 | tp_init: (initproc)Pakfire_init, | |
346 | tp_doc: "Pakfire object", | |
347 | tp_methods: Pakfire_methods, | |
348 | tp_getset: Pakfire_getsetters, | |
349 | tp_repr: (reprfunc)Pakfire_repr, | |
350 | }; |