Skip to content

Commit

Permalink
Add UNIX wildcards support
Browse files Browse the repository at this point in the history
Allowing user to select qubes using UNIX like wildcards
  • Loading branch information
alimirjamali committed Nov 15, 2024
1 parent 886364e commit 86a7a1e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
40 changes: 40 additions & 0 deletions qubesadmin/tests/tools/qvm_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,46 @@ def test_103_list_all(self):
'dom0 Running TestVM - - -\n'
'test-vm Running TestVM - - -\n')

def test_104_wildcards(self):
app = TestApp()
app.domains = TestVMCollection(
[
('dom0', TestVM('dom0')),
('debian-13', TestVM('debian-13')),
('debian-13-minimal', TestVM('debian-13-minimal')),
('debian-13-xfce', TestVM('debian-13-xfce')),
('debian-sid', TestVM('debian-sid')),
('fedora-41', TestVM('fedora-41')),
('fedora-41-minimal', TestVM('fedora-41-minimal')),
('fedora-41-xfce', TestVM('fedora-41-xfce')),
('fedora-rawhide', TestVM('fedora-rawhide')),
]
)
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--raw-list', 'fedora*'], app=app)
self.assertEqual(stdout.getvalue(),
'fedora-41\nfedora-41-minimal\nfedora-41-xfce\nfedora-rawhide\n')

with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--raw-list', '*minimal'], app=app)
self.assertEqual(stdout.getvalue(),
'debian-13-minimal\nfedora-41-minimal\n')

with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--raw-list', '????'], app=app)
self.assertEqual(stdout.getvalue(),
'dom0\n')

with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--raw-list', '??????-[rs]*'], app=app)
self.assertEqual(stdout.getvalue(),
'debian-sid\nfedora-rawhide\n')

with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--raw-list', '??????-[!14s]*'], app=app)
self.assertEqual(stdout.getvalue(),
'fedora-rawhide\n')

def test_110_network_tree(self):
app = TestApp()
app.domains = TestVMCollection(
Expand Down
13 changes: 11 additions & 2 deletions qubesadmin/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from __future__ import print_function

import argparse
import fnmatch
import importlib
import logging
import os
Expand All @@ -38,7 +39,6 @@
#: constant returned when some action should be performed on all qubes
VM_ALL = object()


class QubesAction(argparse.Action):
''' Interface providing a convinience method to be called, after
`namespace.app` is instantiated.
Expand Down Expand Up @@ -175,7 +175,16 @@ def parse_qubes_app(self, parser, namespace):
except KeyError:
parser.error('no such domain: {!r}'.format(vm_name))
else:
for vm_name in getattr(namespace, self.dest):
destinations = set()
for destination in getattr(namespace, self.dest):
if any(wildcard in destination for wildcard in '*?[!]'):
for domain in app.domains:
if fnmatch.fnmatch(domain.name, destination):
destinations.add(domain.name)
else:
destinations.add(destination)

for vm_name in destinations:
try:
namespace.domains += [app.domains[vm_name]]
except KeyError:
Expand Down

0 comments on commit 86a7a1e

Please sign in to comment.