Push most work of ts.addErase() over to python
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 2 Oct 2009 19:48:03 +0000 (22:48 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 2 Oct 2009 19:48:03 +0000 (22:48 +0300)
- minimize the stuff done in C: rpmtsAddEraseElement() only cares about
  header, so that's what we accept on C-level bindings
- on python side, handle db recno's and labels like we've always done,
  additionally accept match iterators and plain headers too

python/rpm/transaction.py
python/rpmts-py.c

index 9c8de0c..4c4bba7 100644 (file)
@@ -61,3 +61,24 @@ class TransactionSet(_rpm.ts):
         if not _rpm.ts.addInstall(self, header, key, upgrade):
             raise _rpm.error, "adding package to transaction failed"
         self._keyList.append(key)
+
+    def addErase(self, item):
+        hdrs = []
+        if isinstance(item, _rpm.hdr):
+            hdrs = [item]
+        elif isinstance(item, _rpm.mi):
+            hdrs = item
+        elif isinstance(item, int):
+            hdrs = self.dbMatch(_rpm.RPMDBI_PACKAGES, item)
+        elif isinstance(item, str):
+            hdrs = self.dbMatch(_rpm.RPMDBI_LABEL, item)
+        else:
+            raise TypeError, "invalid type %s" % type(item)
+
+        for h in hdrs:
+            if not _rpm.ts.addErase(self, h):
+                raise _rpm.error, "package not installed"
+
+        # garbage collection should take care but just in case...
+        if isinstance(hdrs, _rpm.mi):
+            del hdrs
index 39c183c..576de02 100644 (file)
@@ -183,42 +183,15 @@ rpmts_AddInstall(rpmtsObject * s, PyObject * args)
     return PyBool_FromLong((rc == 0));
 }
 
-/* TODO Permit finer control (i.e. not just --allmatches) of deleted elments.*/
 static PyObject *
-rpmts_AddErase(rpmtsObject * s, PyObject * args, PyObject * kwds)
+rpmts_AddErase(rpmtsObject * s, PyObject * args)
 {
-    PyObject * o;
-    int installed = 0;
-    rpmdbMatchIterator mi = NULL;
     Header h;
-    char * kwlist[] = {"name", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:AddErase", kwlist, &o))
+    if (!PyArg_ParseTuple(args, "O&:AddErase", hdrFromPyObject, &h))
         return NULL;
 
-    if (PyString_Check(o)) {
-       char * name = PyString_AsString(o);
-       mi = rpmtsInitIterator(s->ts, RPMDBI_LABEL, name, 0);
-    } else if (PyInt_Check(o)) {
-       uint32_t recno = PyInt_AsLong(o);
-       mi = rpmtsInitIterator(s->ts, RPMDBI_PACKAGES, &recno, sizeof(recno));
-    } else {
-       PyErr_SetString(PyExc_TypeError, "string or integer expected");
-       return NULL;
-    }
-
-    while ((h = rpmdbNextIterator(mi)) != NULL) {
-       installed++;
-       rpmtsAddEraseElement(s->ts, h, -1);
-    }
-    rpmdbFreeIterator(mi);
-    
-    if (installed) {
-       Py_RETURN_NONE;
-    } else {
-       PyErr_SetString(pyrpmError, "package not installed");
-       return NULL;
-    }
+    return PyBool_FromLong(rpmtsAddEraseElement(s->ts, h, -1) == 0);
 }
 
 static int