From: Michael Tremer Date: Sat, 8 Jun 2019 10:09:04 +0000 (+0100) Subject: _pakfire: Allow to automatically remove a repository when no longer needed X-Git-Tag: 0.9.28~1285^2~981 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88e3042a5ca0681d5a084237be41f2694c6276fe;p=pakfire.git _pakfire: Allow to automatically remove a repository when no longer needed Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/repo.c b/src/_pakfire/repo.c index 3e20e839e..6ddc75005 100644 --- a/src/_pakfire/repo.c +++ b/src/_pakfire/repo.c @@ -55,12 +55,14 @@ static void Repo_dealloc(RepoObject* self) { static int Repo_init(RepoObject* self, PyObject* args, PyObject* kwds) { PakfireObject* pakfire; const char* name; + int clean = 0; - if (!PyArg_ParseTuple(args, "O!s", &PakfireType, &pakfire, &name)) + if (!PyArg_ParseTuple(args, "O!s|i", &PakfireType, &pakfire, &name, &clean)) return -1; // Create a new repository self->repo = pakfire_repo_create(pakfire->pakfire, name); + self->clean = clean; return 0; } @@ -376,7 +378,46 @@ static PyObject* Repo_clean(RepoObject* self, PyObject* args) { Py_RETURN_NONE; } +static PyObject* Repo_enter(RepoObject* self) { + Py_INCREF(self); + + return (PyObject*)self; +} + +static PyObject* Repo_exit(RepoObject* self, PyObject* args) { + PyObject* type; + PyObject* value; + PyObject* traceback; + + if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback)) + return NULL; + + // Automatically cleanup the repository + if (self->clean) { + int r = pakfire_repo_clean(self->repo); + + if (r) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + } + + Py_RETURN_NONE; +} + static struct PyMethodDef Repo_methods[] = { + { + "__enter__", + (PyCFunction)Repo_enter, + METH_NOARGS, + NULL + }, + { + "__exit__", + (PyCFunction)Repo_exit, + METH_VARARGS, + NULL + }, { "cache_age", (PyCFunction)Repo_cache_age, diff --git a/src/_pakfire/repo.h b/src/_pakfire/repo.h index d160b8926..b517a3e59 100644 --- a/src/_pakfire/repo.h +++ b/src/_pakfire/repo.h @@ -30,6 +30,7 @@ typedef struct { PyObject_HEAD PakfireRepo repo; + int clean; } RepoObject; extern PyTypeObject RepoType;