]> git.ipfire.org Git - pakfire.git/blob - python/src/problem.c
Cleanup database and add indexes.
[pakfire.git] / python / src / problem.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
23 #include "config.h"
24 #include "problem.h"
25 #include "relation.h"
26 #include "request.h"
27 #include "solution.h"
28 #include "solvable.h"
29 #include "solver.h"
30
31 PyTypeObject ProblemType = {
32 PyObject_HEAD_INIT(NULL)
33 tp_name: "_pakfire.Problem",
34 tp_basicsize: sizeof(ProblemObject),
35 tp_flags: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
36 tp_new : Problem_new,
37 tp_dealloc: (destructor) Problem_dealloc,
38 tp_doc: "Sat Problem objects",
39 tp_str: (reprfunc)Problem_string,
40 };
41
42 PyObject* Problem_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
43 ProblemObject *self;
44 SolverObject *solver;
45 RequestObject *request;
46 Id problem_id;
47
48 if (!PyArg_ParseTuple(args, "OOi", &solver, &request, &problem_id)) {
49 /* XXX raise exception */
50 return NULL;
51 }
52
53 self = (ProblemObject *)type->tp_alloc(type, 0);
54 if (self != NULL) {
55 self->_pool = request->_pool;
56 self->_solver = solver->_solver;
57 self->_id = problem_id;
58
59 // Initialize problem information.
60 Problem_init(self);
61 }
62
63 return (PyObject *)self;
64 }
65
66 PyObject *Problem_dealloc(ProblemObject *self) {
67 self->ob_type->tp_free((PyObject *)self);
68
69 Py_RETURN_NONE;
70 }
71
72 PyObject *Problem_init(ProblemObject *self) {
73 Id id = solver_findproblemrule(self->_solver, self->_id);
74
75 self->rule = solver_ruleinfo(self->_solver, id, &self->source,
76 &self->target, &self->dep);
77
78 Py_RETURN_NONE;
79 }
80
81 PyObject *Problem_get_rule(ProblemObject *self) {
82 return Py_BuildValue("i", self->rule);
83 }
84
85 PyObject *Problem_get_source(ProblemObject *self) {
86 SolvableObject *solvable;
87
88 if (self->source == ID_NULL)
89 Py_RETURN_NONE;
90
91 solvable = PyObject_New(SolvableObject, &SolvableType);
92 if (solvable == NULL)
93 return NULL;
94
95 solvable->_pool = self->_pool;
96 solvable->_id = self->source;
97
98 return (PyObject *)solvable;
99 }
100
101 PyObject *Problem_get_target(ProblemObject *self) {
102 SolvableObject *solvable;
103
104 if (self->target == ID_NULL)
105 Py_RETURN_NONE;
106
107 solvable = PyObject_New(SolvableObject, &SolvableType);
108 if (solvable == NULL)
109 return NULL;
110
111 solvable->_pool = self->_pool;
112 solvable->_id = self->target;
113
114 return (PyObject *)solvable;
115 }
116
117 PyObject *Problem_get_dep(ProblemObject *self) {
118 RelationObject *relation;
119
120 if (self->dep == ID_NULL)
121 Py_RETURN_NONE;
122
123 relation = PyObject_New(RelationObject, &RelationType);
124 if (relation == NULL)
125 return NULL;
126
127 relation->_pool = self->_pool;
128 relation->_id = self->dep;
129
130 return (PyObject *)relation;
131 }
132
133 PyObject *Problem_get_solutions(ProblemObject *self) {
134 SolutionObject *solution;
135 Id s = 0;
136
137 PyObject *list = PyList_New(0);
138
139 while ((s = solver_next_solution(self->_solver, self->_id, s)) != 0) {
140 solution = PyObject_New(SolutionObject, &SolutionType);
141
142 solution->_solver = self->_solver;
143 solution->problem_id = self->_id;
144 solution->id = s;
145
146 PyList_Append(list, (PyObject *)solution);
147 }
148
149 return list;
150 }
151
152 PyObject *Problem_string(ProblemObject *self) {
153 Pool *pool = self->_pool;
154 char s[STRING_SIZE];
155
156 switch (self->rule) {
157 case SOLVER_RULE_DISTUPGRADE:
158 snprintf(s, STRING_SIZE - 1,
159 _("%s does not belong to a distupgrade repository"),
160 pool_solvid2str(pool, self->source)
161 );
162 break;
163
164 case SOLVER_RULE_INFARCH:
165 snprintf(s, STRING_SIZE - 1,
166 _("%s has inferior architecture"),
167 pool_solvid2str(pool, self->source)
168 );
169 break;
170
171 case SOLVER_RULE_UPDATE:
172 snprintf(s, STRING_SIZE - 1,
173 _("problem with installed package %s"),
174 pool_solvid2str(pool, self->source)
175 );
176 break;
177
178 case SOLVER_RULE_JOB:
179 snprintf(s, STRING_SIZE - 1, _("conflicting requests"));
180 break;
181
182 case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
183 snprintf(s, STRING_SIZE - 1,
184 _("nothing provides requested %s"),
185 pool_dep2str(pool, self->dep)
186 );
187 break;
188
189 case SOLVER_RULE_RPM:
190 snprintf(s, STRING_SIZE - 1, _("some dependency problem"));
191 break;
192
193 case SOLVER_RULE_RPM_NOT_INSTALLABLE:
194 snprintf(s, STRING_SIZE - 1,
195 _("package %s is not installable"),
196 pool_solvid2str(pool, self->source)
197 );
198 break;
199
200 case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
201 snprintf(s, STRING_SIZE - 1,
202 _("nothing provides %s needed by %s"),
203 pool_dep2str(pool, self->dep), pool_solvid2str(pool, self->source)
204 );
205 break;
206
207 case SOLVER_RULE_RPM_SAME_NAME:
208 snprintf(s, STRING_SIZE - 1,
209 _("cannot install both %s and %s"),
210 pool_solvid2str(pool, self->source), pool_solvid2str(pool, self->target)
211 );
212 break;
213
214 case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
215 snprintf(s, STRING_SIZE - 1,
216 _("package %s conflicts with %s provided by %s"),
217 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep),
218 pool_solvid2str(pool, self->target)
219 );
220 break;
221
222 case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
223 snprintf(s, STRING_SIZE - 1,
224 _("package %s obsoletes %s provided by %s"),
225 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep),
226 pool_solvid2str(pool, self->target)
227 );
228 break;
229
230 case SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES:
231 snprintf(s, STRING_SIZE - 1,
232 _("installed package %s obsoletes %s provided by %s"),
233 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep),
234 pool_solvid2str(pool, self->target)
235 );
236 break;
237
238 case SOLVER_RULE_RPM_IMPLICIT_OBSOLETES:
239 snprintf(s, STRING_SIZE - 1,
240 _("package %s implicitely obsoletes %s provided by %s"),
241 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep),
242 pool_solvid2str(pool, self->target)
243 );
244 break;
245
246 case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
247 snprintf(s, STRING_SIZE - 1,
248 _("package %s requires %s, but none of the providers can be installed"),
249 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep)
250 );
251 break;
252
253 case SOLVER_RULE_RPM_SELF_CONFLICT:
254 snprintf(s, STRING_SIZE - 1,
255 _("package %s conflicts with %s provided by itself"),
256 pool_solvid2str(pool, self->source), pool_dep2str(pool, self->dep)
257 );
258 break;
259
260 case SOLVER_RULE_UNKNOWN:
261 case SOLVER_RULE_FEATURE:
262 case SOLVER_RULE_LEARNT:
263 case SOLVER_RULE_CHOICE:
264 snprintf(s, STRING_SIZE - 1, _("bad rule type"));
265 break;
266
267 }
268
269 return Py_BuildValue("s", &s);
270 }