]> git.ipfire.org Git - people/stevee/pakfire.git/blame - src/_pakfire/pool.c
Explicetly set libsolv disttype
[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
b55979b4 26#include "constants.h"
c605d735 27#include "pool.h"
376eb555 28#include "relation.h"
c605d735
MT
29#include "repo.h"
30#include "solvable.h"
31
32PyTypeObject PoolType = {
33 PyObject_HEAD_INIT(NULL)
34 tp_name: "_pakfire.Pool",
35 tp_basicsize: sizeof(PoolObject),
36 tp_flags: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
37 tp_new : Pool_new,
38 tp_dealloc: (destructor) Pool_dealloc,
39 tp_doc: "Sat Pool objects",
40};
41
42// Pool
43PyObject* Pool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
44 PoolObject *self;
45 const char *arch;
46
47 if (!PyArg_ParseTuple(args, "s", &arch)) {
48 /* XXX raise exception */
49 return NULL;
50 }
51
52 self = (PoolObject *)type->tp_alloc(type, 0);
53 if (self != NULL) {
54 self->_pool = pool_create();
55
56#ifdef DEBUG
57 // Enable debug messages when DEBUG is defined.
58 pool_setdebuglevel(self->_pool, 1);
59#endif
60
2539f45d 61 pool_setdisttype(self->_pool, DISTTYPE_RPM);
c605d735
MT
62 pool_setarch(self->_pool, arch);
63 if (self->_pool == NULL) {
64 Py_DECREF(self);
65 return NULL;
66 }
67 }
68
69 return (PyObject *)self;
70}
71
72PyObject *Pool_dealloc(PoolObject *self) {
714392de 73 pool_free(self->_pool);
c605d735 74 self->ob_type->tp_free((PyObject *)self);
4069d10c
MT
75
76 Py_RETURN_NONE;
c605d735
MT
77}
78
79PyObject *Pool_add_repo(PoolObject *self, PyObject *args) {
80 const char *name;
81 if (!PyArg_ParseTuple(args, "s", &name)) {
82 /* XXX raise exception */
83 }
84
85 RepoObject *repo;
86
87 repo = PyObject_New(RepoObject, &RepoType);
88 if (repo == NULL)
89 return NULL;
90
91 return (PyObject *)repo;
92}
93
94PyObject *Pool_prepare(PoolObject *self) {
95 _Pool_prepare(self->_pool);
96
97 Py_RETURN_NONE;
98}
99
100void _Pool_prepare(Pool *pool) {
101 pool_addfileprovides(pool);
102 pool_createwhatprovides(pool);
103
4069d10c 104 Repo *r;
c605d735
MT
105 int idx;
106 FOR_REPOS(idx, r) {
107 repo_internalize(r);
108 }
109}
110
111PyObject *Pool_size(PoolObject *self) {
112 Pool *pool = self->_pool;
113
114 return Py_BuildValue("i", pool->nsolvables);
115}
116
1f27e8fe 117PyObject *_Pool_search(Pool *pool, Repo *repo, const char *match, int option, const char *keyname) {
f1ab3110
MT
118 // Prepare the pool, so we can search in it.
119 _Pool_prepare(pool);
120
121 Dataiterator d;
1f27e8fe
MT
122 dataiterator_init(&d, pool, repo, 0,
123 keyname && pool ? pool_str2id(pool, keyname, 0) : 0, match, option);
f1ab3110
MT
124
125 PyObject *list = PyList_New(0);
126
127 SolvableObject *solvable;
128 while (dataiterator_step(&d)) {
129 solvable = PyObject_New(SolvableObject, &SolvableType);
130 solvable->_pool = pool;
131 solvable->_id = d.solvid;
132
133 PyList_Append(list, (PyObject *)solvable);
134 }
135
136 dataiterator_free(&d);
f1ab3110
MT
137 return list;
138}
139
c605d735 140PyObject *Pool_search(PoolObject *self, PyObject *args) {
f1ab3110
MT
141 const char *match = NULL;
142 int option = SEARCH_SUBSTRING;
1f27e8fe 143 const char *keyname = NULL;
f1ab3110 144
1f27e8fe 145 if (!PyArg_ParseTuple(args, "s|is", &match, &option, &keyname)) {
f1ab3110 146 /* XXX raise exception */
714392de 147 return NULL;
f1ab3110
MT
148 }
149
1f27e8fe 150 return _Pool_search(self->_pool, NULL, match, option, keyname);
c605d735
MT
151}
152
153PyObject *Pool_set_installed(PoolObject *self, PyObject *args) {
154 RepoObject *repo;
155
156 if (!PyArg_ParseTuple(args, "O", &repo)) {
157 /* XXX raise exception */
158 }
159
160 pool_set_installed(self->_pool, repo->_repo);
161
162 Py_RETURN_NONE;
163}
164
165PyObject *Pool_providers(PoolObject *self, PyObject *args) {
e313dac1
MT
166 char *name = NULL;
167 Queue job;
168 Solvable *solvable;
169 Pool *pool = self->_pool;
170 Id p, pp;
171 int i;
c605d735 172
e313dac1 173 if (!PyArg_ParseTuple(args, "s", &name)) {
714392de 174 return NULL;
c605d735
MT
175 }
176
c605d735 177 _Pool_prepare(pool);
e313dac1
MT
178 queue_init(&job);
179
180 Id id = pool_str2id(pool, name, 0);
181
182 if (id) {
183 FOR_PROVIDES(p, pp, id) {
184 solvable = pool->solvables + p;
185
186 if (solvable->name == id)
187 queue_push2(&job, SOLVER_SOLVABLE, p);
188 }
189 }
190
191 for (p = 1; p < pool->nsolvables; p++) {
192 solvable = pool->solvables + p;
193 if (!solvable->repo || !pool_installable(pool, solvable))
194 continue;
195
196 id = solvable->name;
197 if (fnmatch(name, pool_id2str(pool, id), 0) == 0) {
198 for (i = 0; i < job.count; i += 2) {
199 if (job.elements[i] == SOLVER_SOLVABLE && job.elements[i + 1] == id)
200 break;
201 }
202
203 if (i == job.count)
204 queue_push2(&job, SOLVER_SOLVABLE, p);
205 }
206 }
207
208 for (id = 1; id < pool->ss.nstrings; id++) {
209 if (!pool->whatprovides[id])
210 continue;
211
212 if (fnmatch(name, pool_id2str(pool, id), 0) == 0) {
213 Id *provides = pool->whatprovidesdata + pool->whatprovides[id];
214
215 while (*provides) {
216 for (i = 0; i < job.count; i += 2) {
217 if (job.elements[i] == SOLVER_SOLVABLE && job.elements[i + 1] == *provides)
218 break;
219 }
220
221 if (i == job.count)
222 queue_push2(&job, SOLVER_SOLVABLE, *provides);
223
b44d71c8 224 provides++;
e313dac1
MT
225 }
226 }
227 }
c605d735 228
e313dac1 229 SolvableObject *s;
c605d735
MT
230 PyObject *list = PyList_New(0);
231
e313dac1
MT
232 for (i = 0; i < job.count; i += 2) {
233 switch (job.elements[i]) {
234 case SOLVER_SOLVABLE:
235 s = PyObject_New(SolvableObject, &SolvableType);
236 s->_pool = pool;
237 s->_id = job.elements[i + 1];
c605d735 238
e313dac1
MT
239 PyList_Append(list, (PyObject *)s);
240 }
c605d735
MT
241 }
242
243 return list;
244}