Skip to content

Commit

Permalink
answerfile: make several usability improvements
Browse files Browse the repository at this point in the history
- previously the generated answerfile didn't contain the
  expected choices for the boolean component (the only one
  allowed component for actor dialogs so far);
- leapp answer command now prints usage message in case of
  inappropriate invocation;
- answerfile path shown at the end of leapp preupgrade run
  even without --debug.
  • Loading branch information
fernflower authored and pirat89 committed Feb 13, 2020
1 parent bc8a1ec commit 5463767
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
19 changes: 8 additions & 11 deletions leapp/cli/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime

from leapp.config import get_config
from leapp.exceptions import CommandError, LeappError
from leapp.exceptions import CommandError, LeappError, UsageError
from leapp.logger import configure_logger
from leapp.messaging.answerstore import AnswerStore
from leapp.repository.scan import find_and_scan_repositories
Expand Down Expand Up @@ -188,6 +188,7 @@ def upgrade(args):
context = str(uuid.uuid4())
cfg = get_config()
configuration = prepare_configuration(args)
answerfile_path = cfg.get('report', 'answerfile')

if os.getuid():
raise CommandError('This command has to be run under the root user.')
Expand Down Expand Up @@ -224,16 +225,12 @@ def upgrade(args):
process_whitelist_experimental(repositories, workflow, configuration, logger)
warn_if_unsupported(configuration)
with beautify_actor_exception():
answerfile_path = cfg.get('report', 'answerfile')
logger.info("Using answerfile at %s", answerfile_path)
workflow.load_answerfile(answerfile_path)
workflow.run(context=context, skip_phases_until=skip_phases_until, skip_dialogs=True)

report_errors(workflow.errors)
generate_report_files(context)

cfg = get_config()

report_files = get_cfg_files('report', cfg)
log_files = get_cfg_files('logs', cfg)
report_info(report_files, log_files, fail=workflow.failure)
Expand Down Expand Up @@ -281,15 +278,15 @@ def preupgrade(args):
report_errors(workflow.errors)
report_files = get_cfg_files('report', cfg)
log_files = get_cfg_files('logs', cfg)
report_info(report_files, log_files, fail=workflow.failure)
report_info(report_files, log_files, answerfile_path, fail=workflow.failure)
if workflow.failure:
sys.exit(1)


@command('answer', help='Manage answerfile')
@command_opt('answerfile', help='Path to an answerfile to update')
@command('answer', help='Manage answerfile: register user choices for specific dialog sections')
@command_opt('answerfile', help='Path to the answerfile to update')
@command_opt('section', action='append', metavar='dialog_sections',
help='Record answer for specific section in answerfile')
help='Register answer for a specific section in the answerfile')
@command_opt('add', is_flag=True,
help='If set sections will be created even if missing in original answerfile')
def answer(args):
Expand All @@ -298,12 +295,12 @@ def answer(args):
if args.section:
args.section = list(itertools.chain(*[i.split(',') for i in args.section]))
else:
raise CommandError('At least one dialog section must be specified, ex. --section dialog.option=mychoice')
raise UsageError('At least one dialog section must be specified, ex. --section dialog.option=mychoice')
try:
sections = [tuple((dialog_option.split('.', 2) + [value]))
for dialog_option, value in [s.split('=', 2) for s in args.section]]
except ValueError:
raise CommandError("A bad formatted section has been passed. Expected format is dialog.option=mychoice")
raise UsageError("A bad formatted section has been passed. Expected format is dialog.option=mychoice")
answerfile_path = args.answerfile or cfg.get('report', 'answerfile')
answerstore = AnswerStore()
answerstore.load(answerfile_path)
Expand Down
1 change: 1 addition & 0 deletions leapp/dialogs/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class BooleanComponent(Component):
"""
BooleanComponent is used for boolean inputs such as Yes/No questions.
"""
choices = ('True', 'False')
values = ('Yes', 'No')
value_type = bool

Expand Down
4 changes: 1 addition & 3 deletions leapp/messaging/answerstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ def generate(self, dialogs, answer_file_path):
choices = ''
answer_entry = ''
if hasattr(component, 'choices'):
choices += '# Available choices:\n'
for choice in component.choices:
choices += '# - {}\n'.format(choice)
choices += '# Available choices: {}\n'.format('/'.join(component.choices))
if component.value_type is tuple:
default = ';'.join(component.default)
answer = ';'.join(answer) if answer else answer
Expand Down
5 changes: 4 additions & 1 deletion leapp/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def report_errors(errors):
sys.stdout.write(pretty_block("END OF ERRORS", color=Color.red))


def report_info(report_paths, log_paths, fail=False):
def report_info(report_paths, log_paths, answerfile=None, fail=False):
report_paths = [report_paths] if not isinstance(report_paths, list) else report_paths
log_paths = [log_paths] if not isinstance(report_paths, list) else log_paths

Expand All @@ -63,6 +63,9 @@ def report_info(report_paths, log_paths, fail=False):
sys.stdout.write(pretty_block("END OF REPORT", color=Color.bold if fail else Color.green))
sys.stdout.write("\n")

if answerfile:
sys.stdout.write("Answerfile has been generated at {}\n".format(answerfile))


def report_unsupported(devel_vars, experimental):
sys.stdout.write(pretty_block("UNSUPPORTED UPGRADE", color=Color.yellow))
Expand Down

0 comments on commit 5463767

Please sign in to comment.