summaryrefslogtreecommitdiffstats
path: root/python/orderlist.cc
blob: d0ea184bd6f6e0cf3fc503067c6822625be79c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * orderlist.cc - Wrapper around pkgOrderList
 *
 * Copyright 2011 Julian Andres Klode <jak@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <Python.h>
#include "apt_pkgmodule.h"
#include "generic.h"
#include <apt-pkg/orderlist.h>

struct PyOrderList : CppPyObject<pkgOrderList*> {
    pkgCache::PkgIterator current;
    int nextIndex;
};

static PyObject *order_list_new(PyTypeObject *type,PyObject *args,
                                  PyObject *kwds)
{
    PyObject *pyDepCache = NULL;
    char *kwlist[] = {"depcache", NULL};
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist,
                                    &PyDepCache_Type, &pyDepCache)
                                    == 0)
        return 0;

    pkgDepCache *depCache = PyDepCache_ToCpp(pyDepCache);
    return PyOrderList_FromCpp(new pkgOrderList(depCache), true, 
                               pyDepCache);
}

static const char order_list_append_doc[] =
    "append(pkg: Package)\n\n"
    "Append a package to the end of the list.";
static PyObject *order_list_append(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *pyPackage = NULL;
    if (PyArg_ParseTuple(args, "O!", &PyPackage_Type, &pyPackage) == 0)
        return 0;

    list->push_back(PyPackage_ToCpp(pyPackage));
    Py_RETURN_NONE;
}

static const char order_list_score_doc[] =
    "score(pkg: Package) -> int\n\n"
    "Return the score of the package.";
static PyObject *order_list_score(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *pyPackage = NULL;
    if (PyArg_ParseTuple(args, "O!", &PyPackage_Type, &pyPackage) == 0)
        return 0;

    return MkPyNumber(list->Score(PyPackage_ToCpp(pyPackage)));
}

static const char order_list_order_critical_doc[] =
    "order_critical()\n\n"
    "Order by PreDepends only (critical unpack order).";
static PyObject *order_list_order_critical(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    if (PyArg_ParseTuple(args, "") == 0)
        return 0;

    list->OrderCritical();
    
    Py_INCREF(Py_None);
    return HandleErrors(Py_None);
}

static const char order_list_order_unpack_doc[] =
    "order_unpack()\n\n"
    "Order the packages for unpacking (see Debian Policy).";
static PyObject *order_list_order_unpack(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    if (PyArg_ParseTuple(args, "") == 0)
        return 0;

    list->OrderUnpack();
    Py_INCREF(Py_None);
    return HandleErrors(Py_None);
}

static const char order_list_order_configure_doc[] =
    "order_configure()\n\n"
    "Order the packages for configuration (see Debian Policy).";
static PyObject *order_list_order_configure(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    if (PyArg_ParseTuple(args, "") == 0)
        return 0;

    list->OrderConfigure();
    
    Py_INCREF(Py_None);
    return HandleErrors(Py_None);
}

static bool valid_flags(unsigned int flags) {
	return (flags & ~pkgOrderList::Added
	              & ~pkgOrderList::AddPending
	              & ~pkgOrderList::Immediate
	              & ~pkgOrderList::Loop
	              & ~pkgOrderList::UnPacked
	              & ~pkgOrderList::Configured
	              & ~pkgOrderList::Removed
	              & ~pkgOrderList::InList
	              & ~pkgOrderList::After
	              & ~pkgOrderList::States) == 0;
}

static const char order_list_flag_doc[] =
    "flag(pkg: Package, flag: int[, unset_flags: int])\n\n"
    "Flag the package, set flags in 'flag' and remove flags in\n"
    "'unset_flags'.";
static PyObject *order_list_flag(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    
    PyObject *pyPkg = NULL;
    unsigned int flags = 0;
    unsigned int unset_flags = 0;
    if (PyArg_ParseTuple(args, "O!I|I", &PyPackage_Type, &pyPkg,
                         &flags, &unset_flags) == 0)
        return 0;

    if (!valid_flags(flags))
       return PyErr_Format(PyExc_ValueError, "flags (%u) is"
                           " not a valid combination of flags.",
                           flags);
    if (!valid_flags(unset_flags))
       return PyErr_Format(PyExc_ValueError, "unset_flags (%u) is"
                           " not a valid combination of flags.",
                           unset_flags);

    list->Flag(PyPackage_ToCpp(pyPkg), flags, unset_flags);
    
    Py_RETURN_NONE;
}

static const char order_list_is_flag_doc[] =
    "is_flag(pkg: Package, flag: int)\n\n"
    "Check if the flag(s) are set.";
static PyObject *order_list_is_flag(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *pyPkg = NULL;
    unsigned int flags = 0;
    if (PyArg_ParseTuple(args, "O!I", &PyPackage_Type, &pyPkg,
                         &flags) == 0)
        return 0;

    if (!valid_flags(flags))
       return PyErr_Format(PyExc_ValueError, "flags (%u) is"
                           " not a valid combination of flags.",
                           flags);

    return PyBool_FromLong(list->IsFlag(PyPackage_ToCpp(pyPkg), flags));
}

