]> git.ipfire.org Git - pakfire.git/blame - src/_pakfire/pakfire.c
libpakfire: Drop unused function
[pakfire.git] / src / _pakfire / pakfire.c
CommitLineData
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
32static 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
41static 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
55static 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
62static 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
69static PyObject* Pakfire_get_path(PakfireObject* self) {
70 const char* path = pakfire_get_path(self->pakfire);
71
72 return PyUnicode_FromString(path);
73}
74
75static 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
81static 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
92static 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 111static 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
130static 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
136static 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
149static 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
161static 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
174static 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
198static 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
208static 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
6e46b18e 220static struct PyMethodDef Pakfire_methods[] = {
ad62bb07
MT
221 {
222 "generate_key",
223 (PyCFunction)Pakfire_generate_key,
224 METH_VARARGS,
225 NULL
226 },
227 {
228 "get_key",
229 (PyCFunction)Pakfire_get_key,
230 METH_VARARGS,
231 NULL
232 },
7b526dd2
MT
233 {
234 "import_key",
235 (PyCFunction)Pakfire_import_key,
236 METH_VARARGS,
237 NULL
238 },
f989dacd
MT
239 {
240 "search",
241 (PyCFunction)Pakfire_search,
242 METH_VARARGS,
243 NULL
244 },
245 {
246 "version_compare",
247 (PyCFunction)Pakfire_version_compare,
248 METH_VARARGS,
249 NULL
250 },
251 {
252 "whatprovides",
253 (PyCFunction)Pakfire_whatprovides,
254 METH_VARARGS|METH_KEYWORDS,
255 NULL
256 },
6e46b18e
MT
257 { NULL },
258};
259
260static struct PyGetSetDef Pakfire_getsetters[] = {
261 {
262 "arch",
263 (getter)Pakfire_get_arch,
264 NULL,
265 NULL,
266 NULL
267 },
843fcc66
MT
268 {
269 "installed_repo",
270 (getter)Pakfire_get_installed_repo,
271 (setter)Pakfire_set_installed_repo,
272 NULL,
273 NULL
274 },
ad62bb07
MT
275 {
276 "keys",
277 (getter)Pakfire_get_keys,
278 NULL,
279 NULL,
280 NULL
281 },
6e46b18e
MT
282 {
283 "path",
284 (getter)Pakfire_get_path,
285 NULL,
286 NULL,
287 NULL
288 },
289 { NULL },
290};
291
292PyTypeObject PakfireType = {
293 PyVarObject_HEAD_INIT(NULL, 0)
294 tp_name: "_pakfire.Pakfire",
295 tp_basicsize: sizeof(PakfireObject),
296 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
297 tp_new: Pakfire_new,
298 tp_dealloc: (destructor)Pakfire_dealloc,
299 tp_init: (initproc)Pakfire_init,
300 tp_doc: "Pakfire object",
301 tp_methods: Pakfire_methods,
302 tp_getset: Pakfire_getsetters,
303 tp_repr: (reprfunc)Pakfire_repr,
304};