]>
Commit | Line | Data |
---|---|---|
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 MT |
25 | |
26 | #include "config.h" | |
27 | #include "pool.h" | |
376eb555 | 28 | #include "relation.h" |
c605d735 MT |
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) { | |
714392de | 72 | pool_free(self->_pool); |
c605d735 | 73 | self->ob_type->tp_free((PyObject *)self); |
4069d10c MT |
74 | |
75 | Py_RETURN_NONE; | |
c605d735 MT |
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 | ||
4069d10c | 103 | Repo *r; |
c605d735 MT |
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 | ||
1f27e8fe | 116 | PyObject *_Pool_search(Pool *pool, Repo *repo, const char *match, int option, const char *keyname) { |
f1ab3110 MT |
117 | // Prepare the pool, so we can search in it. |
118 | _Pool_prepare(pool); | |
119 | ||
120 | Dataiterator d; | |
1f27e8fe MT |
121 | dataiterator_init(&d, pool, repo, 0, |
122 | keyname && pool ? pool_str2id(pool, keyname, 0) : 0, match, option); | |
f1ab3110 MT |
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); | |
f1ab3110 MT |
136 | return list; |
137 | } | |
138 | ||
c605d735 | 139 | PyObject *Pool_search(PoolObject *self, PyObject *args) { |
f1ab3110 MT |
140 | const char *match = NULL; |
141 | int option = SEARCH_SUBSTRING; | |
1f27e8fe | 142 | const char *keyname = NULL; |
f1ab3110 | 143 | |
1f27e8fe | 144 | if (!PyArg_ParseTuple(args, "s|is", &match, &option, &keyname)) { |
f1ab3110 | 145 | /* XXX raise exception */ |
714392de | 146 | return NULL; |
f1ab3110 MT |
147 | } |
148 | ||
1f27e8fe | 149 | return _Pool_search(self->_pool, NULL, match, option, keyname); |
c605d735 MT |
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) { | |
e313dac1 MT |
165 | char *name = NULL; |
166 | Queue job; | |
167 | Solvable *solvable; | |
168 | Pool *pool = self->_pool; | |
169 | Id p, pp; | |
170 | int i; | |
c605d735 | 171 | |
e313dac1 | 172 | if (!PyArg_ParseTuple(args, "s", &name)) { |
714392de | 173 | return NULL; |
c605d735 MT |
174 | } |
175 | ||
c605d735 | 176 | _Pool_prepare(pool); |
e313dac1 MT |
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 | } | |
c605d735 | 227 | |
e313dac1 | 228 | SolvableObject *s; |
c605d735 MT |
229 | PyObject *list = PyList_New(0); |
230 | ||
e313dac1 MT |
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]; | |
c605d735 | 237 | |
e313dac1 MT |
238 | PyList_Append(list, (PyObject *)s); |
239 | } | |
c605d735 MT |
240 | } |
241 | ||
242 | return list; | |
243 | } |