Move to argparse for ddrescue-tui and hw-diags

This commit is contained in:
2Shirt 2024-09-03 01:12:00 -07:00
parent d00d625142
commit 50033f42f6
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
4 changed files with 80 additions and 49 deletions

View file

@ -2,19 +2,10 @@
"""WizardKit: ddrescue TUI""" """WizardKit: ddrescue TUI"""
# vim: sts=2 sw=2 ts=2 # vim: sts=2 sw=2 ts=2
from docopt import docopt
import wk import wk
if __name__ == '__main__': if __name__ == '__main__':
try:
docopt(wk.clone.ddrescue.DOCSTRING)
except SystemExit:
print('')
wk.ui.cli.pause('Press Enter to exit...')
raise
try: try:
wk.clone.ddrescue.main() wk.clone.ddrescue.main()
except SystemExit: except SystemExit:

View file

@ -2,19 +2,10 @@
"""WizardKit: Hardware Diagnostics""" """WizardKit: Hardware Diagnostics"""
# vim: sts=2 sw=2 ts=2 # vim: sts=2 sw=2 ts=2
from docopt import docopt
import wk import wk
if __name__ == '__main__': if __name__ == '__main__':
try:
docopt(wk.hw.diags.DOCSTRING)
except SystemExit:
print('')
wk.ui.cli.pause('Press Enter to exit...')
raise
try: try:
wk.hw.diags.main() wk.hw.diags.main()
except SystemExit: except SystemExit:

View file

@ -1,6 +1,7 @@
"""WizardKit: ddrescue TUI""" """WizardKit: ddrescue TUI"""
# vim: sts=2 sw=2 ts=2 # vim: sts=2 sw=2 ts=2
import argparse
import atexit import atexit
import datetime import datetime
import logging import logging
@ -12,8 +13,6 @@ from random import randint
import pytz import pytz
from docopt import docopt
from wk import cfg, exe, io, log, std from wk import cfg, exe, io, log, std
from wk.cfg.ddrescue import DDRESCUE_SPECIFIC_PASS_SETTINGS from wk.cfg.ddrescue import DDRESCUE_SPECIFIC_PASS_SETTINGS
from wk.clone import menus from wk.clone import menus
@ -29,19 +28,6 @@ from wk.ui import ansi, cli
# STATIC VARIABLES # STATIC VARIABLES
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
DOCSTRING = f'''{cfg.main.KIT_NAME_FULL}: ddrescue TUI
Usage:
ddrescue-tui
ddrescue-tui [options] (clone|image) [<source> [<destination>]]
ddrescue-tui (-h | --help)
Options:
-h --help Show this page
-s --dry-run Print commands to be used instead of running them
--force-local-map Skip mounting shares and save map to local drive
--start-fresh Ignore previous runs and start new recovery
'''
DETECT_DRIVES_NOTICE = ''' DETECT_DRIVES_NOTICE = '''
This option will force the drive controllers to rescan for devices. This option will force the drive controllers to rescan for devices.
The method used is not 100% reliable and may cause issues. If you see The method used is not 100% reliable and may cause issues. If you see
@ -55,6 +41,42 @@ TIMEZONE = pytz.timezone(cfg.main.LINUX_TIME_ZONE)
# Functions # Functions
def argparse_helper() -> dict[str, None|bool|str]:
"""Helper function to setup and return args, returns dict.
NOTE: A dict is used to match the legacy code.
"""
parser = argparse.ArgumentParser(
prog='ddrescue-tui',
description=f'{cfg.main.KIT_NAME_FULL}: ddrescue TUI',
)
parser.add_argument('mode', choices=('clone', 'image'), nargs='?')
parser.add_argument('source', nargs='?')
parser.add_argument('destination', nargs='?')
parser.add_argument(
'-s', '--dry-run', action='store_true',
help='Print commands to be used instead of running them',
)
parser.add_argument(
'--force-local-map', action='store_true',
help='Skip mounting shares and save map to local drive',
)
parser.add_argument(
'--start-fresh', action='store_true',
help='Ignore previous runs and start new recovery',
)
args = parser.parse_args()
legacy_args = {
'clone': False if args.clone is None else args.clone,
'image': False if args.image is None else args.image,
'<source>': args.source,
'<destination>': args.destination,
'--dry-run': args.dry_run,
'--force-local-map': args.force_local_map,
'--start-fresh': args.start_fresh,
}
return legacy_args
def build_ddrescue_cmd(block_pair, pass_name, settings_menu) -> list[str]: def build_ddrescue_cmd(block_pair, pass_name, settings_menu) -> list[str]:
"""Build ddrescue cmd using passed details, returns list.""" """Build ddrescue cmd using passed details, returns list."""
cmd = ['sudo', 'ddrescue'] cmd = ['sudo', 'ddrescue']
@ -273,7 +295,12 @@ def is_missing_source_or_destination(state) -> bool:
def main() -> None: def main() -> None:
"""Main function for ddrescue TUI.""" """Main function for ddrescue TUI."""
args = docopt(DOCSTRING) try:
args = argparse_helper()
except SystemExit:
print('')
cli.pause('Press Enter to exit...')
raise
# Log setup # Log setup
log_path = log.format_log_path(log_name='main', sub_dir='ddrescue-TUI') log_path = log.format_log_path(log_name='main', sub_dir='ddrescue-TUI')

