diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2019-09-15 11:09:36 -0400 |
---|---|---|
committer | Bryn M. Reeves <bmr@redhat.com> | 2019-10-14 12:26:40 +0100 |
commit | 4e004eeb1eba00dd18327268bed1468c9cd367ae (patch) | |
tree | c5c9edc85bb8443b38b7d6b012bbc16967de609b | |
parent | cb2d1a0e67abec3cad327c3e6aa7e18c25316086 (diff) | |
download | sos-4e004eeb1eba00dd18327268bed1468c9cd367ae.tar.gz |
[Predicates] Allow predicates to check package installation
Extends the SoSPredicate class to accept and check a list of packages to
gate command or file collection on if those packages are present on the
system.
Resolves: #1785
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r-- | sos/plugins/__init__.py | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index e1a98aad..a0b291be 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -119,6 +119,9 @@ class SoSPredicate(object): #: Services enablement list services = [] + #: Package presence list + packages = [] + def __str(self, quote=False, prefix="", suffix=""): """Return a string representation of this SoSPredicate with optional prefix, suffix and value quoting. @@ -132,7 +135,11 @@ class SoSPredicate(object): services = self.services services = [quotes % s for s in services] if quote else services - pstr += "services=[%s]" % (",".join(services)) + pstr += "services=[%s] " % (",".join(services)) + + pkgs = self.packages + pkgs = [quotes % p for p in pkgs] if quote else pkgs + pstr += "packages=[%s]" % (",".join(pkgs)) return prefix + pstr + suffix @@ -174,26 +181,38 @@ class SoSPredicate(object): else: return all(_svcs) + def _eval_packages(self): + if not self.packages: + return True + + _pkgs = [self._owner.is_installed(p) for p in self.packages] + + if self.required['packages'] == 'any': + return any(_pkgs) + else: + return all(_pkgs) + def __nonzero__(self): """Predicate evaluation hook. """ # Null predicate? - if not any([self.kmods, self.services, self.dry_run]): + if not any([self.kmods, self.services, self.packages, self.dry_run]): return True - return ((self._eval_kmods() and self._eval_services()) and not - self.dry_run) + return ((self._eval_kmods() and self._eval_services() and + self._eval_packages()) and not self.dry_run) def __init__(self, owner, dry_run=False, kmods=[], services=[], - required={}): + packages=[], required={}): """Initialise a new SoSPredicate object. """ self._owner = owner self.kmods = list(kmods) self.services = list(services) + self.packages = list(packages) self.dry_run = dry_run | self._owner.commons['cmdlineopts'].dry_run - self.required = {'kmods': 'any', 'services': 'any'} + self.required = {'kmods': 'any', 'services': 'any', 'packages': 'any'} self.required.update({ k: v for k, v in required.items() if required[k] != self.required[k] |