From c187e828e1661d09a8437214b2f90dcc25c05c99 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 18 Nov 2005 01:01:37 +0000 Subject: * basic pkgAcquire + pkgPackageManager support added --- python/acquire.cc | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 python/acquire.cc (limited to 'python/acquire.cc') diff --git a/python/acquire.cc b/python/acquire.cc new file mode 100644 index 00000000..550deb0e --- /dev/null +++ b/python/acquire.cc @@ -0,0 +1,106 @@ +// Description /*{{{*/ +// $Id: acquire.cc,v 1.1 2003/06/03 03:03:23 mvo Exp $ +/* ###################################################################### + + Acquire - Wrapper for the acquire code + + ##################################################################### */ + +#include "generic.h" +#include "apt_pkgmodule.h" +#include "acquire.h" + +#include + + +static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) +{ + PkgAcquireStruct &Struct = GetCpp(Self); + + if (PyArg_ParseTuple(Args, "") == 0) + return 0; + + //FIXME: add pulse interval here + pkgAcquire::RunResult run = Struct.fetcher.Run(); + + return HandleErrors(Py_BuildValue("i",run)); +} + +static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) +{ + PkgAcquireStruct &Struct = GetCpp(Self); + + if (PyArg_ParseTuple(Args, "") == 0) + return 0; + + Struct.fetcher.Shutdown(); + + Py_INCREF(Py_None); + return HandleErrors(Py_None); +} + +static PyMethodDef PkgAcquireMethods[] = +{ + {"Run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, + {} +}; + + +static PyObject *AcquireAttr(PyObject *Self,char *Name) +{ + PkgAcquireStruct &Struct = GetCpp(Self); + + if(strcmp("TotalNeeded",Name) == 0) + return Py_BuildValue("l", Struct.fetcher.TotalNeeded()); + if(strcmp("FetchNeeded",Name) == 0) + return Py_BuildValue("l", Struct.fetcher.FetchNeeded()); + if(strcmp("PartialPresent",Name) == 0) + return Py_BuildValue("l", Struct.fetcher.PartialPresent()); + // 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); +} + + + + +PyTypeObject PkgAcquireType = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, // ob_size + "Acquire", // tp_name + sizeof(CppOwnedPyObject), // tp_basicsize + 0, // tp_itemsize + // Methods + CppOwnedDealloc, // 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 +}; + +PyObject *GetAcquire(PyObject *Self,PyObject *Args) +{ + pkgAcquire *fetcher = new pkgAcquire(); + + CppOwnedPyObject *FetcherObj = + CppOwnedPyObject_NEW(0,&PkgAcquireType, *fetcher); + + return FetcherObj; +} + + + + + /*}}}*/ -- cgit v1.2.3 From a56d4330c45ea9dbb9dd8babb7e8f17ae79a6fdd Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 18 Nov 2005 11:23:10 +0000 Subject: * acquire interface works with progress reporting now --- doc/examples/acquire.py | 10 +++++++++- python/acquire.cc | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'python/acquire.cc') diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 12727232..98b70ab0 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -1,4 +1,6 @@ +import apt import apt_pkg +import os apt_pkg.init() @@ -9,10 +11,16 @@ recs = apt_pkg.GetPkgRecords(cache) list = apt_pkg.GetPkgSourceList() list.ReadMainList() +os.mkdir("/tmp/pyapt-test") +os.mkdir("/tmp/pyapt-test/partial") +apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") + pkg = cache["3ddesktop"] depcache.MarkInstall(pkg) -fetcher = apt_pkg.GetAcquire() +progress = apt.progress.TextFetchProgress() +fetcher = apt_pkg.GetAcquire(progress) +#fetcher = apt_pkg.GetAcquire() pm = apt_pkg.GetPackageManager(depcache) print pm diff --git a/python/acquire.cc b/python/acquire.cc index 550deb0e..ff01b659 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -9,6 +9,7 @@ #include "generic.h" #include "apt_pkgmodule.h" #include "acquire.h" +#include "progress.h" #include @@ -92,7 +93,20 @@ PyTypeObject PkgAcquireType = PyObject *GetAcquire(PyObject *Self,PyObject *Args) { - pkgAcquire *fetcher = new pkgAcquire(); + pkgAcquire *fetcher; + + PyObject *pyFetchProgressInst = NULL; + if (PyArg_ParseTuple(Args,"|O",&pyFetchProgressInst) == 0) + return 0; + + if (pyFetchProgressInst != NULL) { + // FIXME: memleak? + PyFetchProgress *progress = new PyFetchProgress(); + progress->setCallbackInst(pyFetchProgressInst); + fetcher = new pkgAcquire(progress); + } else { + fetcher = new pkgAcquire(); + } CppOwnedPyObject *FetcherObj = CppOwnedPyObject_NEW(0,&PkgAcquireType, *fetcher); -- cgit v1.2.3 From bf020b8b55739672ce5335850db921277539327e Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 18 Nov 2005 11:54:43 +0000 Subject: * support for the pkgAcquire::ItemIterator added --- doc/examples/acquire.py | 19 +++++++++++-- python/acquire.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) (limited to 'python/acquire.cc') diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 98b70ab0..f920e5e9 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -11,8 +11,11 @@ recs = apt_pkg.GetPkgRecords(cache) list = apt_pkg.GetPkgSourceList() list.ReadMainList() -os.mkdir("/tmp/pyapt-test") -os.mkdir("/tmp/pyapt-test/partial") +try: + os.mkdir("/tmp/pyapt-test") + os.mkdir("/tmp/pyapt-test/partial") +except OSError: + pass apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") pkg = cache["3ddesktop"] @@ -28,5 +31,15 @@ print fetcher pm.GetArchives(fetcher,list,recs) -fetcher.Run() +for item in fetcher.Items: + print "%s -> %s:\n Status: %s Complete: %s IsTrusted: %s" % (item.DescURI, + item.DestFile, + item.Status, + item.Complete, + item.IsTrusted) + print + + +res = fetcher.Run() +print "fetcher.Run() returned: %s" % res diff --git a/python/acquire.cc b/python/acquire.cc index ff01b659..a65b9592 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -13,6 +13,67 @@ #include +// pkgAcquire::Item +static PyObject *AcquireItemAttr(PyObject *Self,char *Name) +{ + pkgAcquire::ItemIterator &I = GetCpp(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()); + + PyErr_SetString(PyExc_AttributeError,Name); + return 0; +} + + +static PyObject *AcquireItemRepr(PyObject *Self) +{ + pkgAcquire::ItemIterator &I = GetCpp(Self); + + char S[300]; + snprintf(S,sizeof(S),"", + (*I)->DestFile.c_str(),(*I)->ID); + return PyString_FromString(S); +} + + +PyTypeObject AcquireItemType = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, // ob_size + "pkgAcquire::ItemIterator", // tp_name + sizeof(CppOwnedPyObject), // tp_basicsize + 0, // tp_itemsize + // Methods + CppOwnedDealloc, // 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 +}; + static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) { @@ -57,6 +118,20 @@ static PyObject *AcquireAttr(PyObject *Self,char *Name) return Py_BuildValue("l", Struct.fetcher.FetchNeeded()); if(strcmp("PartialPresent",Name) == 0) return Py_BuildValue("l", Struct.fetcher.PartialPresent()); + if(strcmp("Items",Name) == 0) + { + PyObject *List = PyList_New(0); + for (pkgAcquire::ItemIterator I = Struct.fetcher.ItemsBegin(); + I != Struct.fetcher.ItemsEnd(); I++) + { + PyObject *Obj; + Obj = CppOwnedPyObject_NEW(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); -- cgit v1.2.3 From 2fa652df49918cbfd8def4868b327bab3b30feb9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 18 Nov 2005 12:01:37 +0000 Subject: * added stat constants to the acquire-item interface --- doc/examples/acquire.py | 6 +++++- python/acquire.cc | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'python/acquire.cc') diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index f920e5e9..0031fa18 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -16,7 +16,7 @@ try: os.mkdir("/tmp/pyapt-test/partial") except OSError: pass -apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") +#apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") pkg = cache["3ddesktop"] depcache.MarkInstall(pkg) @@ -37,6 +37,10 @@ for item in fetcher.Items: item.Status, item.Complete, item.IsTrusted) + if item.Status == item.StatError: + print "Some error ocured: '%s'" % item.ErrorText + if item.Complete == False: + print "No error, still nothing downloaded" print diff --git a/python/acquire.cc b/python/acquire.cc index a65b9592..91d75a6b 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -36,6 +36,18 @@ static PyObject *AcquireItemAttr(PyObject *Self,char *Name) 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; -- cgit v1.2.3 From f398b1a5833fdf50f8c0c541958d5770ea767be8 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 22 Nov 2005 11:21:49 +0000 Subject: * fix in the pkgmanager.cc code (/me needs to be hit with a clue-stick) --- debian/changelog | 2 +- doc/examples/acquire.py | 20 +++++++++++------- python/acquire.cc | 8 +++++-- python/pkgmanager.cc | 56 +++++++++++++++++++++++++++++++++++++------------ 4 files changed, 63 insertions(+), 23 deletions(-) (limited to 'python/acquire.cc') diff --git a/debian/changelog b/debian/changelog index b0fe307f..9ffd5b12 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,7 +2,7 @@ python-apt (0.6.15) unstable; urgency=low * fix a invalid return from cache.commit(), fail if a download failed * apt.Package.candidateOrigin returns a class now - * added basic pkgAcquire and pkgPackageManager support + * added pkgAcquire, pkgPackageManager and a example (acquire.py) -- Michael Vogt Thu, 17 Nov 2005 13:00:14 +0100 diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 0031fa18..a5de0029 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -1,9 +1,14 @@ import apt import apt_pkg import os +import sys apt_pkg.init() +#apt_pkg.Config.Set("Debug::pkgDPkgPM","1"); +#apt_pkg.Config.Set("Debug::pkgPackageManager","1"); +#apt_pkg.Config.Set("Debug::pkgDPkgProgressReporting","1"); + cache = apt_pkg.GetCache() depcache = apt_pkg.GetDepCache(cache) @@ -16,7 +21,7 @@ try: os.mkdir("/tmp/pyapt-test/partial") except OSError: pass -#apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") +apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") pkg = cache["3ddesktop"] depcache.MarkInstall(pkg) @@ -32,18 +37,19 @@ print fetcher pm.GetArchives(fetcher,list,recs) for item in fetcher.Items: - print "%s -> %s:\n Status: %s Complete: %s IsTrusted: %s" % (item.DescURI, - item.DestFile, - item.Status, - item.Complete, - item.IsTrusted) + print item if item.Status == item.StatError: print "Some error ocured: '%s'" % item.ErrorText if item.Complete == False: - print "No error, still nothing downloaded" + print "No error, still nothing downloaded (%s)" % item.ErrorText print res = fetcher.Run() print "fetcher.Run() returned: %s" % res +print "now runing pm.DoInstall()" +res = pm.DoInstall(1) +print "pm.DoInstall() returned: %s"% res + + diff --git a/python/acquire.cc b/python/acquire.cc index 91d75a6b..87f2717a 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -60,8 +60,12 @@ static PyObject *AcquireItemRepr(PyObject *Self) char S[300]; snprintf(S,sizeof(S),"", - (*I)->DestFile.c_str(),(*I)->ID); + "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); } diff --git a/python/pkgmanager.cc b/python/pkgmanager.cc index 9504fd94..b4c64d3e 100644 --- a/python/pkgmanager.cc +++ b/python/pkgmanager.cc @@ -14,18 +14,14 @@ #include #include +#include #include -struct PkgManagerStruct -{ - pkgPackageManager pm; -}; - static PyObject *PkgManagerGetArchives(PyObject *Self,PyObject *Args) { - PkgManagerStruct &Struct = GetCpp(Self); + pkgPackageManager *pm = GetCpp(Self); PyObject *fetcher, *list, *recs; if (PyArg_ParseTuple(Args, "O!O!O!", @@ -38,24 +34,53 @@ static PyObject *PkgManagerGetArchives(PyObject *Self,PyObject *Args) PkgSourceListStruct &s_list = GetCpp(list); PkgRecordsStruct &s_records = GetCpp(recs); - bool res = Struct.pm.GetArchives(&s_fetcher.fetcher, + bool res = pm->GetArchives(&s_fetcher.fetcher, &s_list.List, &s_records.Records); - return HandleErrors(Py_None); + return HandleErrors(Py_BuildValue("b",res)); +} + +static PyObject *PkgManagerDoInstall(PyObject *Self,PyObject *Args) +{ + //PkgManagerStruct &Struct = GetCpp(Self); + pkgPackageManager *pm = GetCpp(Self); + int status_fd = -1; + + if (PyArg_ParseTuple(Args, "|i", &status_fd) == 0) + return 0; + + pkgPackageManager::OrderResult res = pm->DoInstall(status_fd); + + return HandleErrors(Py_BuildValue("i",res)); } +static PyObject *PkgManagerFixMissing(PyObject *Self,PyObject *Args) +{ + //PkgManagerStruct &Struct = GetCpp(Self); + pkgPackageManager *pm = GetCpp(Self); + + if (PyArg_ParseTuple(Args, "") == 0) + return 0; + + bool res = pm->FixMissing(); + + return HandleErrors(Py_BuildValue("b",res)); +} static PyMethodDef PkgManagerMethods[] = { {"GetArchives",PkgManagerGetArchives,METH_VARARGS,"Load the selected archvies into the fetcher"}, + {"DoInstall",PkgManagerDoInstall,METH_VARARGS,"Do the actual install"}, + {"FixMissing",PkgManagerFixMissing,METH_VARARGS,"Fix the install if a pkg couldn't be downloaded"}, {} }; static PyObject *PkgManagerAttr(PyObject *Self,char *Name) { - PkgManagerStruct &Struct = GetCpp(Self); + //PkgManagerStruct &Struct = GetCpp(Self); + pkgPackageManager *pm = GetCpp(Self); // some constants if(strcmp("ResultCompleted",Name) == 0) @@ -74,10 +99,10 @@ PyTypeObject PkgManagerType = PyObject_HEAD_INIT(&PyType_Type) 0, // ob_size "PackageManager", // tp_name - sizeof(CppOwnedPyObject), // tp_basicsize + sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDealloc, // tp_dealloc 0, // tp_print PkgManagerAttr, // tp_getattr 0, // tp_setattr @@ -89,6 +114,9 @@ PyTypeObject PkgManagerType = 0, // tp_hash }; +#include +#include + PyObject *GetPkgManager(PyObject *Self,PyObject *Args) { PyObject *Owner; @@ -97,9 +125,11 @@ PyObject *GetPkgManager(PyObject *Self,PyObject *Args) pkgPackageManager *pm = _system->CreatePM(GetCpp(Owner)); - CppOwnedPyObject *PkgManagerObj = - CppOwnedPyObject_NEW(0,&PkgManagerType, *pm); + CppPyObject *PkgManagerObj = + CppPyObject_NEW(&PkgManagerType,pm); + // FIXME: mem-leak??? + Py_INCREF(PkgManagerObj); return PkgManagerObj; } -- cgit v1.2.3 From f505ecbf86d6ab30208c579a5e25c819c3733d89 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 22 Nov 2005 11:52:23 +0000 Subject: * fixes in the acquire interface code (use CppPyObject with pkgAcquire* directly instead of using a PkgAcquireStruct) --- python/acquire.cc | 29 ++++++++++++++--------------- python/acquire.h | 6 ------ python/pkgmanager.cc | 7 +++---- python/sourcelist.cc | 1 - 4 files changed, 17 insertions(+), 26 deletions(-) delete mode 100644 python/acquire.h (limited to 'python/acquire.cc') diff --git a/python/acquire.cc b/python/acquire.cc index 87f2717a..1bbed72a 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -8,7 +8,6 @@ #include "generic.h" #include "apt_pkgmodule.h" -#include "acquire.h" #include "progress.h" #include @@ -93,25 +92,25 @@ PyTypeObject AcquireItemType = static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) { - PkgAcquireStruct &Struct = GetCpp(Self); + pkgAcquire *fetcher = GetCpp(Self); if (PyArg_ParseTuple(Args, "") == 0) return 0; //FIXME: add pulse interval here - pkgAcquire::RunResult run = Struct.fetcher.Run(); + pkgAcquire::RunResult run = fetcher->Run(); return HandleErrors(Py_BuildValue("i",run)); } static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) { - PkgAcquireStruct &Struct = GetCpp(Self); + pkgAcquire *fetcher = GetCpp(Self); if (PyArg_ParseTuple(Args, "") == 0) return 0; - Struct.fetcher.Shutdown(); + fetcher->Shutdown(); Py_INCREF(Py_None); return HandleErrors(Py_None); @@ -126,19 +125,19 @@ static PyMethodDef PkgAcquireMethods[] = static PyObject *AcquireAttr(PyObject *Self,char *Name) { - PkgAcquireStruct &Struct = GetCpp(Self); + pkgAcquire *fetcher = GetCpp(Self); if(strcmp("TotalNeeded",Name) == 0) - return Py_BuildValue("l", Struct.fetcher.TotalNeeded()); + return Py_BuildValue("l", fetcher->TotalNeeded()); if(strcmp("FetchNeeded",Name) == 0) - return Py_BuildValue("l", Struct.fetcher.FetchNeeded()); + return Py_BuildValue("l", fetcher->FetchNeeded()); if(strcmp("PartialPresent",Name) == 0) - return Py_BuildValue("l", Struct.fetcher.PartialPresent()); + return Py_BuildValue("l", fetcher->PartialPresent()); if(strcmp("Items",Name) == 0) { PyObject *List = PyList_New(0); - for (pkgAcquire::ItemIterator I = Struct.fetcher.ItemsBegin(); - I != Struct.fetcher.ItemsEnd(); I++) + for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); + I != fetcher->ItemsEnd(); I++) { PyObject *Obj; Obj = CppOwnedPyObject_NEW(Self,&AcquireItemType,I); @@ -167,10 +166,10 @@ PyTypeObject PkgAcquireType = PyObject_HEAD_INIT(&PyType_Type) 0, // ob_size "Acquire", // tp_name - sizeof(CppOwnedPyObject), // tp_basicsize + sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppDealloc, // tp_dealloc 0, // tp_print AcquireAttr, // tp_getattr 0, // tp_setattr @@ -199,8 +198,8 @@ PyObject *GetAcquire(PyObject *Self,PyObject *Args) fetcher = new pkgAcquire(); } - CppOwnedPyObject *FetcherObj = - CppOwnedPyObject_NEW(0,&PkgAcquireType, *fetcher); + CppPyObject *FetcherObj = + CppPyObject_NEW(&PkgAcquireType, fetcher); return FetcherObj; } diff --git a/python/acquire.h b/python/acquire.h deleted file mode 100644 index 1afcfff3..00000000 --- a/python/acquire.h +++ /dev/null @@ -1,6 +0,0 @@ -#include - -struct PkgAcquireStruct -{ - pkgAcquire fetcher; -}; diff --git a/python/pkgmanager.cc b/python/pkgmanager.cc index ce6d35a8..bcd4c45b 100644 --- a/python/pkgmanager.cc +++ b/python/pkgmanager.cc @@ -8,14 +8,13 @@ #include "generic.h" #include "apt_pkgmodule.h" -#include "acquire.h" #include "pkgrecords.h" #include #include #include #include - +#include #include @@ -30,11 +29,11 @@ static PyObject *PkgManagerGetArchives(PyObject *Self,PyObject *Args) &PkgRecordsType, &recs) == 0) return 0; - PkgAcquireStruct &s_fetcher = GetCpp(fetcher); + pkgAcquire *s_fetcher = GetCpp(fetcher); pkgSourceList *s_list = GetCpp(list); PkgRecordsStruct &s_records = GetCpp(recs); - bool res = pm->GetArchives(&s_fetcher.fetcher, s_list, + bool res = pm->GetArchives(s_fetcher, s_list, &s_records.Records); return HandleErrors(Py_BuildValue("b",res)); diff --git a/python/sourcelist.cc b/python/sourcelist.cc index 2d65062c..4eed21a7 100644 --- a/python/sourcelist.cc +++ b/python/sourcelist.cc @@ -30,7 +30,6 @@ static PyObject *PkgSourceListFindIndex(PyObject *Self,PyObject *Args) static char *doc_PkgSourceListReadMainList = "xxx"; static PyObject *PkgSourceListReadMainList(PyObject *Self,PyObject *Args) { - // PkgSourceListStruct &Struct = GetCpp(Self); pkgSourceList *list = GetCpp(Self); bool res = list->ReadMainList(); -- cgit v1.2.3 From 0a8598b3b728e0445f84186b3f302734d1347371 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 28 Nov 2005 12:27:33 +0000 Subject: * rewrote the cache.commit() code to make use of the new pkgAcquire/pkgPackageManager interface --- apt/cache.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++---- debian/changelog | 3 ++ debian/control | 2 +- python/acquire.cc | 7 +++-- 4 files changed, 88 insertions(+), 9 deletions(-) (limited to 'python/acquire.cc') diff --git a/apt/cache.py b/apt/cache.py index 1fb128a3..ab3775aa 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -21,8 +21,9 @@ import apt_pkg from apt import Package -from apt.progress import OpTextProgress -from UserDict import UserDict +import apt.progress +import os +import sys class Cache(object): """ Dictionary-like package cache @@ -48,6 +49,8 @@ class Cache(object): self._cache = apt_pkg.GetCache(progress) self._depcache = apt_pkg.GetDepCache(self._cache) self._records = apt_pkg.GetPkgRecords(self._cache) + self._list = apt_pkg.GetPkgSourceList() + self._list.ReadMainList() self._dict = {} # build the packages dict @@ -115,9 +118,71 @@ class Cache(object): else: self._cache.Update(fetchProgress); - def commit(self, fprogress, iprogress): + def _fetchArchives(self, pm, fetchProgress): + """ fetch the needed archives """ + + # get lock + lockfile = apt_pkg.Config.FindDir("Dir::Cache::Archives") + "lock" + lock = apt_pkg.GetLock(lockfile) + if lock < 0: + raise IOError, "Failed to lock %s" % lockfile + fetcher = apt_pkg.GetAcquire(fetchProgress) + # this may as well throw a SystemError exception + if not pm.GetArchives(fetcher, self._list, self._records): + return False + # do the actual fetching + res = fetcher.Run() + if res == fetcher.ResultFailed: + return False + + # now check the result (this is the code from apt-get.cc) + failed = False + transient = False + errMsg = "" + for item in fetcher.Items: + if item.StatDone and item.Complete: + continue + if item.StatIdle: + transient = True + continue + errMsg += "Failed to fetch %s %s\n" % (item.DescURI,item.ErrorText) + failed = True + + # we raise a exception if the download failed + if failed: + raise IOError, errMsg + + # cleanup + fetcher.Shutdown() + os.close(lock) + return res + + def installArchives(self, pm, installProgress): + return pm.DoInstall() + + def commit(self, fetchProgress=None, installProgress=None): """ Apply the marked changes to the cache """ - return self._depcache.Commit(fprogress, iprogress) + # FIXME: + # use the new acquire/pkgmanager interface here, + # raise exceptions when a download or install fails + # and send proper error strings to the application. + # Current a failed download will just display "error" + # which is less than optimal! + + while True: + pm = apt_pkg.GetPackageManager(self._depcache) + + # fetch archives first + res = self._fetchArchives(pm, fetchProgress) + + # then install + res = self.installArchives(pm, installProgress) + if res == pm.ResultCompleted: + break + if res == pm.ResultFailed: + raise SystemError, "install failed" + + return res # cache changes def cachePostChange(self): @@ -228,7 +293,7 @@ def cache_post_changed(): if __name__ == "__main__": print "Cache self test" apt_pkg.init() - c = Cache(OpTextProgress()) + c = Cache(apt.progress.OpTextProgress()) c.connect("cache_pre_change", cache_pre_changed) c.connect("cache_post_change", cache_post_changed) print c.has_key("aptitude") @@ -246,6 +311,16 @@ if __name__ == "__main__": #print p.name x = p.name + + # see if fetching works + for dir in ["/tmp/pytest", "/tmp/pytest/partial"]: + if not os.path.exists(dir): + os.mkdir(dir) + apt_pkg.Config.Set("Dir::Cache::Archives","/tmp/pytest") + pm = apt_pkg.GetPackageManager(c._depcache) + c._fetchArchives(pm, apt.progress.TextFetchProgress()) + #sys.exit(1) + print "Testing filtered cache (argument is old cache)" f = FilteredCache(c) f.cache.connect("cache_pre_change", cache_pre_changed) diff --git a/debian/changelog b/debian/changelog index 9ffd5b12..1a4cb0a8 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,6 +3,9 @@ python-apt (0.6.15) unstable; urgency=low * fix a invalid return from cache.commit(), fail if a download failed * apt.Package.candidateOrigin returns a class now * added pkgAcquire, pkgPackageManager and a example (acquire.py) + * tightend build-dependencies for new apt and the c++ transition + * rewrote cache.Commit() and make it raise proper Exception if stuff + goes wrong -- Michael Vogt Thu, 17 Nov 2005 13:00:14 +0100 diff --git a/debian/control b/debian/control index 8fb8a669..eec81bd3 100644 --- a/debian/control +++ b/debian/control @@ -4,7 +4,7 @@ Priority: optional Maintainer: APT Development Team Uploaders: Matt Zimmerman , Michael Vogt Standards-Version: 3.6.1.1 -Build-Depends: debhelper (>= 4.2.28), libapt-pkg-dev (>= 0.6.40.1), apt-utils, python-dev, python2.4-dev, python2.3-dev +Build-Depends: debhelper (>= 4.2.28), libapt-pkg-dev (>= 0.6.43ubuntu1), apt-utils, python-dev, python2.4-dev, python2.3-dev Package: python-apt Architecture: all diff --git a/python/acquire.cc b/python/acquire.cc index 1bbed72a..2bcd94c1 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -94,11 +94,11 @@ static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) { pkgAcquire *fetcher = GetCpp(Self); - if (PyArg_ParseTuple(Args, "") == 0) + int pulseInterval = 500000; + if (PyArg_ParseTuple(Args, "|i", &pulseInterval) == 0) return 0; - //FIXME: add pulse interval here - pkgAcquire::RunResult run = fetcher->Run(); + pkgAcquire::RunResult run = fetcher->Run(pulseInterval); return HandleErrors(Py_BuildValue("i",run)); } @@ -119,6 +119,7 @@ static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) static PyMethodDef PkgAcquireMethods[] = { {"Run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, + {"Shutdown",PkgAcquireShutdown, METH_VARARGS,"Shutdown the fetcher"}, {} }; -- cgit v1.2.3 From a31cea47fc17cb55016fd52a6ab6bafe5a0857a8 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 1 Dec 2005 13:36:35 +0000 Subject: * pkgAcqFile wrapper added --- debian/changelog | 7 ++++++ doc/examples/acquire.py | 7 ++++++ python/acquire.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ python/apt_pkgmodule.cc | 1 + python/apt_pkgmodule.h | 1 + 5 files changed, 83 insertions(+) (limited to 'python/acquire.cc') diff --git a/debian/changelog b/debian/changelog index efd6cdca..402ff2dc 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +python-apt (0.6.16) unstable; urgency=low + + * added GetPkgAcqFile to queue individual file downloads with the + system + + -- Michael Vogt Thu, 1 Dec 2005 14:01:39 +0100 + python-apt (0.6.15) unstable; urgency=low * rewrote cache.Commit() and make it raise proper Exception if stuff diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 72d0c74e..2736ad9c 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -34,6 +34,13 @@ pm = apt_pkg.GetPackageManager(depcache) print pm print fetcher +af = apt_pkg.GetPkgAcqFile(fetcher, + uri="ftp://ftp.debian.org/debian/dists/README", + descr="sample descr", + destFile="/tmp/lala2") +fetcher.Run() +sys.exit(1) + pm.GetArchives(fetcher,list,recs) for item in fetcher.Items: diff --git a/python/acquire.cc b/python/acquire.cc index 2bcd94c1..03606ef2 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -208,4 +208,71 @@ PyObject *GetAcquire(PyObject *Self,PyObject *Args) + +// pkgAcquireFile + +static PyObject *AcquireFileAttr(PyObject *Self,char *Name) +{ + pkgAcqFile *acqFile = GetCpp(Self); + + PyErr_SetString(PyExc_AttributeError,Name); + return 0; +} + + + + +PyTypeObject PkgAcquireFileType = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, // ob_size + "pkgAcquireFile", // tp_name + sizeof(CppPyObject),// tp_basicsize + 0, // tp_itemsize + // Methods + CppDealloc, // 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 +}; + +PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) +{ + PyObject *pyfetcher; + char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; + int size; + 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(pyfetcher); + pkgAcqFile *af = new pkgAcqFile(fetcher, // owner + uri, // uri + md5, // md5 + size, // size + descr, // descr + shortDescr, // shortdescr + destDir, // destdir + destFile // destfile + ); + CppPyObject *AcqFileObj = CppPyObject_NEW(&PkgAcquireFileType); + AcqFileObj->Object = af; + + return AcqFileObj; +} + + /*}}}*/ diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index 5731cae4..e73628c3 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -398,6 +398,7 @@ static PyMethodDef methods[] = // Acquire {"GetAcquire",GetAcquire,METH_VARARGS,"GetAcquire() -> Acquire"}, + {"GetPkgAcqFile",(PyCFunction)GetPkgAcqFile,METH_KEYWORDS|METH_VARARGS,"GetPkgAcquireFile() -> pkgAcquireFile"}, // PkgManager {"GetPackageManager",GetPkgManager,METH_VARARGS,"GetPackageManager() -> PackageManager"}, diff --git a/python/apt_pkgmodule.h b/python/apt_pkgmodule.h index 93112f89..84b0069f 100644 --- a/python/apt_pkgmodule.h +++ b/python/apt_pkgmodule.h @@ -75,6 +75,7 @@ PyObject *GetCdrom(PyObject *Self,PyObject *Args); // acquire extern PyTypeObject PkgAcquireType; PyObject *GetAcquire(PyObject *Self,PyObject *Args); +PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject *kwds); // packagemanager extern PyTypeObject PkgManagerType; -- cgit v1.2.3 From 1aff3d1820f8efe90d811d96c0f5b3806f20d325 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 1 Dec 2005 14:00:27 +0000 Subject: * fix uninitialized size member in pkgAcqFile --- python/acquire.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python/acquire.cc') diff --git a/python/acquire.cc b/python/acquire.cc index 03606ef2..9d30e663 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -246,7 +246,7 @@ PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) { PyObject *pyfetcher; char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; - int size; + int size = 0; uri = md5 = descr = shortDescr = destDir = destFile = ""; char * kwlist[] = {"owner","uri", "md5", "size", "descr", "shortDescr", -- cgit v1.2.3 From 712fd8a6948cc3f11de57f7abf598a8df18a646b Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 6 Dec 2005 13:13:46 +0000 Subject: * bugfix in the acquire.FetchNeeded code --- doc/examples/acquire.py | 11 +++++++++++ python/acquire.cc | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'python/acquire.cc') diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 2736ad9c..516a3d6d 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -16,6 +16,17 @@ recs = apt_pkg.GetPkgRecords(cache) list = apt_pkg.GetPkgSourceList() list.ReadMainList() +# show the amount fetch needed for a dist-upgrade +depcache.Upgrade(True) +progress = apt.progress.TextFetchProgress() +fetcher = apt_pkg.GetAcquire(progress) +pm = apt_pkg.GetPackageManager(depcache) +pm.GetArchives(fetcher,list,recs) +print "%s (%s)" % (apt_pkg.SizeToStr(fetcher.FetchNeeded), fetcher.FetchNeeded) + +for pkg in cache.Packages: + depcache.MarkKeep(pkg) + try: os.mkdir("/tmp/pyapt-test") os.mkdir("/tmp/pyapt-test/partial") diff --git a/python/acquire.cc b/python/acquire.cc index 9d30e663..97064c43 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -129,11 +129,11 @@ static PyObject *AcquireAttr(PyObject *Self,char *Name) pkgAcquire *fetcher = GetCpp(Self); if(strcmp("TotalNeeded",Name) == 0) - return Py_BuildValue("l", fetcher->TotalNeeded()); + return Py_BuildValue("d", fetcher->TotalNeeded()); if(strcmp("FetchNeeded",Name) == 0) - return Py_BuildValue("l", fetcher->FetchNeeded()); + return Py_BuildValue("d", fetcher->FetchNeeded()); if(strcmp("PartialPresent",Name) == 0) - return Py_BuildValue("l", fetcher->PartialPresent()); + return Py_BuildValue("d", fetcher->PartialPresent()); if(strcmp("Items",Name) == 0) { PyObject *List = PyList_New(0); -- cgit v1.2.3 From cce2b71c2fd64c8587238178c27a049ec450ce07 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 7 Dec 2005 10:40:14 +0000 Subject: * make pkgAcqFile use the old interface so that we don't depend on a new apt and still work with breezys apt --- debian/changelog | 2 +- debian/control | 2 +- doc/examples/acquire.py | 28 +++++++++++++++++++++++----- python/acquire.cc | 12 ++++-------- 4 files changed, 29 insertions(+), 15 deletions(-) (limited to 'python/acquire.cc') diff --git a/debian/changelog b/debian/changelog index 2c202597..5f4c0856 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,7 +1,7 @@ python-apt (0.6.16) unstable; urgency=low * added GetPkgAcqFile to queue individual file downloads with the - system + system (dosn't make use of the improved pkgAcqFile yet) * added SourceList.GetIndexes() * rewrote apt.cache.update() to use the improved aquire interface * apt/ API change: apt.Package.candidateOrigin returns a list of origins diff --git a/debian/control b/debian/control index 472712d9..bfa67c67 100644 --- a/debian/control +++ b/debian/control @@ -4,7 +4,7 @@ Priority: optional Maintainer: APT Development Team Uploaders: Matt Zimmerman , Michael Vogt Standards-Version: 3.6.1.1 -Build-Depends: debhelper (>= 4.2.28), libapt-pkg-dev (>= 0.6.43), apt-utils, python-dev, python2.4-dev, python2.3-dev +Build-Depends: debhelper (>= 4.2.28), libapt-pkg-dev (>= 0.6.40), apt-utils, python-dev, python2.4-dev, python2.3-dev Package: python-apt Architecture: all diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py index 516a3d6d..a0790c98 100644 --- a/doc/examples/acquire.py +++ b/doc/examples/acquire.py @@ -2,6 +2,28 @@ import apt import apt_pkg import os import sys +import tempfile + +def get_file(fetcher, uri, destFile): + cwd = os.getcwd() + # create a temp dir + dir = tempfile.mkdtemp() + os.chdir(dir) + # get the file + af = apt_pkg.GetPkgAcqFile(fetcher, + uri=uri, + descr="sample descr") + res = fetcher.Run() + if res != fetcher.ResultContinue: + os.rmdir(dir) + os.chdir(cwd) + return False + filename = os.path.basename(uri) + os.rename(dir+"/"+filename,destFile) + # cleanup + os.rmdir(dir) + os.chdir(cwd) + return True apt_pkg.init() @@ -45,11 +67,7 @@ pm = apt_pkg.GetPackageManager(depcache) print pm print fetcher -af = apt_pkg.GetPkgAcqFile(fetcher, - uri="ftp://ftp.debian.org/debian/dists/README", - descr="sample descr", - destFile="/tmp/lala2") -fetcher.Run() +get_file(fetcher, "ftp://ftp.debian.org/debian/dists/README", "/tmp/lala") sys.exit(1) pm.GetArchives(fetcher,list,recs) diff --git a/python/acquire.cc b/python/acquire.cc index 97064c43..65f8f2d7 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -250,12 +250,11 @@ PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) uri = md5 = descr = shortDescr = destDir = destFile = ""; char * kwlist[] = {"owner","uri", "md5", "size", "descr", "shortDescr", - "destDir", "destFile", NULL}; + NULL}; - if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist, + if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|siss", kwlist, &PkgAcquireType, &pyfetcher, &uri, &md5, - &size, &descr, &shortDescr, &destDir, - &destFile) == 0) + &size, &descr, &shortDescr) == 0) return 0; pkgAcquire *fetcher = GetCpp(pyfetcher); @@ -264,10 +263,7 @@ PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) md5, // md5 size, // size descr, // descr - shortDescr, // shortdescr - destDir, // destdir - destFile // destfile - ); + shortDescr); // short-desc CppPyObject *AcqFileObj = CppPyObject_NEW(&PkgAcquireFileType); AcqFileObj->Object = af; -- cgit v1.2.3