View file

@ -1,14 +1,13 @@
"""WizardKit: Hardware diagnostics""" """WizardKit: Hardware diagnostics"""
# vim: sts=2 sw=2 ts=2 # vim: sts=2 sw=2 ts=2
import argparse
import atexit import atexit
import logging import logging
import os import os
import pathlib import pathlib
import subprocess import subprocess
from docopt import docopt
from wk import cfg, debug, exe, log, std from wk import cfg, debug, exe, log, std
from wk.cfg.hw import STATUS_COLORS from wk.cfg.hw import STATUS_COLORS
from wk.hw import benchmark as hw_benchmark from wk.hw import benchmark as hw_benchmark
@ -28,18 +27,6 @@ from wk.ui import ansi, cli, tui
# STATIC VARIABLES # STATIC VARIABLES
DOCSTRING = f'''{cfg.main.KIT_NAME_FULL}: Hardware Diagnostics
Usage:
hw-diags [options]
hw-diags (-h | --help)
Options:
-c --cli Force CLI mode
-h --help Show this page
-q --quick Skip menu and perform a quick check
-t --test-mode Run diags in test mode
'''
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
TEST_GROUPS = { TEST_GROUPS = {
# Also used to build the menu options # Also used to build the menu options
@ -264,6 +251,36 @@ class State():
# Functions # Functions
def argparse_helper() -> dict[str, bool]:
"""Helper function to setup and return args, returns dict.
NOTE: A dict is used to match the legacy code.
"""
parser = argparse.ArgumentParser(
prog='hw-diags',
description=f'{cfg.main.KIT_NAME_FULL}: Hardware Diagnostics',
)
parser.add_argument(
'-c', '--cli', action='store_true',
help='Force CLI mode',
)
parser.add_argument(
'-q', '--quick', action='store_true',
help='Skip menu and perform a quick check',
)
parser.add_argument(
'-t', '--test-mode', action='store_true',
help='Run diags in test mode',
)
args = parser.parse_args()
legacy_args = {
'--cli': args.cli,
'--quick': args.quick,
'--test-mode': args.test_mode,
}
return legacy_args
def build_menu(cli_mode=False, quick_mode=False) -> cli.Menu: def build_menu(cli_mode=False, quick_mode=False) -> cli.Menu:
"""Build main menu, returns wk.ui.cli.Menu.""" """Build main menu, returns wk.ui.cli.Menu."""
menu = cli.Menu(title='') menu = cli.Menu(title='')
@ -748,7 +765,12 @@ def disk_surface_scan(state: State, test_objects, test_mode=False) -> None:
def main() -> None: def main() -> None:
"""Main function for hardware diagnostics.""" """Main function for hardware diagnostics."""
args = docopt(DOCSTRING) try:
args = argparse_helper()
except SystemExit:
print('')
cli.pause('Press Enter to exit...')
raise
log.update_log_path(dest_name='Hardware-Diagnostics', timestamp=True) log.update_log_path(dest_name='Hardware-Diagnostics', timestamp=True)
# Safety check # Safety check