static const char order_list_wipe_flags_doc[] =
    "wipe_flags(flags: int)\n\n"
    "Remove the flags in 'flags' from all packages in this list";
static PyObject *order_list_wipe_flags(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    unsigned int flags = 0;
    if (PyArg_ParseTuple(args, "I", &flags) == 0)
        return 0;

    if (!valid_flags(flags))
       return PyErr_Format(PyExc_ValueError, "flags (%u) is"
                           " not a valid combination of flags.",
                           flags);

    list->WipeFlags(flags);
    Py_RETURN_NONE;
}

static const char order_list_is_now_doc[] =
    "is_now(pkg: Package)\n\n"
    "Check if the package is flagged for any state but removal.";
static PyObject *order_list_is_now(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *pyPkg = NULL;
    if (PyArg_ParseTuple(args, "O!", &PyPackage_Type, &pyPkg) == 0)
        return 0;

    return PyBool_FromLong(list->IsNow(PyPackage_ToCpp(pyPkg)));
}

static const char order_list_is_missing_doc[] =
    "is_now(pkg: Package)\n\n"
    "Check if the package is marked for install.";
static PyObject *order_list_is_missing(PyObject *self,PyObject *args)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *pyPkg = NULL;
    if (PyArg_ParseTuple(args, "O!", &PyPackage_Type, &pyPkg) == 0)
        return 0;

    return PyBool_FromLong(list->IsMissing(PyPackage_ToCpp(pyPkg)));
}


#define METHOD(name) {#name, order_list_##name, METH_VARARGS,\
                      order_list_##name##_doc}

static PyMethodDef order_list_methods[] = {
	METHOD(append),
	METHOD(score),
	METHOD(order_critical),
	METHOD(order_unpack),
	METHOD(order_configure),
	METHOD(flag),
	METHOD(is_flag),
	METHOD(is_now),
	METHOD(is_missing),
	METHOD(wipe_flags),
	{}
};

static PyObject *order_list_seq_item(PyObject *self,Py_ssize_t index)
{
    pkgOrderList *list = GetCpp<pkgOrderList*>(self);
    PyObject *owner = GetOwner<pkgOrderList*>(self);
    PyObject *pycache = GetOwner<pkgOrderList*>(owner);
    pkgCache *cache = PyCache_ToCpp(pycache);

    if (index < 0 || index >= list->size())
        return PyErr_Format(PyExc_IndexError, "Out of range: %zd", index);

    return PyPackage_FromCpp(pkgCache::PkgIterator(*cache,
                                *(list->begin() + index)),
                             true, owner);
}

Py_ssize_t order_list_seq_length(PyObject *self)
{
    return GetCpp<pkgOrderList*>(self)->size();
}

static PySequenceMethods order_list_as_sequence =
{
   order_list_seq_length, // sq_length
   0,                     // sq_concat
   0,                     // sq_repeat
   order_list_seq_item,   // sq_item
   0,                     // sq_ass_item
   0,                     // sq_contains
   0,                     // sq_inplace_concat
   0                      // sq_inplace_repeat
};

static const char order_list_doc[] = "OrderList(depcache: DepCache)\n\n"
    "Sequence type for packages with special ordering methods.";
PyTypeObject PyOrderList_Type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "apt_pkg.OrderList",                 // tp_name
    sizeof(CppPyObject<pkgOrderList*>),  // tp_basicsize
    0,                                   // tp_itemsize
    // Methods
    CppDeallocPtr<pkgOrderList*>,        // tp_dealloc
    0,                                   // tp_print
    0,                                   // tp_getattr
    0,                                   // tp_setattr
    0,                                   // tp_compare
    0,                                   // tp_repr
    0,                                   // tp_as_number
    &order_list_as_sequence,             // tp_as_sequence
    0,                                   // tp_as_mapping
    0,                                   // tp_hash
    0,                                   // tp_call
    0,                                   // tp_str
    0,                                   // tp_getattro
    0,                                   // tp_setattro
    0,                                   // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                  // tp_flags
    order_list_doc,                      // tp_doc
    0,                                   // tp_traverse
    0,                                   // tp_clear
    0,                                   // tp_richcompare
    0,                                   // tp_weaklistoffset
    0,                                   // tp_iter
    0,                                   // tp_iternext
    order_list_methods,                  // tp_methods
    0,                                   // tp_members
    0,                                   // tp_getset
    0,                                   // tp_base
    0,                                   // tp_dict
    0,                                   // tp_descr_get
    0,                                   // tp_descr_set
    0,                                   // tp_dictoffset
    0,                                   // tp_init
    0,                                   // tp_alloc
    order_list_new,                      // tp_new
};