summaryrefslogtreecommitdiff
path: root/python/acquire-item.cc
blob: d5f9ad10d078c8e6f3ff0dccd1de71100066c95f (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
318
/*
 * acquire-item.cc - Wrapper around pkgAcquire::Item and pkgAcqFile.
 *
 * Copyright 2004-2009 Canonical Ltd.
 * Copyright 2009 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 "generic.h"
#include "apt_pkgmodule.h"

#include <apt-pkg/acquire-item.h>
#include <map>

using namespace std;

inline pkgAcquire::Item *acquireitem_tocpp(PyObject *self)
{
    pkgAcquire::Item *itm = GetCpp<pkgAcquire::Item*>(self);
    if (itm == 0)
        PyErr_SetString(PyExc_ValueError, "Acquire() has been shut down or "
                        "the AcquireFile() object has been deallocated.");
    return itm;
}

static PyObject *acquireitem_get_complete(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? PyBool_FromLong(item->Complete) : 0;
}

static PyObject *acquireitem_get_desc_uri(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? CppPyString(item->DescURI()) : 0;
}

static PyObject *acquireitem_get_destfile(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? CppPyString(item->DestFile) : 0;
}


static PyObject *acquireitem_get_error_text(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? CppPyString(item->ErrorText) : 0;
}

static PyObject *acquireitem_get_filesize(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? Py_BuildValue("i", item->FileSize) : 0;
}

static PyObject *acquireitem_get_id(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? Py_BuildValue("k", item->ID) : 0;
}

static PyObject *acquireitem_get_mode(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? Py_BuildValue("s", item->Mode) : 0;
}

static PyObject *acquireitem_get_is_trusted(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? PyBool_FromLong(item->IsTrusted()) : 0;
}

static PyObject *acquireitem_get_local(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? PyBool_FromLong(item->Local) : 0;
}

static PyObject *acquireitem_get_status(PyObject *self, void *closure)
{
    pkgAcquire::Item *item = acquireitem_tocpp(self);
    return item ? Py_BuildValue("i", item->Status) : 0;
}

static int acquireitem_set_id(PyObject *self, PyObject *value, void *closure)
{
    pkgAcquire::Item *Itm = acquireitem_tocpp(self);
    if (Itm == 0)
        return -1;
    if (PyLong_Check(value)) {
        Itm->ID = PyLong_AsLong(value);
    }
    else if (PyInt_Check(value)) {
        Itm->ID = PyInt_AsLong(value);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "value must be integer.");
        return -1;
    }
    return 0;
}


static PyGetSetDef acquireitem_getset[] = {
    {"complete",acquireitem_get_complete},
    {"desc_uri",acquireitem_get_desc_uri},
    {"destfile",acquireitem_get_destfile},
    {"error_text",acquireitem_get_error_text},
    {"filesize",acquireitem_get_filesize},
    {"id",acquireitem_get_id,acquireitem_set_id},
    {"mode",acquireitem_get_mode},
    {"is_trusted",acquireitem_get_is_trusted},
    {"local",acquireitem_get_local},
    {"status",acquireitem_get_status},
#ifdef COMPAT_0_7
    {"Complete",acquireitem_get_complete},
    {"DescURI",acquireitem_get_desc_uri},
    {"DestFile",acquireitem_get_destfile},
    {"ErrorText",acquireitem_get_error_text},
    {"FileSize",acquireitem_get_filesize},
    {"ID",acquireitem_get_id},
    {"IsTrusted",acquireitem_get_is_trusted},
    {"Local",acquireitem_get_local},
    {"Status",acquireitem_get_status},
#endif
    {}
};

static PyObject *acquireitem_repr(PyObject *Self)
{
    pkgAcquire::Item *Itm = acquireitem_tocpp(Self);
    if (Itm == 0)
        return 0;
    return PyString_FromFormat("<%s object: "
                               "Status: %i Complete: %i Local: %i IsTrusted: %i "
                               "FileSize: %lu DestFile:'%s' "
                               "DescURI: '%s' ID:%lu ErrorText: '%s'>",
                               Self->ob_type->tp_name,
                               Itm->Status, Itm->Complete, Itm->Local, Itm->IsTrusted(),
                               Itm->FileSize, Itm->DestFile.c_str(),  Itm->DescURI().c_str(),
                               Itm->ID,Itm->ErrorText.c_str());
}

static void acquireitem_dealloc(PyObject *self)
{
    CppDeallocPtr<pkgAcquire::Item*>(self);
}

PyTypeObject PyAcquireItem_Type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "apt_pkg.AcquireItem",         // tp_name
    sizeof(CppPyObject<pkgAcquire::Item*>),   // tp_basicsize
    0,                                   // tp_itemsize
    // Methods
    acquireitem_dealloc,                  // tp_dealloc
    0,                                   // tp_print
    0,                                   // tp_getattr
    0,                                   // tp_setattr
    0,                                   // tp_compare
    acquireitem_repr,                     // tp_repr
    0,                                   // tp_as_number
    0,                                   // 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 |
    Py_TPFLAGS_HAVE_GC,                  // tp_flags
    "AcquireItem Object",                // tp_doc
    CppTraverse<pkgAcquire::Item*>, // tp_traverse
    CppClear<pkgAcquire::Item*>,    // tp_clear
    0,                                   // tp_richcompare
    0,                                   // tp_weaklistoffset
    0,                                   // tp_iter
    0,                                   // tp_iternext
    0,                                   // tp_methods
    0,                                   // tp_members
    acquireitem_getset,                   // tp_getset
};

static PyObject *acquirefile_new(PyTypeObject *type, PyObject *Args, PyObject * kwds)
{
    PyObject *pyfetcher;
    char *uri, *md5, *descr, *shortDescr, *destDir, *destFile;
    int size = 0;
    uri = md5 = descr = shortDescr = destDir = destFile = "";

    char *kwlist[] = {"owner","uri", "md5", "size", "descr", "short_descr",
                      "destdir", "destfile", NULL
                     };

    if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist,
                                    &PyAcquire_Type, &pyfetcher, &uri, &md5,
                                    &size, &descr, &shortDescr, &destDir, &destFile) == 0)
        return 0;

    pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher);
    pkgAcqFile *af = new pkgAcqFile(fetcher,  // owner
                                    uri, // uri
                                    md5,  // md5
                                    size,   // size
                                    descr, // descr
                                    shortDescr,
                                    destDir,
                                    destFile); // short-desc
    CppPyObject<pkgAcqFile*> *AcqFileObj = CppPyObject_NEW<pkgAcqFile*>(pyfetcher, type);
    AcqFileObj->Object = af;
    return AcqFileObj;
}


static char *acquirefile_doc =
    "AcquireFile(owner, uri[, md5, size, descr, short_descr, destdir,"
    "destfile]) -> New AcquireFile() object\n\n"
    "The parameter *owner* refers to an apt_pkg.Acquire() object. You can use\n"
    "*destdir* OR *destfile* to specify the destination directory/file.";

PyTypeObject PyAcquireFile_Type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "apt_pkg.AcquireFile",                // tp_name
    sizeof(CppPyObject<pkgAcqFile*>),// tp_basicsize
    0,                                   // tp_itemsize
    // Methods
    acquireitem_dealloc,                 // tp_dealloc
    0,                                   // tp_print
    0,                                   // tp_getattr
    0,                                   // tp_setattr
    0,                                   // tp_compare
    0,                                   // tp_repr
    0,                                   // tp_as_number
    0,                                   // 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
    Py_TPFLAGS_BASETYPE |
    Py_TPFLAGS_HAVE_GC,
    acquirefile_doc,                     // tp_doc
    CppTraverse<pkgAcqFile*>,       // tp_traverse
    CppClear<pkgAcqFile*>,          // tp_clear
    0,                                   // tp_richcompare
    0,                                   // tp_weaklistoffset
    0,                                   // tp_iter
    0,                                   // tp_iternext
    0,                                   // tp_methods
    0,                                   // tp_members
    0,                                   // tp_getset
    &PyAcquireItem_Type,                 // tp_base
    0,                                   // tp_dict
    0,                                   // tp_descr_get
    0,                                   // tp_descr_set
    0,                                   // tp_dictoffset
    0,                                   // tp_init
    0,                                   // tp_alloc
    acquirefile_new,                     // tp_new
};

#ifdef COMPAT_0_7
char *doc_GetPkgAcqFile =
    "GetPkgAcqFile(pkgAquire, uri[, md5, size, descr, shortDescr, destDir, destFile]) -> PkgAcqFile\n";
PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds)
{
    PyErr_WarnEx(PyExc_DeprecationWarning, "apt_pkg.GetPkgAcqFile() is "
                 "deprecated. Please see apt_pkg.AcquireFile() for the "
                 "replacement", 1);
    PyObject *pyfetcher;
    char *uri, *md5, *descr, *shortDescr, *destDir, *destFile;
    int size = 0;
    uri = md5 = descr = shortDescr = destDir = destFile = "";

    char * kwlist[] = {"owner","uri", "md5", "size", "descr", "shortDescr",
                       "destDir", "destFile", NULL
                      };

    if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist,
                                    &PyAcquire_Type, &pyfetcher, &uri, &md5,
                                    &size, &descr, &shortDescr, &destDir, &destFile) == 0)
        return 0;

    pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher);
    pkgAcqFile *af = new pkgAcqFile(fetcher,  // owner
                                    uri, // uri
                                    md5,  // md5
                                    size,   // size
                                    descr, // descr
                                    shortDescr,
                                    destDir,
                                    destFile); // short-desc
    CppPyObject<pkgAcqFile*> *AcqFileObj = CppPyObject_NEW<pkgAcqFile*>(NULL, &PyAcquireFile_Type);
    AcqFileObj->Object = af;
    AcqFileObj->NoDelete = true;

    return AcqFileObj;
}
#endif