From f536935632b607bea7d375484cf29149a14833be Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Wed, 1 Jul 2015 17:32:24 +0200 Subject: [PATCH] qubes/tools: add --verbose and --quiet to tools --- qubes/tools/__init__.py | 28 ++++++++++++++++++++++++++++ qubes/tools/qubes_create.py | 1 + qubes/tools/qvm_create.py | 1 + qubes/tools/qvm_ls.py | 1 + 4 files changed, 31 insertions(+) diff --git a/qubes/tools/__init__.py b/qubes/tools/__init__.py index 98517757..964ea262 100644 --- a/qubes/tools/__init__.py +++ b/qubes/tools/__init__.py @@ -29,6 +29,8 @@ import argparse import importlib import os +import qubes.log + class PropertyAction(argparse.Action): '''Action for argument parser that stores a property.''' @@ -98,11 +100,21 @@ def get_parser_base(want_force_root=False, **kwargs): action='store', help='Qubes OS store file') + parser.add_argument('--verbose', '-v', + action='count', + help='increase verbosity') + + parser.add_argument('--quiet', '-q', + action='count', + help='decrease verbosity') + if want_force_root: parser.add_argument('--force-root', action='store_true', default=False, help='Force to run as root.') + parser.set_defaults(verbose=1, quiet=0) + return parser @@ -145,3 +157,19 @@ def dont_run_as_root(parser, args): if euid == 0 and not args.force_root: parser.error('refusing to run as root; add --force-root to override') + + +def set_verbosity(parser, args): + '''Apply a verbosity setting. + + This is done by configuring global logging. + :param argparse.ArgumentParser parser: command parser + :param argparse.Namespace args: args as parsed by parser + ''' + + verbose = args.verbose - args.quiet + + if verbose >= 2: + qubes.log.enable_debug() + elif verbose >= 1: + qubes.log.enable() diff --git a/qubes/tools/qubes_create.py b/qubes/tools/qubes_create.py index 78cef80c..c9cac254 100644 --- a/qubes/tools/qubes_create.py +++ b/qubes/tools/qubes_create.py @@ -40,6 +40,7 @@ def main(args=None): ''' args = parser.parse_args(args) + qubes.tools.set_verbosity(parser, args) app = qubes.Qubes.create_empty_store(args.xml) return True diff --git a/qubes/tools/qvm_create.py b/qubes/tools/qvm_create.py index 61851822..caf6e982 100644 --- a/qubes/tools/qvm_create.py +++ b/qubes/tools/qvm_create.py @@ -72,6 +72,7 @@ parser.add_argument('name', metavar='VMNAME', def main(): args = parser.parse_args() + qubes.tools.set_verbosity(parser, args) qubes.tools.dont_run_as_root(parser, args) if 'label' not in args.properties: diff --git a/qubes/tools/qvm_ls.py b/qubes/tools/qvm_ls.py index 3b6495ef..7d577aac 100644 --- a/qubes/tools/qvm_ls.py +++ b/qubes/tools/qvm_ls.py @@ -607,6 +607,7 @@ def main(args=None): parser = get_parser() args = parser.parse_args(args) + qubes.tools.set_verbosity(parser, args) app = qubes.Qubes(args.xml) if args.fields: