]> git.ipfire.org Git - people/stevee/pakfire.git/blob - python/src/repo.c
Cleanup database and add indexes.
[people/stevee/pakfire.git] / python / src / repo.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 <errno.h>
23 #include <stdbool.h>
24 #include <solv/repo.h>
25 #include <solv/repo_solv.h>
26 #include <solv/repo_write.h>
27
28 #include "config.h"
29 #include "pool.h"
30 #include "repo.h"
31 #include "solvable.h"
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)) {
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) {
69 self->ob_type->tp_free((PyObject *)self);
70
71 Py_RETURN_NONE;
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 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
110 PyObject *Repo_get_priority(RepoObject *self) {
111 return Py_BuildValue("i", self->_repo->priority);
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
127 PyObject *Repo_write(RepoObject *self, PyObject *args) {
128 const char *filename;
129 char exception[STRING_SIZE];
130
131 if (!PyArg_ParseTuple(args, "s", &filename)) {
132 return NULL;
133 }
134
135 // Prepare the pool and internalize all attributes.
136 //_Pool_prepare(self->_repo->pool);
137
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 }
145
146 repo_write(self->_repo, fp);
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)) {
156 return NULL;
157 }
158
159 // XXX catch if file cannot be opened
160 FILE *fp = fopen(filename, "rb");
161 repo_add_solv(self->_repo, fp, 0);
162 fclose(fp);
163
164 Py_RETURN_NONE;
165 }
166
167 PyObject *Repo_internalize(RepoObject *self) {
168 repo_internalize(self->_repo);
169
170 Py_RETURN_NONE;
171 }
172
173 PyObject *Repo_clear(RepoObject *self) {
174 repo_empty(self->_repo, 1);
175
176 Py_RETURN_NONE;
177 }
178
179 PyObject *Repo_get_all(RepoObject *self) {
180 Solvable *s;
181 Id p;
182 Repo *r = self->_repo;
183
184 assert(r != NULL);
185 assert(r->pool != NULL);
186
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
196 solv->_pool = r->pool;
197 solv->_id = p;
198
199 PyList_Append(list, (PyObject *)solv);
200 Py_DECREF(solv);
201 }
202
203 return list;
204 }