diff options
Diffstat (limited to 'python/acquire.cc')
| -rw-r--r-- | python/acquire.cc | 558 |
1 files changed, 335 insertions, 223 deletions
diff --git a/python/acquire.cc b/python/acquire.cc index 1ecf55a5..5e22280e 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -1,278 +1,390 @@ -// Description /*{{{*/ -// $Id: acquire.cc,v 1.1 2003/06/03 03:03:23 mvo Exp $ -/* ###################################################################### - - Acquire - Wrapper for the acquire code - - ##################################################################### */ +/* acquire.cc - Wrapper for pkgAcquire. + * + * Copyright 2004-2009 Canonical Ltd + * Copyright 2009 Julian Andres Klode <jak@debian.org> + * + * Authors: Michael Vogt + * Julian Andres Klode + * + * 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 "progress.h" #include <apt-pkg/acquire-item.h> +#include <apt-pkg/acquire-worker.h> -// pkgAcquire::Item -static PyObject *AcquireItemAttr(PyObject *Self,char *Name) + +static PyObject *acquireworker_get_current_item(PyObject *self, void *closure) { - pkgAcquire::ItemIterator &I = GetCpp<pkgAcquire::ItemIterator>(Self); - - if (strcmp("ID",Name) == 0) - return Py_BuildValue("i",(*I)->ID); - else if (strcmp("Status",Name) == 0) - return Py_BuildValue("i",(*I)->Status); - else if (strcmp("Complete",Name) == 0) - return Py_BuildValue("i",(*I)->Complete); - else if (strcmp("Local",Name) == 0) - return Py_BuildValue("i",(*I)->Local); - else if (strcmp("IsTrusted",Name) == 0) - return Py_BuildValue("i",(*I)->IsTrusted()); - else if (strcmp("FileSize",Name) == 0) - return Py_BuildValue("i",(*I)->FileSize); - else if (strcmp("ErrorText",Name) == 0) - return Safe_FromString((*I)->ErrorText.c_str()); - else if (strcmp("DestFile",Name) == 0) - return Safe_FromString((*I)->DestFile.c_str()); - else if (strcmp("DescURI",Name) == 0) - return Safe_FromString((*I)->DescURI().c_str()); - // constants - else if (strcmp("StatIdle",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Item::StatIdle); - else if (strcmp("StatFetching",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Item::StatFetching); - else if (strcmp("StatDone",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Item::StatDone); - else if (strcmp("StatError",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Item::StatError); - else if (strcmp("StatAuthError",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Item::StatAuthError); - - - PyErr_SetString(PyExc_AttributeError,Name); - return 0; + pkgAcquire::Worker *worker = GetCpp<pkgAcquire::Worker*>(self); + pkgAcquire::ItemDesc *desc = worker->CurrentItem; + if (desc == NULL) { + Py_RETURN_NONE; + } + PyObject *PyAcq = GetOwner<pkgAcquire::Worker*>(self); + PyObject *PyItem = PyAcquireItem_FromCpp(desc->Owner, false, PyAcq); + PyObject *PyDesc = PyAcquireItemDesc_FromCpp(desc, false, PyItem); + Py_XDECREF(PyItem); + return PyDesc; } +static PyObject *acquireworker_get_status(PyObject *self, void *closure) +{ + return CppPyString(GetCpp<pkgAcquire::Worker*>(self)->Status); +} -static PyObject *AcquireItemRepr(PyObject *Self) +static PyObject *acquireworker_get_current_size(PyObject *self, void *closure) { - pkgAcquire::ItemIterator &I = GetCpp<pkgAcquire::ItemIterator>(Self); - - char S[300]; - snprintf(S,sizeof(S),"<pkgAcquire::ItemIterator object: " - "Status: %i Complete: %i Local: %i IsTrusted: %i " - "FileSize: %i DestFile:'%s' " - "DescURI: '%s' ID:%i ErrorText: '%s'>", - (*I)->Status, (*I)->Complete, (*I)->Local, (*I)->IsTrusted(), - (*I)->FileSize, (*I)->DestFile.c_str(), (*I)->DescURI().c_str(), - (*I)->ID,(*I)->ErrorText.c_str()); - return PyString_FromString(S); + return Py_BuildValue("k",GetCpp<pkgAcquire::Worker*>(self)->CurrentSize); } +static PyObject *acquireworker_get_total_size(PyObject *self, void *closure) +{ + return Py_BuildValue("k",GetCpp<pkgAcquire::Worker*>(self)->TotalSize); +} -PyTypeObject AcquireItemType = +static PyObject *acquireworker_get_resumepoint(PyObject *self, void *closure) { - PyObject_HEAD_INIT(&PyType_Type) - 0, // ob_size - "pkgAcquire::ItemIterator", // tp_name - sizeof(CppOwnedPyObject<pkgAcquire::ItemIterator>), // tp_basicsize - 0, // tp_itemsize - // Methods - CppOwnedDealloc<pkgAcquire::ItemIterator>, // tp_dealloc - 0, // tp_print - AcquireItemAttr, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - AcquireItemRepr, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash -}; + return Py_BuildValue("k",GetCpp<pkgAcquire::Worker*>(self)->ResumePoint); +} +static PyGetSetDef acquireworker_getset[] = { + {"current_item",acquireworker_get_current_item}, + {"status",acquireworker_get_status}, + {"current_size",acquireworker_get_current_size}, + {"total_size",acquireworker_get_total_size}, + {"resumepoint",acquireworker_get_resumepoint}, + {NULL} +}; -static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) -{ - pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); - int pulseInterval = 500000; - if (PyArg_ParseTuple(Args, "|i", &pulseInterval) == 0) - return 0; +PyTypeObject PyAcquireWorker_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.AcquireWorker", // tp_name + sizeof(CppPyObject<pkgAcquire::Worker*>),// tp_basicsize + 0, // tp_itemsize + // Methods + CppDealloc<pkgAcquire::Worker*>, // 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_HAVE_GC, + 0, // tp_doc + CppTraverse<pkgAcquire::Worker*>, // tp_traverse + CppClear<pkgAcquire::Worker*>, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + acquireworker_getset, // tp_getset +}; - pkgAcquire::RunResult run = fetcher->Run(pulseInterval); - return HandleErrors(Py_BuildValue("i",run)); +static pkgAcquire::ItemDesc* acquireitemdesc_tocpp(PyObject *self) { + pkgAcquire::ItemDesc *item = GetCpp<pkgAcquire::ItemDesc*>(self); + if (item == NULL) + PyErr_SetString(PyExc_ValueError, "Acquire has been shutdown"); + return item; } -static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) +static PyObject *acquireitemdesc_get_uri(PyObject *self, void *closure) { - pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); - - if (PyArg_ParseTuple(Args, "") == 0) - return 0; - - fetcher->Shutdown(); - - Py_INCREF(Py_None); - return HandleErrors(Py_None); + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->URI) : NULL; } - -static PyMethodDef PkgAcquireMethods[] = +static PyObject *acquireitemdesc_get_description(PyObject *self, void *closure) { - {"Run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, - {"Shutdown",PkgAcquireShutdown, METH_VARARGS,"Shutdown the fetcher"}, - {} -}; - - -static PyObject *AcquireAttr(PyObject *Self,char *Name) + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->Description) : NULL; +} +static PyObject *acquireitemdesc_get_shortdesc(PyObject *self, void *closure) { - pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); - - if(strcmp("TotalNeeded",Name) == 0) - return Py_BuildValue("d", fetcher->TotalNeeded()); - if(strcmp("FetchNeeded",Name) == 0) - return Py_BuildValue("d", fetcher->FetchNeeded()); - if(strcmp("PartialPresent",Name) == 0) - return Py_BuildValue("d", fetcher->PartialPresent()); - if(strcmp("Items",Name) == 0) - { - PyObject *List = PyList_New(0); - for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); - I != fetcher->ItemsEnd(); I++) - { - PyObject *Obj; - Obj = CppOwnedPyObject_NEW<pkgAcquire::ItemIterator>(Self,&AcquireItemType,I); - PyList_Append(List,Obj); - Py_DECREF(Obj); - - } - return List; - } - // some constants - if(strcmp("ResultContinue",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Continue); - if(strcmp("ResultFailed",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Failed); - if(strcmp("ResultCancelled",Name) == 0) - return Py_BuildValue("i", pkgAcquire::Cancelled); - - return Py_FindMethod(PkgAcquireMethods,Self,Name); + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->ShortDesc) : NULL; +} +static PyObject *acquireitemdesc_get_owner(CppPyObject<pkgAcquire::ItemDesc*> *self, void *closure) +{ + if (self->Owner != NULL) { + Py_INCREF(self->Owner); + return self->Owner; + } + else if (self->Object) { + self->Owner = PyAcquireItem_FromCpp(self->Object->Owner, false, NULL); + Py_INCREF(self->Owner); + return self->Owner; + } + Py_RETURN_NONE; } +static PyGetSetDef acquireitemdesc_getset[] = { + {"uri",acquireitemdesc_get_uri,0,"The URI from which to download this item."}, + {"description",acquireitemdesc_get_description}, + {"shortdesc",acquireitemdesc_get_shortdesc}, + {"owner",(getter)acquireitemdesc_get_owner}, + {NULL} +}; - - -PyTypeObject PkgAcquireType = -{ - PyObject_HEAD_INIT(&PyType_Type) - 0, // ob_size - "Acquire", // tp_name - sizeof(CppPyObject<pkgAcquire*>), // tp_basicsize - 0, // tp_itemsize - // Methods - CppDealloc<pkgAcquire*>, // tp_dealloc - 0, // tp_print - AcquireAttr, // 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 +static char *acquireitemdesc_doc = + "Represent an AcquireItemDesc"; + +PyTypeObject PyAcquireItemDesc_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.AcquireItemDesc", // tp_name + sizeof(CppPyObject<pkgAcquire::ItemDesc*>),// tp_basicsize + 0, // tp_itemsize + // Methods + CppDealloc<pkgAcquire::ItemDesc*>, // 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_HAVE_GC), + acquireitemdesc_doc, // tp_doc + CppTraverse<pkgAcquire::ItemDesc*>,// tp_traverse + CppClear<pkgAcquire::ItemDesc*>, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + acquireitemdesc_getset, // 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 + 0, // tp_new }; -PyObject *GetAcquire(PyObject *Self,PyObject *Args) +static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) { - pkgAcquire *fetcher; - - PyObject *pyFetchProgressInst = NULL; - if (PyArg_ParseTuple(Args,"|O",&pyFetchProgressInst) == 0) - return 0; + pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); - if (pyFetchProgressInst != NULL) { - // FIXME: memleak? - PyFetchProgress *progress = new PyFetchProgress(); - progress->setCallbackInst(pyFetchProgressInst); - fetcher = new pkgAcquire(progress); - } else { - fetcher = new pkgAcquire(); - } + int pulseInterval = 500000; + if (PyArg_ParseTuple(Args, "|i", &pulseInterval) == 0) + return 0; - CppPyObject<pkgAcquire*> *FetcherObj = - CppPyObject_NEW<pkgAcquire*>(&PkgAcquireType, fetcher); + pkgAcquire::RunResult run = fetcher->Run(pulseInterval); - return FetcherObj; + return HandleErrors(Py_BuildValue("i",run)); } +static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) +{ + pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); + if (PyArg_ParseTuple(Args, "") == 0) + return 0; + fetcher->Shutdown(); + Py_INCREF(Py_None); + return HandleErrors(Py_None); +} -// pkgAcquireFile +static PyMethodDef PkgAcquireMethods[] = { + {"run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, + {"shutdown",PkgAcquireShutdown, METH_VARARGS,"Shutdown the fetcher"}, +#ifdef COMPAT_0_7 + {"Run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, + {"Shutdown",PkgAcquireShutdown, METH_VARARGS,"Shutdown the fetcher"}, +#endif + {} +}; -static PyObject *AcquireFileAttr(PyObject *Self,char *Name) +#define fetcher (GetCpp<pkgAcquire*>(Self)) +static PyObject *PkgAcquireGetTotalNeeded(PyObject *Self,void*) { - pkgAcqFile *acqFile = GetCpp<pkgAcqFile*>(Self); + return Py_BuildValue("d", fetcher->TotalNeeded()); +} +static PyObject *PkgAcquireGetFetchNeeded(PyObject *Self,void*) +{ + return Py_BuildValue("d", fetcher->FetchNeeded()); +} +static PyObject *PkgAcquireGetPartialPresent(PyObject *Self,void*) +{ + return Py_BuildValue("d", fetcher->PartialPresent()); +} +#undef fetcher - PyErr_SetString(PyExc_AttributeError,Name); - return 0; +static PyObject *PkgAcquireGetWorkers(PyObject *self, void *closure) +{ + PyObject *List = PyList_New(0); + pkgAcquire *Owner = GetCpp<pkgAcquire*>(self); + PyObject *PyWorker = NULL; + for (pkgAcquire::Worker *Worker = Owner->WorkersBegin(); + Worker != 0; Worker = Owner->WorkerStep(Worker)) { + PyWorker = PyAcquireWorker_FromCpp(Worker, false, self); + PyList_Append(List, PyWorker); + Py_DECREF(PyWorker); + } + return List; +} +static PyObject *PkgAcquireGetItems(PyObject *Self,void*) +{ + pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); + PyObject *List = PyList_New(0); + PyObject *Obj; + for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); + I != fetcher->ItemsEnd(); I++) { + Obj = PyAcquireItem_FromCpp(*I, false, Self); + PyList_Append(List,Obj); + Py_DECREF(Obj); + } + return List; } +static PyGetSetDef PkgAcquireGetSet[] = { + {"fetch_needed",PkgAcquireGetFetchNeeded}, + {"items",PkgAcquireGetItems}, + {"workers",PkgAcquireGetWorkers}, + {"partial_present",PkgAcquireGetPartialPresent}, + {"total_needed",PkgAcquireGetTotalNeeded}, +#ifdef COMPAT_0_7 + {"FetchNeeded",PkgAcquireGetFetchNeeded}, + {"Items",PkgAcquireGetItems}, + {"PartialPresent",PkgAcquireGetPartialPresent}, + {"TotalNeeded",PkgAcquireGetTotalNeeded}, +#endif + {} +}; +static PyObject *PkgAcquireNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) +{ + pkgAcquire *fetcher; + + PyObject *pyFetchProgressInst = NULL; + char *kwlist[] = {"progress", 0}; + if (PyArg_ParseTupleAndKeywords(Args,kwds,"|O",kwlist,&pyFetchProgressInst) == 0) + return 0; + + PyFetchProgress *progress = 0; + if (pyFetchProgressInst != NULL) { + // FIXME: memleak? + progress = new PyFetchProgress(); + progress->setCallbackInst(pyFetchProgressInst); + fetcher = new pkgAcquire(progress); + } + else { + fetcher = new pkgAcquire(); + } + + PyObject *FetcherObj = CppPyObject_NEW<pkgAcquire*>(NULL, type, fetcher); + + if (progress != 0) + progress->setPyAcquire(FetcherObj); + // prepare our map of items. + return FetcherObj; +} +/** + * Create a new apt_pkg.Acquire Python object from the pkgAcquire object. + */ +PyObject *PyAcquire_FromCpp(pkgAcquire *fetcher, bool Delete, PyObject *owner) { + CppPyObject<pkgAcquire*> *obj = CppPyObject_NEW<pkgAcquire*>(owner, &PyAcquire_Type, fetcher); + obj->NoDelete = (!Delete); + return obj; +} -PyTypeObject PkgAcquireFileType = -{ - PyObject_HEAD_INIT(&PyType_Type) - 0, // ob_size - "pkgAcquireFile", // tp_name - sizeof(CppPyObject<pkgAcqFile*>),// tp_basicsize - 0, // tp_itemsize - // Methods - CppDealloc<pkgAcqFile*>, // tp_dealloc - 0, // tp_print - AcquireFileAttr, // 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 +static char *doc_PkgAcquire = + "Acquire(progress: apt_pkg.AcquireProgress) -> Acquire() object.\n\n" + "Create a new acquire object. The parameter *progress* can be used to\n" + "specify an apt_pkg.AcquireProgress() object, which will display the\n" + "progress of the fetching."; + +PyTypeObject PyAcquire_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.Acquire", // tp_name + sizeof(CppPyObject<pkgAcquire*>), // tp_basicsize + 0, // tp_itemsize + // Methods + CppDeallocPtr<pkgAcquire*>, // 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), + doc_PkgAcquire, // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + PkgAcquireMethods, // tp_methods + 0, // tp_members + PkgAcquireGetSet, // 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 + PkgAcquireNew, // tp_new }; -char *doc_GetPkgAcqFile = -"GetPkgAcqFile(pkgAquire, uri[, md5, size, descr, shortDescr, destDir, destFile]) -> PkgAcqFile\n"; -PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) +#ifdef COMPAT_0_7 +PyObject *GetAcquire(PyObject *Self,PyObject *Args) { - 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, - &PkgAcquireType, &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*>(&PkgAcquireFileType); - AcqFileObj->Object = af; - - return AcqFileObj; + PyErr_WarnEx(PyExc_DeprecationWarning,"apt_pkg.GetAcquire() is deprecated." + " Please see apt_pkg.Acquire() for the replacement.", 1); + return PkgAcquireNew(&PyAcquire_Type,Args,0); } +#endif - - /*}}}*/ |
