]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add python bindings for cgroup_compare_cgroup()
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 7 Mar 2023 19:45:35 +0000 (12:45 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 3 Apr 2023 14:53:01 +0000 (08:53 -0600)
Add python bindings for comparing two Cgroup instances.  Perform
the usual python comparisons and also invoke cgroup_compare_cgroup()
to ensure the cgroups completely match.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 40b90e7b6ae3f9493db4f44e427349e3e53c05e5)

src/python/cgroup.pxd
src/python/libcgroup.pyx

index 1077544154c475d5d317f7efe17354b5819566a6..26446cfd1659302a6a712ece45bb4993575df55c 100644 (file)
@@ -103,4 +103,6 @@ cdef extern from "libcgroup.h":
                              const cgroup_systemd_scope_opts * const opts)
 
     void cgroup_set_default_systemd_cgroup()
+
+    int cgroup_compare_cgroup(cgroup *cgroup_a, cgroup *cgroup_b)
 # vim: set et ts=4 sw=4:
index 81456ace4bf1a0732176aa12811218d2d7da6ea8..d56cdd8aa83823bd43876116643e5742483b92af 100644 (file)
@@ -60,6 +60,18 @@ class Controller:
 
         return out_str
 
+    def __eq__(self, other):
+        if not isinstance(other, Controller):
+            return False
+
+        if self.name != other.name:
+            return False
+
+        if self.settings != other.settings:
+            return False
+
+        return True
+
 cdef class Cgroup:
     """ Python object representing a libcgroup cgroup """
     cdef cgroup.cgroup * _cgp
@@ -100,6 +112,24 @@ cdef class Cgroup:
 
         return out_str
 
+    def __eq__(self, other):
+        if not isinstance(other, Cgroup):
+            return False
+
+        if self.name != other.name:
+            return False
+
+        if self.version != other.version:
+            return False
+
+        if self.controllers != other.controllers:
+            return False
+
+        if not self.compare(other):
+            return False
+
+        return True
+
     @staticmethod
     def library_version():
         cdef const cgroup.cgroup_library_version * version
@@ -543,6 +573,26 @@ cdef class Cgroup:
         Cgroup.cgroup_init()
         cgroup.cgroup_set_default_systemd_cgroup()
 
+    cdef compare(self, Cgroup other):
+        """Compare this cgroup instance with another cgroup instance
+
+        Arguments:
+        other - other cgroup instance to be compared
+
+        Return:
+        Returns true if the cgroups are equal.  False otherwise
+
+        Description:
+        Invokes the libcgroup C function, cgroup_compare_cgroup().
+        cgroup_compare_cgroup() walks through the cgroup and compares the
+        cgroup, its controllers, and the values/settings within the
+        controller.
+        """
+        ret = cgroup.cgroup_compare_cgroup(self._cgp, other._cgp)
+        if ret == 0:
+            return True
+        else:
+            return False
 
     def __dealloc__(self):
         cgroup.cgroup_free(&self._cgp);