From: Tom Hromatka Date: Tue, 7 Mar 2023 19:45:35 +0000 (-0700) Subject: python: Add python bindings for cgroup_compare_cgroup() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=01c75a68678f940f1fdd4acb6926cd23df75b177;p=thirdparty%2Flibcgroup.git python: Add python bindings for cgroup_compare_cgroup() 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 (cherry picked from commit 40b90e7b6ae3f9493db4f44e427349e3e53c05e5) --- diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 10775441..26446cfd 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -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: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index 81456ace..d56cdd8a 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -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);