From 9e65d82617534fd3dbfa2fdb00d8d87a05305ff4 Mon Sep 17 00:00:00 2001 From: Ali Mirjamali Date: Sun, 17 Nov 2024 22:17:50 +0330 Subject: [PATCH] Add `--version` CLI option resolves: https://github.com/QubesOS/qubes-issues/issues/9585 --- qubesmanager/backup.py | 17 +++++++++++++++++ qubesmanager/bootfromdevice.py | 12 +++++++++++- qubesmanager/clone_vm.py | 10 +++++++++- qubesmanager/create_new_vm.py | 10 +++++++++- qubesmanager/qube_manager.py | 18 ++++++++++++++++++ qubesmanager/qvm_template_gui.py | 17 +++++++++++++++++ qubesmanager/restore.py | 15 ++++++++++++--- qubesmanager/settings.py | 11 ++++++++++- qubesmanager/template_manager.py | 19 +++++++++++++++++++ 9 files changed, 122 insertions(+), 7 deletions(-) diff --git a/qubesmanager/backup.py b/qubesmanager/backup.py index c199d017..826d8ecb 100644 --- a/qubesmanager/backup.py +++ b/qubesmanager/backup.py @@ -20,7 +20,10 @@ # # +import argparse +import importlib.metadata import signal +import sys from qubesadmin import exc from qubesadmin import utils as admin_utils @@ -41,6 +44,19 @@ from . import resources # pylint: disable=too-few-public-methods +def parse_args(): + parser = argparse.ArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) + _metadata_ = importlib.metadata.metadata('qubesmanager') + parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) + parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) + parser.version += '\nLicense: {}'.format(_metadata_['license']) + parser.add_argument('--version', action='version') + args = parser.parse_args() + return args + + class BackupThread(QtCore.QThread): def __init__(self, vm): QtCore.QThread.__init__(self) @@ -74,6 +90,7 @@ def run(self): class BackupVMsWindow(ui_backupdlg.Ui_Backup, QtWidgets.QWizard): def __init__(self, qt_app, qubes_app, dispatcher, parent=None): + self.cliargs = parse_args() super().__init__(parent) self.qt_app = qt_app diff --git a/qubesmanager/bootfromdevice.py b/qubesmanager/bootfromdevice.py index e04ea7a4..18711375 100644 --- a/qubesmanager/bootfromdevice.py +++ b/qubesmanager/bootfromdevice.py @@ -17,8 +17,12 @@ # # +import argparse import functools +import importlib.metadata +import os.path import subprocess +import sys from . import utils from . import ui_bootfromdevice # pylint: disable=no-name-in-module from PyQt6 import QtWidgets, QtGui, QtCore # pylint: disable=import-error @@ -169,7 +173,13 @@ def select_file_dialog(self): self.pathText.setText(new_path) -parser = tools.QubesArgumentParser(vmname_nargs=1) +parser = tools.QubesArgumentParser(vmname_nargs=1, \ + formatter_class=argparse.RawDescriptionHelpFormatter) +_metadata_ = importlib.metadata.metadata('qubesmanager') +parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) +parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) +parser.version += '\nLicense: {}'.format(_metadata_['license']) def main(args=None): diff --git a/qubesmanager/clone_vm.py b/qubesmanager/clone_vm.py index 2cc59d86..9f735ac7 100644 --- a/qubesmanager/clone_vm.py +++ b/qubesmanager/clone_vm.py @@ -20,6 +20,8 @@ # # +import argparse +import importlib.metadata import os import sys import subprocess @@ -163,7 +165,13 @@ def clone_finished(self): str(self.name.text())]) -parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='?') +parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='?', \ + formatter_class=argparse.RawDescriptionHelpFormatter) +_metadata_ = importlib.metadata.metadata('qubesmanager') +parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) +parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) +parser.version += '\nLicense: {}'.format(_metadata_['license']) def main(args=None): diff --git a/qubesmanager/create_new_vm.py b/qubesmanager/create_new_vm.py index f75f97e3..f98c9ee0 100644 --- a/qubesmanager/create_new_vm.py +++ b/qubesmanager/create_new_vm.py @@ -21,6 +21,8 @@ # # +import argparse +import importlib.metadata import os import sys import subprocess @@ -334,7 +336,13 @@ def settings_change(self): self.install_system.setChecked(False) -parser = qubesadmin.tools.QubesArgumentParser() +parser = qubesadmin.tools.QubesArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) +_metadata_ = importlib.metadata.metadata('qubesmanager') +parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) +parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) +parser.version += '\nLicense: {}'.format(_metadata_['license']) def main(args=None): diff --git a/qubesmanager/qube_manager.py b/qubesmanager/qube_manager.py index 030bc283..2e85ce69 100644 --- a/qubesmanager/qube_manager.py +++ b/qubesmanager/qube_manager.py @@ -21,8 +21,12 @@ # with this program; if not, see . # # + +import argparse +import importlib.metadata import shlex import subprocess +import sys import threading from datetime import datetime, timedelta from functools import partial @@ -62,6 +66,19 @@ # pylint: disable=unused-import, no-name-in-module from . import resources +def parse_args(): + parser = argparse.ArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) + _metadata_ = importlib.metadata.metadata('qubesmanager') + parser.version = '{} ({}) {}'.format(path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) + parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) + parser.version += '\nLicense: {}'.format(_metadata_['license']) + parser.add_argument('--version', action='version') + args = parser.parse_args() + return args + + def spawn_in_background(cmd: str | list[str]) -> None: if isinstance(cmd, str): cmd = shlex.split(cmd) @@ -742,6 +759,7 @@ class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QMainWindow): def __init__(self, qt_app, qubes_app, dispatcher, _parent=None): # pylint: disable=too-many-statements + self.cliargs = parse_args() super().__init__() self.setupUi(self) diff --git a/qubesmanager/qvm_template_gui.py b/qubesmanager/qvm_template_gui.py index 949ca3db..a6f89a1f 100644 --- a/qubesmanager/qvm_template_gui.py +++ b/qubesmanager/qvm_template_gui.py @@ -23,15 +23,18 @@ # pylint: disable=invalid-name import abc +import argparse import asyncio import collections import functools +import importlib.metadata import subprocess import threading from datetime import datetime from datetime import UTC import json import os +import sys import typing import shlex @@ -79,6 +82,19 @@ """) +def parse_args(): + parser = argparse.ArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) + _metadata_ = importlib.metadata.metadata('qubesmanager') + parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) + parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) + parser.version += '\nLicense: {}'.format(_metadata_['license']) + parser.add_argument('--version', action='version') + args = parser.parse_args() + return args + + class TreeItem(abc.ABC): COL_NAMES = [ 'Name', @@ -631,6 +647,7 @@ class QvmTemplateWindow( ui_qvmtemplate.Ui_MainWindow, PyQt6.QtWidgets.QMainWindow): def __init__(self, qt_app, qubes_app, dispatcher, _parent=None): + self.cliargs = parse_args() super().__init__() self.setupUi(self) self.template_tree.header().setSectionResizeMode( diff --git a/qubesmanager/restore.py b/qubesmanager/restore.py index c78964b2..23d372c3 100644 --- a/qubesmanager/restore.py +++ b/qubesmanager/restore.py @@ -21,9 +21,11 @@ # import argparse +import importlib.metadata from PyQt6 import QtCore, QtWidgets, QtGui # pylint: disable=import-error import os import os.path +import sys import logging import logging.handlers @@ -43,12 +45,19 @@ # pylint: disable=too-few-public-methods def parse_args(): - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( '--log', action='store', default='INFO', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Provide logging level. Values: DEBUG, INFO, ' 'INFO (default), ERROR, CRITICAL') + _metadata_ = importlib.metadata.metadata('qubesmanager') + parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) + parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) + parser.version += '\nLicense: {}'.format(_metadata_['license']) + parser.add_argument('--version', action='version') args = parser.parse_args() return args @@ -85,7 +94,7 @@ def run(self): class RestoreVMsWindow(ui_restoredlg.Ui_Restore, QtWidgets.QWizard): def __init__(self, qt_app, qubes_app, parent=None): - cliargs = parse_args() + self.cliargs = parse_args() super().__init__(parent) self.qt_app = qt_app @@ -104,7 +113,7 @@ def __init__(self, qt_app, qubes_app, parent=None): handler = logging.handlers.QueueHandler(self.feedback_queue) logger = logging.getLogger('qubesadmin.backup') logger.addHandler(handler) - logger.setLevel(cliargs.log) + logger.setLevel(self.cliargs.log) self.backup_restore = None self.target_appvm = None diff --git a/qubesmanager/settings.py b/qubesmanager/settings.py index 9d6d20a0..4c3d577e 100644 --- a/qubesmanager/settings.py +++ b/qubesmanager/settings.py @@ -22,10 +22,13 @@ # # +import argparse import collections import functools +import importlib.metadata import re import subprocess +import os import sys import traceback from qubesadmin.tools import QubesArgumentParser @@ -1551,7 +1554,13 @@ def delete_rule_button_pressed(self): self.fw_model.remove_child(i) -parser = QubesArgumentParser(vmname_nargs=1) +parser = QubesArgumentParser(vmname_nargs=1, + formatter_class=argparse.RawDescriptionHelpFormatter) +_metadata_ = importlib.metadata.metadata('qubesmanager') +parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) +parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) +parser.version += '\nLicense: {}'.format(_metadata_['license']) parser.add_argument('--tab', metavar='TAB', action='store', diff --git a/qubesmanager/template_manager.py b/qubesmanager/template_manager.py index b3c7681c..bdaacb15 100644 --- a/qubesmanager/template_manager.py +++ b/qubesmanager/template_manager.py @@ -20,6 +20,11 @@ # # +import argparse +import importlib.metadata +import sys +import os + from qubesadmin import exc from PyQt6 import QtWidgets, QtGui, QtCore # pylint: disable=import-error @@ -35,11 +40,25 @@ column_names = ['State', 'Qube', 'Current template', 'New template'] +def parse_args(): + parser = argparse.ArgumentParser( \ + formatter_class=argparse.RawDescriptionHelpFormatter) + _metadata_ = importlib.metadata.metadata('qubesmanager') + parser.version = '{} ({}) {}'.format(os.path.basename(sys.argv[0]), \ + _metadata_['summary'], _metadata_['version']) + parser.version += '\nCopyright (C) {}'.format(_metadata_['author']) + parser.version += '\nLicense: {}'.format(_metadata_['license']) + parser.add_argument('--version', action='version') + args = parser.parse_args() + return args + + class TemplateManagerWindow( ui_templatemanager.Ui_MainWindow, QtWidgets.QMainWindow): def __init__(self, qt_app, qubes_app, dispatcher, parent=None): # pylint: disable=unused-argument + self.cliargs = parse_args() super().__init__(parent) self.setupUi(self)