From 138bc243aa592cd628f6e41a1b3c35f1f01f3c37 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Tue, 8 Aug 2017 16:48:40 +0100 Subject: [Plugin] add executable command enablement checks Add a new list/tuple member to the Plugin class that contains a list of executable commands that will enable the plugin if any are present. For example, a plugin: class MyPlugin(Plugin, RedHatPlugin): commands = ('mycmd1', 'mycmd2') Will be automatically enabled if either 'mycmd1' or 'mycmd2' is present and executable in the policy defined PATH for the run. Related: #1051. Signed-off-by: Bryn M. Reeves --- sos/plugins/__init__.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index a31297f6..2d6f6ff5 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -19,7 +19,7 @@ from __future__ import with_statement from sos.utilities import (sos_get_command_output, import_module, grep, - fileobj, tail) + fileobj, tail, is_executable) import os import glob import re @@ -111,6 +111,7 @@ class Plugin(object): version = 'unversioned' packages = () files = () + commands = () archive = None profiles = () sysroot = '/' @@ -865,23 +866,31 @@ class Plugin(object): def check_enabled(self): """This method will be used to verify that a plugin should execute - given the condition of the underlying environment. The default - implementation will return True if neither class.files or - class.packages is specified. If either are specified the plugin will - check for the existence of any of the supplied files or packages and - return True if any exist. It is encouraged to override this method if - this behavior isn't applicable. + given the condition of the underlying environment. + + The default implementation will return True if none of class.files, + class.packages, nor class.commands is specified. If any of these is + specified the plugin will check for the existence of any of the + corresponding paths, packages or commands and return True if any + are present. + + For plugins with more complex enablement checks this method may be + overridden. """ # some files or packages have been specified for this package - if self.files or self.packages: + if any([self.files, self.packages, self.commands]): if isinstance(self.files, six.string_types): self.files = [self.files] if isinstance(self.packages, six.string_types): self.packages = [self.packages] + if isinstance(self.commands, six.string_types): + self.commands = [self.commands] + return (any(os.path.exists(fname) for fname in self.files) or - any(self.is_installed(pkg) for pkg in self.packages)) + any(self.is_installed(pkg) for pkg in self.packages) or + any(is_executable(cmd) for cmd in self.commands)) return True def default_enabled(self): -- cgit