]> git.ipfire.org Git - people/stevee/pakfire.git/blob - python/src/pool.c
Cleanup database and add indexes.
[people/stevee/pakfire.git] / python / src / pool.c
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 #############################################################################*/
20
21 #include <Python.h>
22 #include <fnmatch.h>
23 #include <solv/poolarch.h>
24 #include <solv/solver.h>
25
26 #include "config.h"
27 #include "pool.h"
28 #include "relation.h"
29 #include "repo.h"
30 #include "solvable.h"
31
32 PyTypeObject 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
43 PyObject* 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
61 pool_setarch(self->_pool, arch);
62 if (self->_pool == NULL) {
63 Py_DECREF(self);
64 return NULL;
65 }
66 }
67
68 return (PyObject *)self;
69 }
70
71 PyObject *Pool_dealloc(PoolObject *self) {
72 pool_free(self->_pool);
73 self->ob_type->tp_free((PyObject *)self);
74
75 Py_RETURN_NONE;
76 }
77
78 PyObject *Pool_add_repo(PoolObject *self, PyObject *args) {
79 const char *name;
80 if (!PyArg_ParseTuple(args, "s", &name)) {
81 /* XXX raise exception */
82 }
83
84 RepoObject *repo;
85
86 repo = PyObject_New(RepoObject, &RepoType);
87 if (repo == NULL)
88 return NULL;
89
90 return (PyObject *)repo;
91 }
92
93 PyObject *Pool_prepare(PoolObject *self) {
94 _Pool_prepare(self->_pool);
95
96 Py_RETURN_NONE;
97 }
98
99 void _Pool_prepare(Pool *pool) {
100 pool_addfileprovides(pool);
101 pool_createwhatprovides(pool);
102
103 Repo *r;
104 int idx;
105 FOR_REPOS(idx, r) {
106 repo_internalize(r);
107 }
108 }
109
110 PyObject *Pool_size(PoolObject *self) {
111 Pool *pool = self->_pool;
112
113 return Py_BuildValue("i", pool->nsolvables);
114 }
115
116 PyObject *_Pool_search(Pool *pool, Repo *repo, const char *match, int option, const char *keyname) {
117 // Prepare the pool, so we can search in it.
118 _Pool_prepare(pool);
119
120 Dataiterator d;
121 dataiterator_init(&d, pool, repo, 0,
122 keyname && pool ? pool_str2id(pool, keyname, 0) : 0, match, option);
123
124 PyObject *list = PyList_New(0);
125
126 SolvableObject *solvable;
127 while (dataiterator_step(&d)) {
128 solvable = PyObject_New(SolvableObject, &SolvableType);
129 solvable->_pool = pool;
130 solvable->_id = d.solvid;
131
132 PyList_Append(list, (PyObject *)solvable);
133 }
134
135 dataiterator_free(&d);
136 return list;
137 }
138
139 PyObject *Pool_search(PoolObject *self, PyObject *args) {
140 const char *match = NULL;
141 int option = SEARCH_SUBSTRING;
142 const char *keyname = NULL;
143
144 if (!PyArg_ParseTuple(args, "s|is", &match, &option, &keyname)) {
145 /* XXX raise exception */
146 return NULL;
147 }
148
149 return _Pool_search(self->_pool, NULL, match, option, keyname);
150 }
151
152 PyObject *Pool_set_installed(PoolObject *self, PyObject *args) {
153 RepoObject *repo;
154
155 if (!PyArg_ParseTuple(args, "O", &repo)) {
156 /* XXX raise exception */
157 }
158
159 pool_set_installed(self->_pool, repo->_repo);
160
161 Py_RETURN_NONE;
162 }
163
164 PyObject *Pool_providers(PoolObject *self, PyObject *args) {
165 char *name = NULL;
166 Queue job;
167 Solvable *solvable;
168 Pool *pool = self->_pool;
169 Id p, pp;
170 int i;
171
172 if (!PyArg_ParseTuple(args, "s", &name)) {
173 return NULL;
174 }
175
176 _Pool_prepare(pool);
177 queue_init(&job);
178
179 Id id = pool_str2id(pool, name, 0);
180
181 if (id) {
182 FOR_PROVIDES(p, pp, id) {
183 solvable = pool->solvables + p;
184
185 if (solvable->name == id)
186 queue_push2(&job, SOLVER_SOLVABLE, p);
187 }
188 }
189
190 for (p = 1; p < pool->nsolvables; p++) {
191 solvable = pool->solvables + p;
192 if (!solvable->repo || !pool_installable(pool, solvable))
193 continue;
194
195 id = solvable->name;
196 if (fnmatch(name, pool_id2str(pool, id), 0) == 0) {
197 for (i = 0; i < job.count; i += 2) {
198 if (job.elements[i] == SOLVER_SOLVABLE && job.elements[i + 1] == id)
199 break;
200 }
201
202 if (i == job.count)
203 queue_push2(&job, SOLVER_SOLVABLE, p);
204 }
205 }
206
207 for (id = 1; id < pool->ss.nstrings; id++) {
208 if (!pool->whatprovides[id])
209 continue;
210
211 if (fnmatch(name, pool_id2str(pool, id), 0) == 0) {
212 Id *provides = pool->whatprovidesdata + pool->whatprovides[id];
213
214 while (*provides) {
215 for (i = 0; i < job.count; i += 2) {
216 if (job.elements[i] == SOLVER_SOLVABLE && job.elements[i + 1] == *provides)
217 break;
218 }
219
220 if (i == job.count)
221 queue_push2(&job, SOLVER_SOLVABLE, *provides);
222
223 *provides++;
224 }
225 }
226 }
227
228 SolvableObject *s;
229 PyObject *list = PyList_New(0);
230
231 for (i = 0; i < job.count; i += 2) {
232 switch (job.elements[i]) {
233 case SOLVER_SOLVABLE:
234 s = PyObject_New(SolvableObject, &SolvableType);
235 s->_pool = pool;
236 s->_id = job.elements[i + 1];
237
238 PyList_Append(list, (PyObject *)s);
239 }
240 }
241
242 return list;
243 }