merge subparsers and refactor
This commit is contained in:
parent
74aa28e027
commit
bb60b25399
3 changed files with 39 additions and 24 deletions
|
@ -70,7 +70,7 @@ def run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version):
|
||||||
local_borg_version,
|
local_borg_version,
|
||||||
global_arguments,
|
global_arguments,
|
||||||
extract_to_stdout=False,
|
extract_to_stdout=False,
|
||||||
destination_path=bootstrap_arguments.destination,
|
destination_path=bootstrap_arguments.destination or '/',
|
||||||
strip_components=bootstrap_arguments.strip_components,
|
strip_components=bootstrap_arguments.strip_components,
|
||||||
progress=bootstrap_arguments.progress,
|
progress=bootstrap_arguments.progress,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import argparse
|
||||||
import collections
|
import collections
|
||||||
from argparse import Action, ArgumentParser
|
from argparse import Action, ArgumentParser
|
||||||
|
|
||||||
|
@ -73,18 +74,15 @@ def parse_subparser_arguments(unparsed_arguments, subparsers):
|
||||||
if item in subparsers:
|
if item in subparsers:
|
||||||
remaining_arguments.remove(item)
|
remaining_arguments.remove(item)
|
||||||
|
|
||||||
if canonical_name not in subcommand_parsers_mapping:
|
arguments[canonical_name] = None if canonical_name in subcommand_parsers_mapping else parsed
|
||||||
arguments[canonical_name] = parsed
|
|
||||||
else:
|
|
||||||
arguments[canonical_name] = None
|
|
||||||
|
|
||||||
for argument in arguments:
|
for argument in arguments:
|
||||||
if arguments[argument] == None:
|
if not arguments[argument]:
|
||||||
for subcommand in subcommand_parsers_mapping[argument]:
|
if not any(subcommand in arguments for subcommand in subcommand_parsers_mapping[argument]):
|
||||||
if subcommand not in arguments:
|
raise ValueError("Missing subcommand for {}. Expected one of {}".format(
|
||||||
raise ValueError("Missing subcommand for {}. Expected one of {}".format(
|
argument, subcommand_parsers_mapping[argument]
|
||||||
argument, subcommand_parsers_mapping[argument]
|
))
|
||||||
))
|
|
||||||
|
|
||||||
# If no actions are explicitly requested, assume defaults.
|
# If no actions are explicitly requested, assume defaults.
|
||||||
if not arguments and '--help' not in unparsed_arguments and '-h' not in unparsed_arguments:
|
if not arguments and '--help' not in unparsed_arguments and '-h' not in unparsed_arguments:
|
||||||
|
@ -948,7 +946,17 @@ def make_parsers():
|
||||||
)
|
)
|
||||||
borg_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')
|
borg_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')
|
||||||
|
|
||||||
return top_level_parser, subparsers, config_subparsers
|
merged_subparsers = argparse._SubParsersAction(None, None, metavar=None, dest='merged', parser_class=None)
|
||||||
|
|
||||||
|
for name, subparser in subparsers.choices.items():
|
||||||
|
merged_subparsers._name_parser_map[name] = subparser
|
||||||
|
subparser._name_parser_map = merged_subparsers._name_parser_map
|
||||||
|
|
||||||
|
for name, subparser in config_subparsers.choices.items():
|
||||||
|
merged_subparsers._name_parser_map[name] = subparser
|
||||||
|
subparser._name_parser_map = merged_subparsers._name_parser_map
|
||||||
|
|
||||||
|
return top_level_parser, merged_subparsers
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments(*unparsed_arguments):
|
def parse_arguments(*unparsed_arguments):
|
||||||
|
@ -956,14 +964,18 @@ def parse_arguments(*unparsed_arguments):
|
||||||
Given command-line arguments with which this script was invoked, parse the arguments and return
|
Given command-line arguments with which this script was invoked, parse the arguments and return
|
||||||
them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance.
|
them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance.
|
||||||
'''
|
'''
|
||||||
top_level_parser, subparsers, config_subparsers = make_parsers()
|
top_level_parser, subparsers = make_parsers()
|
||||||
|
|
||||||
subparser_choices = subparsers.choices.copy()
|
|
||||||
subparser_choices.update(config_subparsers.choices)
|
|
||||||
|
|
||||||
arguments, remaining_arguments = parse_subparser_arguments(
|
arguments, remaining_arguments = parse_subparser_arguments(
|
||||||
unparsed_arguments, subparser_choices
|
unparsed_arguments, subparsers.choices
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if 'bootstrap' in arguments.keys() and len(arguments.keys()) > 1:
|
||||||
|
raise ValueError(
|
||||||
|
'The bootstrap action cannot be combined with other actions. Please run it separately.'
|
||||||
|
)
|
||||||
|
|
||||||
arguments['global'] = top_level_parser.parse_args(remaining_arguments)
|
arguments['global'] = top_level_parser.parse_args(remaining_arguments)
|
||||||
|
|
||||||
if arguments['global'].excludes_filename:
|
if arguments['global'].excludes_filename:
|
||||||
|
|
|
@ -617,7 +617,7 @@ def collect_configuration_run_summary_logs(configs, arguments):
|
||||||
if 'extract' in arguments or 'mount' in arguments:
|
if 'extract' in arguments or 'mount' in arguments:
|
||||||
validate.guard_single_repository_selected(repository, configs)
|
validate.guard_single_repository_selected(repository, configs)
|
||||||
|
|
||||||
if 'config' not in arguments:
|
if 'bootstrap' not in arguments:
|
||||||
validate.guard_configuration_contains_repository(repository, configs)
|
validate.guard_configuration_contains_repository(repository, configs)
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
yield from log_error_records(str(error))
|
yield from log_error_records(str(error))
|
||||||
|
@ -626,14 +626,17 @@ def collect_configuration_run_summary_logs(configs, arguments):
|
||||||
if 'bootstrap' in arguments:
|
if 'bootstrap' in arguments:
|
||||||
# no configuration file is needed for bootstrap
|
# no configuration file is needed for bootstrap
|
||||||
local_borg_version = borg_version.local_borg_version({}, 'borg')
|
local_borg_version = borg_version.local_borg_version({}, 'borg')
|
||||||
borgmatic.actions.bootstrap.run_bootstrap(arguments['bootstrap'], arguments['global'], local_borg_version)
|
try:
|
||||||
yield logging.makeLogRecord(
|
borgmatic.actions.bootstrap.run_bootstrap(arguments['bootstrap'], arguments['global'], local_borg_version)
|
||||||
dict(
|
yield logging.makeLogRecord(
|
||||||
levelno=logging.INFO,
|
dict(
|
||||||
levelname='INFO',
|
levelno=logging.INFO,
|
||||||
msg='Bootstrap successful',
|
levelname='INFO',
|
||||||
|
msg='Bootstrap successful',
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
except (CalledProcessError, ValueError, OSError) as error:
|
||||||
|
yield from log_error_records('Error running bootstrap', error)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not configs:
|
if not configs:
|
||||||
|
|
Loading…
Reference in a new issue