From 13b8dc869626af8e8e811c57187d853bc198e74f Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 4 Sep 2024 00:53:42 -0700 Subject: [PATCH] Replace remaining docopt sections --- scripts/wk/clone/state.py | 28 ++++++------- scripts/wk/kit/ufd.py | 82 ++++++++++++++++++++++++++------------- 2 files changed, 69 insertions(+), 41 deletions(-) diff --git a/scripts/wk/clone/state.py b/scripts/wk/clone/state.py index 8af31970..e685e55c 100644 --- a/scripts/wk/clone/state.py +++ b/scripts/wk/clone/state.py @@ -316,7 +316,7 @@ class State(): """Get total size of all block_pairs in bytes, returns int.""" return sum(pair.size for pair in self.block_pairs) - def init_recovery(self, docopt_args: dict[str, Any]) -> None: + def init_recovery(self, cli_args: dict[str, Any]) -> None: """Select source/dest and set env.""" cli.clear_screen() disk_menu = menus.disks() @@ -324,10 +324,10 @@ class State(): self.ui.set_progress_file(str(self.progress_out)) # Set mode - self.mode = set_mode(docopt_args) + self.mode = set_mode(cli_args) # Select source - self.source = select_disk_obj('source', disk_menu, docopt_args['']) + self.source = select_disk_obj('source', disk_menu, cli_args['']) self.update_top_panes() if self.source.trim: cli.print_warning('Source device supports TRIM') @@ -340,12 +340,12 @@ class State(): self.destination = select_disk_obj( 'destination', disk_menu, - docopt_args[''], + cli_args[''], ) self.ui.add_title_pane('Destination', self.destination.name) elif self.mode == 'Image': - if docopt_args['']: - self.destination = pathlib.Path(docopt_args['']).resolve() + if cli_args['']: + self.destination = pathlib.Path(cli_args['']).resolve() else: self.destination = menus.select_path('Destination') self.destination.mkdir(parents=True, exist_ok=True) @@ -370,11 +370,11 @@ class State(): self.working_dir = get_working_dir( self.mode, self.destination, - force_local=docopt_args['--force-local-map'], + force_local=cli_args['--force-local-map'], ) # Start fresh if requested - if docopt_args['--start-fresh']: + if cli_args['--start-fresh']: clean_working_dir(self.working_dir) # Add block pairs @@ -412,10 +412,10 @@ class State(): # Prep destination if self.mode == 'Clone': - prep_destination(self, source_parts, dry_run=docopt_args['--dry-run']) + prep_destination(self, source_parts, dry_run=cli_args['--dry-run']) # Safety Checks #2 - if not docopt_args['--dry-run']: + if not cli_args['--dry-run']: for pair in self.block_pairs: pair.safety_check() @@ -1051,12 +1051,12 @@ def select_disk_obj(label:str, disk_menu: cli.Menu, disk_path: str) -> hw_disk.D raise std.GenericAbort() -def set_mode(docopt_args) -> str: - """Set mode from docopt_args or user selection, returns str.""" - if docopt_args['clone']: +def set_mode(cli_args) -> str: + """Set mode from cli_args or user selection, returns str.""" + if cli_args['clone']: return 'Clone' - if docopt_args['image']: + if cli_args['image']: return 'Image' # Ask user if necessary diff --git a/scripts/wk/kit/ufd.py b/scripts/wk/kit/ufd.py index 66a3b7f6..11a8859b 100644 --- a/scripts/wk/kit/ufd.py +++ b/scripts/wk/kit/ufd.py @@ -1,6 +1,7 @@ """WizardKit: UFD Functions""" # vim: sts=2 sw=2 ts=2 +import argparse import logging import math import os @@ -9,8 +10,6 @@ import re import shutil from subprocess import CalledProcessError -from docopt import docopt - from wk import io, log from wk.cfg.main import KIT_NAME_FULL, KIT_NAME_SHORT from wk.cfg.ufd import ( @@ -29,30 +28,6 @@ from wk.ui import cli as ui # STATIC VARIABLES -DOCSTRING = '''WizardKit: Build UFD - -Usage: - build-ufd [options] --ufd-device PATH - [--linux PATH] - [--main-kit PATH] - [--winpe PATH] - [--extra-dir PATH] - [EXTRA_IMAGES...] - build-ufd (-h | --help) - -Options: - -e PATH, --extra-dir PATH - -k PATH, --main-kit PATH - -l PATH, --linux PATH - -u PATH, --ufd-device PATH - -w PATH, --winpe PATH - - -d --debug Enable debug mode - -h --help Show this page - -M --use-mbr Use real MBR instead of GPT w/ Protective MBR - -F --force Bypass all confirmation messages. USE WITH EXTREME CAUTION! - -U --update Don't format device, just update -''' LOG = logging.getLogger(__name__) EXTRA_IMAGES_LIST = '/mnt/UFD/arch/extra_images.list' MIB = 1024 ** 2 @@ -61,6 +36,54 @@ UFD_LABEL = f'{KIT_NAME_SHORT}_UFD' # 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='build-ufd', + description=f'{KIT_NAME_FULL}: Build UFD', + ) + parser.add_argument('-u', '--ufd-device', required=True) + parser.add_argument('-l', '--linux', required=False) + parser.add_argument('-e', '--extra-dir', required=False) + parser.add_argument('-k', '--main-kit', required=False) + parser.add_argument('-w', '--winpe', required=False) + parser.add_argument( + '-d', '--debug', action='store_true', + help='Enable debug mode', + ) + parser.add_argument( + '-M', '--use-mbr', action='store_true', + help='Use real MBR instead of GPT w/ Protective MBR', + ) + parser.add_argument( + '-F', '--force', action='store_true', + help='Bypass all confirmation messages. USE WITH EXTREME CAUTION!', + ) + parser.add_argument( + '-U', '--update', action='store_true', + help="Don't format device, just update", + ) + parser.add_argument( + 'EXTRA_IMAGES', nargs='*', + ) + args = parser.parse_args() + legacy_args = { + '--debug': args.debug, + '--extra-dir': args.extra_dir, + '--force': args.force, + '--linux': args.linux, + '--main-kit': args.main_kit, + '--ufd-device': args.ufd_device, + '--update': args.update, + '--use-mbr': args.use_mbr, + '--winpe': args.winpe, + 'EXTRA_IMAGES': args.extra_images, + } + return legacy_args + def apply_image(part_path, image_path, hide_macos_boot=True) -> None: """Apply raw image to dev_path using dd.""" cmd = [ @@ -93,7 +116,12 @@ def apply_image(part_path, image_path, hide_macos_boot=True) -> None: def build_ufd() -> None: """Build UFD using selected sources.""" - args = docopt(DOCSTRING) + try: + args = argparse_helper() + except SystemExit: + print('') + ui.pause('Press Enter to exit...') + raise if args['--debug']: log.enable_debug_mode() if args['--update'] and args['EXTRA_IMAGES']: