From 793b4f4f8d4dde8a46f943d78f6bcfeca9001573 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Sat, 26 May 2018 11:51:11 +0100 Subject: [sosreport] move SoSOptions from sos.sosreport to sos The options object now needs to be referenced in both the main sosreport script, and the policy classes: move its definition from the sos.sosreport module to sos to avoid recursive import errors between sosreport and policy. Signed-off-by: Bryn M. Reeves --- sos/__init__.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ sos/sosreport.py | 88 +---------------------------------------- 2 files changed, 118 insertions(+), 87 deletions(-) diff --git a/sos/__init__.py b/sos/__init__.py index 7d0f92b5..c6ec2486 100644 --- a/sos/__init__.py +++ b/sos/__init__.py @@ -16,6 +16,7 @@ gettext to internationalize messages. """ import gettext +from collections import deque __version__ = "3.5" @@ -30,3 +31,119 @@ def _default(msg): _sos = _default + +# Global option definitions +# These must be in the module itself in order to be available to both +# the sosreport and policy module (and to avoid recursive import errors). +# +# FIXME: these definitions make our main module a bit more bulky: the +# alternative is to place these in a new sos.options module. This may +# prove to be the best route long-term (as it could also contain an +# exported parsing routine, and all the command-line definitions). + +#: Names of all arguments +_arg_names = [ + 'all_logs', 'batch', 'build', 'case_id', 'chroot', 'compression_type', + 'config_file', 'debug', 'enableplugins', 'experimental', 'label', + 'list_plugins', 'list_profiles', 'log_size', 'noplugins', 'noreport', + 'onlyplugins', 'plugopts', 'profiles', 'quiet', 'sysroot', 'tmp_dir', + 'usealloptions', 'verbosity', 'verify' +] + +#: Arguments with non-zero default values +_arg_defaults = { + "log_size": 10, + "chroot": "auto", + "compression_type": "auto", +} + + +class SoSOptions(object): + list_plugins = False + noplugins = [] + enableplugins = [] + onlyplugins = [] + plugopts = [] + usealloptions = False + all_logs = False + log_size = _arg_defaults["log_size"] + batch = False + build = False + verbosity = 0 + verify = False + quiet = False + debug = False + case_id = "" + label = "" + profiles = deque() + list_profiles = False + config_file = "" + tmp_dir = "" + noreport = False + sysroot = None + chroot = _arg_defaults["chroot"] + compression_type = _arg_defaults["compression_type"] + experimental = False + threads = 4 + + def _copy_opt(self, opt, src): + if hasattr(src, opt): + setattr(self, opt, getattr(src, opt)) + + def _copy_opts(self, src): + for arg in _arg_names: + self._copy_opt(arg, src) + + def __init__(self, **kwargs): + """Initialise a new ``SoSOptions`` object from keyword arguments. + + Initialises the new object with values taken from keyword + arguments matching the names of ``SoSOptions`` attributes. + + A ``ValueError`` is raised is any of the supplied keyword + arguments does not correspond to a known ``SoSOptions` + attribute name. + + :param *kwargs: a list of ``SoSOptions`` keyword args. + :returns: the new ``SoSOptions`` object. + """ + for arg in kwargs.keys(): + if arg not in _arg_names: + raise ValueError("Unknown SoSOptions attribute: %s" % arg) + setattr(self, arg, kwargs[arg]) + + @classmethod + def from_args(cls, args): + """Initialise a new SoSOptions object from a ``Namespace`` + obtained by parsing command line arguments. + + :param args: parsed command line arguments + :returns: an initialised SoSOptions object + :returntype: SoSOptions + """ + opts = SoSOptions() + opts._copy_opts(args) + return opts + + def merge(self, src, replace=False): + """Merge another set of ``SoSOptions`` into this object. + + Merge two ``SoSOptions`` objects by setting unset or default + values to their value in the ``src`` object. + + :param src: the ``SoSOptions`` object to copy from + :param replace: ``True`` if non-default values should be + overwritten. + """ + + for arg in _arg_names: + if not hasattr(src, arg): + continue + if arg in _arg_defaults.keys(): + if replace or getattr(self, arg) == _arg_defaults[arg]: + self._copy_opt(arg, src) + else: + if replace or not getattr(self, arg): + self._copy_opt(arg, src) + +# vim: set et ts=4 sw=4 : diff --git a/sos/sosreport.py b/sos/sosreport.py index b89c036a..2f4625d9 100644 --- a/sos/sosreport.py +++ b/sos/sosreport.py @@ -34,6 +34,7 @@ from concurrent.futures import ThreadPoolExecutor, TimeoutError from sos import _sos as _ from sos import __version__ +from sos import _arg_names, _arg_defaults, SoSOptions import sos.policies from sos.archive import TarFileArchive from sos.reporting import (Report, Section, Command, CopiedFile, CreatedFile, @@ -199,22 +200,6 @@ class XmlReport(object): # valid modes for --chroot chroot_modes = ["auto", "always", "never"] -#: Names of all arguments -_arg_names = [ - 'all_logs', 'batch', 'build', 'case_id', 'chroot', 'compression_type', - 'config_file', 'debug', 'enableplugins', 'experimental', 'label', - 'list_plugins', 'list_profiles', 'log_size', 'noplugins', 'noreport', - 'onlyplugins', 'plugopts', 'profiles', 'quiet', 'sysroot', 'tmp_dir', - 'usealloptions', 'verbosity', 'verify' -] - -#: Arguments with non-zero default values -_arg_defaults = { - "log_size": 10, - "chroot": "auto", - "compression_type": "auto", -} - def _parse_args(args): """ Parse command line options and arguments""" @@ -320,77 +305,6 @@ def _parse_args(args): return parser.parse_args(args) -class SoSOptions(object): - list_plugins = False - noplugins = [] - enableplugins = [] - onlyplugins = [] - plugopts = [] - usealloptions = False - all_logs = False - log_size = 10 - batch = False - build = False - verbosity = 0 - verify = False - quiet = False - debug = False - case_id = "" - label = "" - profiles = deque() - list_profiles = False - config_file = "" - tmp_dir = "" - noreport = False - sysroot = None - chroot = 'auto' - compression_type = 'auto' - experimental = False - threads = 4 - - def _copy_opt(self, opt, src): - if hasattr(src, opt): - setattr(self, opt, getattr(src, opt)) - - def _copy_opts(self, src): - for arg in _arg_names: - self._copy_opt(arg, src) - - @classmethod - def from_args(cls, args): - """Initialise a new SoSOptions object from a ``Namespace`` - obtained by parsing command line arguments. - - :param args: parsed command line arguments - :returns: an initialised SoSOptions object - :returntype: SoSOptions - """ - opts = SoSOptions() - opts._copy_opts(args) - return opts - - def merge(self, src, replace=False): - """Merge another set of ``SoSOptions`` into this object. - - Merge two ``SoSOptions`` objects by setting unset or default - values to their value in the ``src`` object. - - :param src: the ``SoSOptions`` object to copy from - :param replace: ``True`` if non-default values should be - overwritten. - """ - - for arg in _arg_names: - if not hasattr(src, arg): - continue - if arg in _arg_defaults.keys(): - if replace or getattr(self, arg) == _arg_defaults[arg]: - self._copy_opt(arg, src) - else: - if replace or not getattr(self, arg): - self._copy_opt(arg, src) - - class SoSReport(object): """The main sosreport class""" -- cgit