]> git.ipfire.org Git - people/stevee/pakfire.git/blame - src/_pakfire/pool.c
libpakfire: Make Pakfire parent object of Repo
[people/stevee/pakfire.git] / src / _pakfire / pool.c
CommitLineData
b792d887
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2011 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#############################################################################*/
c605d735 20
2e596ab8 21#include <Python.h>
e313dac1 22#include <fnmatch.h>
45f5a3d9 23#include <solv/poolarch.h>
e313dac1 24#include <solv/solver.h>
c605d735 25
b8a51cb6 26#include <pakfire/errno.h>
af2ad1e0 27#include <pakfire/pakfire.h>
b8a51cb6
MT
28#include <pakfire/pool.h>
29#include <pakfire/repo.h>
30
b55979b4 31#include "constants.h"
af2ad1e0 32#include "pakfire.h"
c605d735 33#include "pool.h"
376eb555 34#include "relation.h"
b8a51cb6 35#include "util.h"
c605d735 36
b8a51cb6
MT
37static PyObject* Pool_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
38 PoolObject* self = (PoolObject *)type->tp_alloc(type, 0);
39 if (self) {
40 self->pool = NULL;
c605d735
MT
41 }
42
43 return (PyObject *)self;
44}
45
b8a51cb6
MT
46static void Pool_dealloc(PoolObject* self) {
47 if (self->pool)
af2ad1e0 48 pakfire_pool_unref(self->pool);
4069d10c 49
b8a51cb6 50 Py_TYPE(self)->tp_free((PyObject *)self);
c605d735
MT
51}
52
b8a51cb6 53static int Pool_init(PoolObject* self, PyObject* args, PyObject* kwds) {
af2ad1e0 54 PakfireObject* pakfire = NULL;
b8a51cb6 55
af2ad1e0 56 if (!PyArg_ParseTuple(args, "O!", &PakfireType, &pakfire))
b8a51cb6
MT
57 return -1;
58
af2ad1e0
MT
59 self->pool = pakfire_get_pool(pakfire->pakfire);
60 if (!self->pool)
b8a51cb6 61 return -1;
c605d735 62
b8a51cb6
MT
63 return 0;
64}
65
66static PyObject* Pool_version_compare(PoolObject* self, PyObject* args) {
67 const char* evr1 = NULL;
68 const char* evr2 = NULL;
c605d735 69
b8a51cb6 70 if (!PyArg_ParseTuple(args, "ss", &evr1, &evr2))
c605d735
MT
71 return NULL;
72
b8a51cb6
MT
73 int cmp = pakfire_pool_version_compare(self->pool, evr1, evr2);
74 return PyLong_FromLong(cmp);
75}
76
77static Py_ssize_t Pool_len(PoolObject* self) {
78 return pakfire_pool_count(self->pool);
c605d735
MT
79}
80
b8a51cb6
MT
81static PyObject* Pool_get_installonly(PoolObject* self) {
82 const char** installonly = pakfire_pool_get_installonly(self->pool);
f1ab3110 83
b8a51cb6
MT
84 PyObject* list = PyList_New(0);
85 const char* name;
f1ab3110 86
b8a51cb6
MT
87 while ((name = *installonly++) != NULL) {
88 PyObject* item = PyUnicode_FromString(name);
89 PyList_Append(list, item);
f1ab3110 90
b8a51cb6 91 Py_DECREF(item);
f1ab3110
MT
92 }
93
b8a51cb6 94 Py_INCREF(list);
f1ab3110
MT
95 return list;
96}
97
b8a51cb6
MT
98static int Pool_set_installonly(PoolObject* self, PyObject* value) {
99 if (!PySequence_Check(value)) {
100 PyErr_SetString(PyExc_AttributeError, "Expected a sequence.");
101 return -1;
f1ab3110
MT
102 }
103
b8a51cb6
MT
104 const int length = PySequence_Length(value);
105 const char* installonly[length + 1];
c605d735 106
b8a51cb6
MT
107 for (int i = 0; i < length; i++) {
108 PyObject* item = PySequence_GetItem(value, i);
c605d735 109
b8a51cb6
MT
110 installonly[i] = PyUnicode_AsUTF8(item);
111 Py_DECREF(item);
c605d735 112 }
b8a51cb6 113 installonly[length] = NULL;
c605d735 114
b8a51cb6 115 pakfire_pool_set_installonly(self->pool, installonly);
c605d735 116
b8a51cb6 117 return 0;
c605d735
MT
118}
119
b8a51cb6
MT
120static PyObject* Pool_get_cache_path(PoolObject* self) {
121 const char* path = pakfire_pool_get_cache_path(self->pool);
122 if (!path)
123 Py_RETURN_NONE;
c605d735 124
b8a51cb6
MT
125 return PyUnicode_FromString(path);
126}
e313dac1 127
b8a51cb6
MT
128static int Pool_set_cache_path(PoolObject* self, PyObject* value) {
129 const char* path = PyUnicode_AsUTF8(value);
130 assert(path);
e313dac1 131
b8a51cb6
MT
132 pakfire_pool_set_cache_path(self->pool, path);
133 return 0;
134}
e313dac1 135
b8a51cb6
MT
136static PyObject* Pool_whatprovides(PoolObject* self, PyObject* args, PyObject* kwds) {
137 char* kwlist[] = {"provides", "glob", "icase", "name_only", NULL};
e313dac1 138
b8a51cb6
MT
139 const char* provides;
140 int glob = 0;
141 int icase = 0;
142 int name_only = 0;
e313dac1 143
b8a51cb6
MT
144 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|iii", kwlist, &provides, &glob, &icase, &name_only))
145 return NULL;
e313dac1 146
b8a51cb6
MT
147 int flags = 0;
148 if (glob)
149 flags |= PAKFIRE_GLOB;
150 if (icase)
151 flags |= PAKFIRE_ICASE;
152 if (name_only)
153 flags |= PAKFIRE_NAME_ONLY;
e313dac1 154
b8a51cb6 155 PakfirePackageList list = pakfire_pool_whatprovides(self->pool, provides, flags);
e313dac1 156
b8a51cb6
MT
157 return PyList_FromPackageList(self, list);
158}
e313dac1 159
b8a51cb6
MT
160static PyObject* Pool_search(PoolObject* self, PyObject* args) {
161 const char* what;
e313dac1 162
b8a51cb6
MT
163 if (!PyArg_ParseTuple(args, "s", &what))
164 return NULL;
e313dac1 165
b8a51cb6
MT
166 PakfirePackageList list = pakfire_pool_search(self->pool, what, 0);
167 return PyList_FromPackageList(self, list);
168}
c605d735 169
b8a51cb6
MT
170static struct PyMethodDef Pool_methods[] = {
171 {
172 "search",
173 (PyCFunction)Pool_search,
174 METH_VARARGS,
175 NULL
176 },
177 {
178 "version_compare",
179 (PyCFunction)Pool_version_compare,
180 METH_VARARGS,
181 NULL
182 },
183 {
184 "whatprovides",
185 (PyCFunction)Pool_whatprovides,
186 METH_VARARGS|METH_KEYWORDS,
187 NULL
188 },
189 { NULL }
190};
c605d735 191
b8a51cb6
MT
192static struct PyGetSetDef Pool_getsetters[] = {
193 {
194 "cache_path",
195 (getter)Pool_get_cache_path,
196 (setter)Pool_set_cache_path,
197 NULL,
198 NULL
199 },
b8a51cb6
MT
200 {
201 "installonly",
202 (getter)Pool_get_installonly,
203 (setter)Pool_set_installonly,
204 NULL,
205 NULL
206 },
207 { NULL }
208};
c605d735 209
b8a51cb6
MT
210static PySequenceMethods Pool_sequence = {
211 sq_length: (lenfunc)Pool_len,
212};
c605d735 213
b8a51cb6
MT
214PyTypeObject PoolType = {
215 PyVarObject_HEAD_INIT(NULL, 0)
216 tp_name: "_pakfire.Pool",
217 tp_basicsize: sizeof(PoolObject),
218 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
219 tp_new: Pool_new,
220 tp_dealloc: (destructor)Pool_dealloc,
221 tp_init: (initproc)Pool_init,
222 tp_doc: "Pool object",
223 tp_methods: Pool_methods,
224 tp_getset: Pool_getsetters,
225 tp_as_sequence: &Pool_sequence,
226};