]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Allow to automatically remove a repository when no longer needed
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 10:09:04 +0000 (11:09 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 10:09:04 +0000 (11:09 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/repo.c
src/_pakfire/repo.h

index 3e20e839eb2b7860df8637de034f06efd1e362e0..6ddc75005b8f7dba188985bc6e2355fb0d327633 100644 (file)
@@ -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,
index d160b8926d97e5dc2d926dda652223dd74371b21..b517a3e59f01a01b7a6b98cb5b55376912c66dfc 100644 (file)
@@ -30,6 +30,7 @@
 typedef struct {
     PyObject_HEAD
     PakfireRepo repo;
+    int clean;
 } RepoObject;
 
 extern PyTypeObject RepoType;