]>
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 | |
8276111d | 21 | #include <Python.h> |
51b452f0 | 22 | #include <errno.h> |
c605d735 | 23 | #include <stdbool.h> |
45f5a3d9 MT |
24 | #include <solv/repo.h> |
25 | #include <solv/repo_solv.h> | |
26 | #include <solv/repo_write.h> | |
c605d735 | 27 | |
51b452f0 | 28 | #include "config.h" |
c605d735 MT |
29 | #include "pool.h" |
30 | #include "repo.h" | |
8276111d | 31 | #include "solvable.h" |
c605d735 MT |
32 | |
33 | PyTypeObject RepoType = { | |
34 | PyObject_HEAD_INIT(NULL) | |
35 | tp_name: "_pakfire.Repo", | |
36 | tp_basicsize: sizeof(RepoObject), | |
37 | tp_flags: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | |
38 | tp_new : Repo_new, | |
39 | tp_dealloc: (destructor) Repo_dealloc, | |
40 | tp_doc: "Sat Repo objects", | |
41 | }; | |
42 | ||
43 | PyObject* Repo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { | |
44 | RepoObject *self; | |
45 | ||
46 | PoolObject *pool; | |
47 | const char *name; | |
48 | ||
49 | if (!PyArg_ParseTuple(args, "Os", &pool, &name)) { | |
c605d735 MT |
50 | return NULL; |
51 | } | |
52 | ||
53 | assert(pool); | |
54 | assert(name); | |
55 | ||
56 | self = (RepoObject *)type->tp_alloc(type, 0); | |
57 | if (self != NULL) { | |
58 | self->_repo = repo_create(pool->_pool, name); | |
59 | if (self->_repo == NULL) { | |
60 | Py_DECREF(self); | |
61 | return NULL; | |
62 | } | |
63 | } | |
64 | ||
65 | return (PyObject *)self; | |
66 | } | |
67 | ||
68 | PyObject *Repo_dealloc(RepoObject *self) { | |
c605d735 | 69 | self->ob_type->tp_free((PyObject *)self); |
4069d10c MT |
70 | |
71 | Py_RETURN_NONE; | |
c605d735 MT |
72 | } |
73 | ||
74 | PyObject *Repo_name(RepoObject *self) { | |
75 | Repo *repo = self->_repo; | |
76 | ||
77 | return Py_BuildValue("s", repo->name); | |
78 | } | |
79 | ||
80 | PyObject *Repo_size(RepoObject *self) { | |
81 | Repo *repo = self->_repo; | |
82 | ||
83 | return Py_BuildValue("i", repo->nsolvables); | |
84 | } | |
85 | ||
86 | PyObject *Repo_get_enabled(RepoObject *self) { | |
87 | if (self->_repo->disabled == 0) { | |
88 | Py_RETURN_TRUE; | |
89 | } | |
90 | ||
91 | Py_RETURN_FALSE; | |
92 | } | |
93 | ||
94 | PyObject *Repo_set_enabled(RepoObject *self, PyObject *args) { | |
95 | bool enabled; | |
96 | ||
97 | if (!PyArg_ParseTuple(args, "b", &enabled)) { | |
c605d735 MT |
98 | return NULL; |
99 | } | |
100 | ||
101 | if (enabled == true) { | |
102 | self->_repo->disabled = 0; | |
103 | } else { | |
104 | self->_repo->disabled = 1; | |
105 | } | |
106 | ||
107 | Py_RETURN_NONE; | |
108 | } | |
109 | ||
a7cd7a38 | 110 | PyObject *Repo_get_priority(RepoObject *self) { |
4069d10c | 111 | return Py_BuildValue("i", self->_repo->priority); |
a7cd7a38 MT |
112 | } |
113 | ||
114 | PyObject *Repo_set_priority(RepoObject *self, PyObject *args) { | |
115 | int priority; | |
116 | ||
117 | if (!PyArg_ParseTuple(args, "i", &priority)) { | |
118 | /* XXX raise exception */ | |
119 | return NULL; | |
120 | } | |
121 | ||
122 | self->_repo->priority = priority; | |
123 | ||
124 | Py_RETURN_NONE; | |
125 | } | |
126 | ||
c605d735 MT |
127 | PyObject *Repo_write(RepoObject *self, PyObject *args) { |
128 | const char *filename; | |
51b452f0 | 129 | char exception[STRING_SIZE]; |
c605d735 MT |
130 | |
131 | if (!PyArg_ParseTuple(args, "s", &filename)) { | |
9b68f47c | 132 | return NULL; |
c605d735 MT |
133 | } |
134 | ||
135 | // Prepare the pool and internalize all attributes. | |
9b68f47c | 136 | //_Pool_prepare(self->_repo->pool); |
c605d735 | 137 | |
51b452f0 MT |
138 | FILE *fp = NULL; |
139 | if ((fp = fopen(filename, "wb")) == NULL) { | |
140 | snprintf(exception, STRING_SIZE - 1, "Could not open file for writing: %s (%s).", | |
141 | filename, strerror(errno)); | |
142 | PyErr_SetString(PyExc_RuntimeError, exception); | |
143 | return NULL; | |
144 | } | |
c605d735 | 145 | |
9b68f47c | 146 | repo_write(self->_repo, fp); |
c605d735 MT |
147 | fclose(fp); |
148 | ||
149 | Py_RETURN_NONE; | |
150 | } | |
151 | ||
152 | PyObject *Repo_read(RepoObject *self, PyObject *args) { | |
153 | const char *filename; | |
154 | ||
155 | if (!PyArg_ParseTuple(args, "s", &filename)) { | |
9b68f47c | 156 | return NULL; |
c605d735 MT |
157 | } |
158 | ||
159 | // XXX catch if file cannot be opened | |
160 | FILE *fp = fopen(filename, "rb"); | |
9b68f47c | 161 | repo_add_solv(self->_repo, fp, 0); |
c605d735 MT |
162 | fclose(fp); |
163 | ||
164 | Py_RETURN_NONE; | |
165 | } | |
31267a64 | 166 | |
d8cfced9 MT |
167 | PyObject *Repo_internalize(RepoObject *self) { |
168 | repo_internalize(self->_repo); | |
169 | ||
170 | Py_RETURN_NONE; | |
171 | } | |
172 | ||
31267a64 MT |
173 | PyObject *Repo_clear(RepoObject *self) { |
174 | repo_empty(self->_repo, 1); | |
175 | ||
176 | Py_RETURN_NONE; | |
177 | } | |
8276111d MT |
178 | |
179 | PyObject *Repo_get_all(RepoObject *self) { | |
180 | Solvable *s; | |
181 | Id p; | |
182 | Repo *r = self->_repo; | |
183 | ||
cbe0b87a MT |
184 | assert(r != NULL); |
185 | assert(r->pool != NULL); | |
186 | ||
8276111d MT |
187 | PyObject *list = PyList_New(0); |
188 | ||
189 | FOR_REPO_SOLVABLES(r, p, s) { | |
190 | SolvableObject *solv; | |
191 | ||
192 | solv = PyObject_New(SolvableObject, &SolvableType); | |
193 | if (solv == NULL) | |
194 | return NULL; | |
195 | ||
cbe0b87a | 196 | solv->_pool = r->pool; |
8276111d MT |
197 | solv->_id = p; |
198 | ||
199 | PyList_Append(list, (PyObject *)solv); | |
cbe0b87a | 200 | Py_DECREF(solv); |
8276111d MT |
201 | } |
202 | ||
8276111d MT |
203 | return list; |
204 | } |