Implement vmname parsing

This commit is contained in:
Bahtiar `kalkin-` Gadimov 2016-05-18 17:18:54 +02:00
parent 3549a9d4ec
commit d4c74d210f
No known key found for this signature in database
GPG Key ID: 96ED3C3BA19C3DEE

View File

@ -205,7 +205,12 @@ class QubesArgumentParser(argparse.ArgumentParser):
:param bool want_app_no_instance: don't actually instantiate \ :param bool want_app_no_instance: don't actually instantiate \
:py:class:`qubes.Qubes` object, just add argument for custom xml file :py:class:`qubes.Qubes` object, just add argument for custom xml file
:param bool want_force_root: add ``--force-root`` option :param bool want_force_root: add ``--force-root`` option
:param bool want_vm: add ``VMNAME`` as first positional argument :param mixed vmname_nargs: The number of ``VMNAME`` arguments that should be
consumed. Values include:
- N (an integer) consumes N arguments (and produces a list)
- '?' consumes zero or one arguments
- '*' consumes zero or more arguments (and produces a list)
- '+' consumes one or more arguments (and produces a list)
*kwargs* are passed to :py:class:`argparser.ArgumentParser`. *kwargs* are passed to :py:class:`argparser.ArgumentParser`.
Currenty supported options: Currenty supported options:
@ -214,63 +219,39 @@ class QubesArgumentParser(argparse.ArgumentParser):
``--verbose`` and ``--quiet`` ``--verbose`` and ``--quiet``
''' '''
def __init__(self, def __init__(self, want_app=True, want_app_no_instance=False,
want_app=True, want_force_root=False, vmname_nargs=None, **kwargs):
want_app_no_instance=False,
want_force_root=False,
want_vm=False,
want_vm_optional=False,
want_vm_all=False,
**kwargs):
super(QubesArgumentParser, self).__init__(**kwargs) super(QubesArgumentParser, self).__init__(**kwargs)
self._want_app = want_app self._want_app = want_app
self._want_app_no_instance = want_app_no_instance self._want_app_no_instance = want_app_no_instance
self._want_force_root = want_force_root self._want_force_root = want_force_root
self._want_vm = want_vm self._vmname_nargs = vmname_nargs
self._want_vm_optional = want_vm_optional
self._want_vm_all = want_vm_all
if self._want_app: if self._want_app:
self.add_argument('--qubesxml', metavar='FILE', self.add_argument('--qubesxml', metavar='FILE', action='store',
action='store', dest='app', dest='app', help=argparse.SUPPRESS)
help=argparse.SUPPRESS)
self.add_argument('--verbose', '-v',
action='count',
help='increase verbosity')
self.add_argument('--quiet', '-q', self.add_argument('--verbose', '-v', action='count',
action='count', help='increase verbosity')
help='decrease verbosity')
self.add_argument('--quiet', '-q', action='count',
help='decrease verbosity')
if self._want_force_root: if self._want_force_root:
self.add_argument('--force-root', self.add_argument('--force-root', action='store_true',
action='store_true', default=False, default=False, help='force to run as root')
help='force to run as root')
if self._want_vm: if self._vmname_nargs in [argparse.ZERO_OR_MORE, argparse.ONE_OR_MORE]:
if self._want_vm_all: vm_name_group = VmNameGroup(self, self._vmname_nargs)
vmchoice = self.add_mutually_exclusive_group() self._mutually_exclusive_groups.append(vm_name_group)
vmchoice.add_argument('--all', elif self._vmname_nargs is not None:
action='store_const', const=VM_ALL, dest='vm', self.add_argument('VMNAME', nargs=self._vmname_nargs,
help='perform the action on all qubes') action=VmNameAction)
self.add_argument('--exclude',
action='append', default=[],
help='exclude the qube from --all')
nargs = '?'
else:
vmchoice = self
nargs = '?' if self._want_vm_optional else None
vmchoice.add_argument('vm', metavar='VMNAME',
action='store', nargs=nargs,
help='name of the domain')
self.set_defaults(verbose=1, quiet=0) self.set_defaults(verbose=1, quiet=0)
def parse_args(self, *args, **kwargs): def parse_args(self, *args, **kwargs):
namespace = super(QubesArgumentParser, self).parse_args(*args, **kwargs) namespace = super(QubesArgumentParser, self).parse_args(*args, **kwargs)
@ -278,30 +259,13 @@ class QubesArgumentParser(argparse.ArgumentParser):
self.set_qubes_verbosity(namespace) self.set_qubes_verbosity(namespace)
namespace.app = qubes.Qubes(namespace.app) namespace.app = qubes.Qubes(namespace.app)
if self._want_vm:
if self._want_vm_all:
if namespace.vm is VM_ALL:
namespace.vm = [vm for vm in namespace.app.domains
if vm.qid != 0 and vm.name not in namespace.exclude]
else:
if namespace.exclude:
self.error('--exclude can only be used with --all')
try:
namespace.vm = \
(namespace.app.domains[namespace.vm],)
except KeyError:
self.error(
'no such domain: {!r}'.format(namespace.vm))
else:
try:
namespace.vm = namespace.app.domains[namespace.vm]
except KeyError:
self.error('no such domain: {!r}'.format(namespace.vm))
if self._want_force_root: if self._want_force_root:
self.dont_run_as_root(namespace) self.dont_run_as_root(namespace)
for action in self._actions:
if issubclass(action.__class__, QubesAction):
action.parse_qubes_app(self, namespace)
return namespace return namespace
@ -328,9 +292,9 @@ class QubesArgumentParser(argparse.ArgumentParser):
self.error_runtime( self.error_runtime(
'refusing to run as root; add --force-root to override') 'refusing to run as root; add --force-root to override')
@staticmethod @staticmethod
def get_loglevel_from_verbosity(namespace): def get_loglevel_from_verbosity(namespace):
''' Return loglevel calculated from quiet and verbose arguments '''
return (namespace.quiet - namespace.verbose) * 10 + logging.WARNING return (namespace.quiet - namespace.verbose) * 10 + logging.WARNING