From 055b404f13e2b2ff86ac2b2dee0dfc42575b9327 Mon Sep 17 00:00:00 2001 From: Simon Zimmermann Date: Sat, 18 Jan 2014 10:50:24 +0100 Subject: [PATCH 1/2] Correctly compare values as returned from 'sysctl -e -n' --- library/system/sysctl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/library/system/sysctl b/library/system/sysctl index 59b92eb6f4..0039928809 100644 --- a/library/system/sysctl +++ b/library/system/sysctl @@ -148,7 +148,7 @@ class SysctlModule(object): if self.args['sysctl_set']: if self.proc_value is None: self.changed = True - elif self.proc_value != self.args['value']: + elif self._compare_values(self.proc_value, self.args['value']): self.changed = True self.set_proc = True @@ -161,6 +161,21 @@ class SysctlModule(object): if self.set_proc: self.set_token_value(self.args['name'], self.args['value']) + def _compare_values(self, a, b): + """Expects two string values. It will split the string by whitespace + and compare each value. It will return True if both lists are the same, + contain the same elements and the same order.""" + if a is None or b is None: + return False + + a = a.split() + b = b.split() + + if len(a) != len(b): + return False + + return len([i for i, j in zip(a, b) if i == j]) != len(a) + # ============================================================== # SYSCTL COMMAND MANAGEMENT # ============================================================== From 3db808c840d0da29c0a555fc2385172583b2ee83 Mon Sep 17 00:00:00 2001 From: Simon Zimmermann Date: Sat, 18 Jan 2014 11:04:15 +0100 Subject: [PATCH 2/2] correctly compare the values, better func name --- library/system/sysctl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/system/sysctl b/library/system/sysctl index 0039928809..85fe4151d3 100644 --- a/library/system/sysctl +++ b/library/system/sysctl @@ -148,7 +148,7 @@ class SysctlModule(object): if self.args['sysctl_set']: if self.proc_value is None: self.changed = True - elif self._compare_values(self.proc_value, self.args['value']): + elif not self._values_is_equal(self.proc_value, self.args['value']): self.changed = True self.set_proc = True @@ -161,7 +161,7 @@ class SysctlModule(object): if self.set_proc: self.set_token_value(self.args['name'], self.args['value']) - def _compare_values(self, a, b): + def _values_is_equal(self, a, b): """Expects two string values. It will split the string by whitespace and compare each value. It will return True if both lists are the same, contain the same elements and the same order.""" @@ -174,7 +174,7 @@ class SysctlModule(object): if len(a) != len(b): return False - return len([i for i, j in zip(a, b) if i == j]) != len(a) + return len([i for i, j in zip(a, b) if i == j]) == len(a) # ============================================================== # SYSCTL COMMAND MANAGEMENT