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