]> git.ipfire.org Git - people/ms/pakfire.git/blob - src/repo.c
Move from libsatsolver to libsolv.
[people/ms/pakfire.git] / src / repo.c
1
2 #include <Python.h>
3 #include <stdbool.h>
4 #include <solv/repo.h>
5 #include <solv/repo_solv.h>
6 #include <solv/repo_write.h>
7
8 #include "pool.h"
9 #include "repo.h"
10 #include "solvable.h"
11
12 PyTypeObject RepoType = {
13 PyObject_HEAD_INIT(NULL)
14 tp_name: "_pakfire.Repo",
15 tp_basicsize: sizeof(RepoObject),
16 tp_flags: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
17 tp_new : Repo_new,
18 tp_dealloc: (destructor) Repo_dealloc,
19 tp_doc: "Sat Repo objects",
20 };
21
22 PyObject* Repo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
23 RepoObject *self;
24
25 PoolObject *pool;
26 const char *name;
27
28 if (!PyArg_ParseTuple(args, "Os", &pool, &name)) {
29 /* XXX raise exception */
30 return NULL;
31 }
32
33 assert(pool);
34 assert(name);
35
36 self = (RepoObject *)type->tp_alloc(type, 0);
37 if (self != NULL) {
38 self->_repo = repo_create(pool->_pool, name);
39 if (self->_repo == NULL) {
40 Py_DECREF(self);
41 return NULL;
42 }
43 }
44
45 return (PyObject *)self;
46 }
47
48 PyObject *Repo_dealloc(RepoObject *self) {
49 // repo_free(self->_repo, 0);
50 self->ob_type->tp_free((PyObject *)self);
51
52 Py_RETURN_NONE;
53 }
54
55 PyObject *Repo_name(RepoObject *self) {
56 Repo *repo = self->_repo;
57
58 return Py_BuildValue("s", repo->name);
59 }
60
61 PyObject *Repo_size(RepoObject *self) {
62 Repo *repo = self->_repo;
63
64 return Py_BuildValue("i", repo->nsolvables);
65 }
66
67 PyObject *Repo_get_enabled(RepoObject *self) {
68 if (self->_repo->disabled == 0) {
69 Py_RETURN_TRUE;
70 }
71
72 Py_RETURN_FALSE;
73 }
74
75 PyObject *Repo_set_enabled(RepoObject *self, PyObject *args) {
76 bool enabled;
77
78 if (!PyArg_ParseTuple(args, "b", &enabled)) {
79 /* XXX raise exception */
80 return NULL;
81 }
82
83 if (enabled == true) {
84 self->_repo->disabled = 0;
85 } else {
86 self->_repo->disabled = 1;
87 }
88
89 Py_RETURN_NONE;
90 }
91
92 PyObject *Repo_get_priority(RepoObject *self) {
93 return Py_BuildValue("i", self->_repo->priority);
94 }
95
96 PyObject *Repo_set_priority(RepoObject *self, PyObject *args) {
97 int priority;
98
99 if (!PyArg_ParseTuple(args, "i", &priority)) {
100 /* XXX raise exception */
101 return NULL;
102 }
103
104 self->_repo->priority = priority;
105
106 Py_RETURN_NONE;
107 }
108
109 PyObject *Repo_write(RepoObject *self, PyObject *args) {
110 const char *filename;
111
112 if (!PyArg_ParseTuple(args, "s", &filename)) {
113 /* XXX raise exception */
114 }
115
116 // Prepare the pool and internalize all attributes.
117 _Pool_prepare(self->_repo->pool);
118
119 // XXX catch if file cannot be opened
120 FILE *fp = fopen(filename, "wb");
121
122 repo_write(self->_repo, fp, NULL, NULL, 0);
123
124 fclose(fp);
125
126 Py_RETURN_NONE;
127 }
128
129 PyObject *Repo_read(RepoObject *self, PyObject *args) {
130 const char *filename;
131
132 if (!PyArg_ParseTuple(args, "s", &filename)) {
133 /* XXX raise exception */
134 }
135
136 // XXX catch if file cannot be opened
137 FILE *fp = fopen(filename, "rb");
138
139 repo_add_solv(self->_repo, fp);
140
141 fclose(fp);
142
143 Py_RETURN_NONE;
144 }
145
146 PyObject *Repo_clear(RepoObject *self) {
147 repo_empty(self->_repo, 1);
148
149 Py_RETURN_NONE;
150 }
151
152 PyObject *Repo_get_all(RepoObject *self) {
153 Solvable *s;
154 Id p;
155 Repo *r = self->_repo;
156
157 PyObject *list = PyList_New(0);
158
159 FOR_REPO_SOLVABLES(r, p, s) {
160 SolvableObject *solv;
161
162 solv = PyObject_New(SolvableObject, &SolvableType);
163 if (solv == NULL)
164 return NULL;
165
166 solv->_pool = self->_repo->pool;
167 solv->_id = p;
168
169 PyList_Append(list, (PyObject *)solv);
170 }
171
172 Py_INCREF(list);
173 return list;
174 }