bitbake-setup: Improve readability
cover bitbake-setup: Improve readability
The series is an attempt at improving the readability of some of the bitbake-setup output on the console, to improve user experience.
Here's a summary of the changes:
- Align outputs of choices in the init command (instead of tabs)
- Keep printing plain messages when passing --quiet (otherwise it makes most command unusable)
- Print the diffs with color, which makes them much easier to read.
All the coloring can be disabled by passing --color=never.
---
Antonin Godard (4):
bitbake-setup: define a global for the color choice
bitbake-setup: improve readability of choices
bitbake-setup: print plain messages in quiet mode
bitbake-setup: print colored diffs
bin/bitbake-setup | 91 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 27 deletions(-)
---
base-commit: 8b635a400152bb40beaa0d7a47181febb43f5c84
change-id: 20260123-improve-bbsetup-readability-ff6a0ee8ee25
PATCH 1/4 bitbake-setup: define a global for the color choice
Define a BBSETUP_COLOR global variable that holds the color choice optionally passed from the command-line. This will be re-used in the next commits to force subprocess commands to color their outputs, as passing --color=auto in them always render without coloring.
--- bin/bitbake-setup | 6 ++++++ 1 file changed, 6 insertions(+)
bin/bitbake-setup +6 -0
@@ -29,6 +29,8 @@ import bb.process logger = bb.msg.logger_create('bitbake-setup', sys.stdout) +BBSETUP_COLOR = False + # These settings can only be set in the global context GLOBAL_ONLY_SETTINGS = ( "top-dir-prefix",
@@ -1095,6 +1097,10 @@ def main(): elif args.quiet: logger.setLevel(logging.ERROR) + global BBSETUP_COLOR + if args.color == 'always' or (args.color == 'auto' and sys.stdout.isatty() and os.environ.get('NO_COLOR', '') == ''): + BBSETUP_COLOR = True + # Need to re-run logger_create with color argument # (will be the same logger since it has the same name) bb.msg.logger_create('bitbake-setup', output=sys.stdout,
Signature · 1 line
2.52.0
Re: [bitbake-devel] [PATCH 1/4] bitbake-setup: define a global for the color choice
bin/bitbake-setup def main(): · line 1103
Quoted 4 lines · Antonin Godard via lists.openembedded.org
bin/bitbake-setup +4
-0+ global BBSETUP_COLOR + if args.color == 'always' or (args.color == 'auto' and sys.stdout.isatty() and os.environ.get('NO_COLOR', '') == ''): + BBSETUP_COLOR = True +
Global variables are evil. They easily enable unwanted side effects, when a function call doesn't just 'return something', it also changes the global program state, which makes it harder to reason about program behavior, especially when the program is multi-threaded.
Which is to say, can you change this into a function please?
Alex
PATCH 2/4 bitbake-setup: improve readability of choices
In reply to Antonin GodardBold the configuration names and align the descriptions for improved readability. For this, define a print_configs() function which should also help factorizing the code as this is done in multiple places.
--- bin/bitbake-setup | 73 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 23 deletions(-)
bin/bitbake-setup +50 -23
@@ -37,6 +37,32 @@ GLOBAL_ONLY_SETTINGS = ( "top-dir-name", ) +def print_configs(prompt: str,choices: list[str], descriptions: list[str] = []): + """ + Helper function to print a list of choices and align the output. + Each option name is made bold to stand out. + """ + if not prompt.endswith(':'): + prompt += ":" + logger.plain(prompt) + + if not descriptions: + descriptions = ["" for _ in choices] + + # maximum size of all choices, for alignment + cmax = max([len(c) for c in choices]) + 1 + + for n, c in enumerate(choices): + msg = f"{n + 1}. " + if BBSETUP_COLOR: + # make it bold + msg += "\033[1m" + msg += f"{c:<{cmax}}" + if BBSETUP_COLOR: + msg += "\033[0m" + msg += f" {descriptions[n]}" + logger.plain(msg) + # If bitbake is from a release tarball or somewhere like pypi where # updates may not be straightforward, prefer to use the git repo as the # default registry
@@ -492,36 +518,39 @@ def choose_bitbake_config(configs, parameters, non_interactive): if non_interactive: raise Exception("Unable to choose from bitbake configurations in non-interactive mode: {}".format(configs_dict)) - logger.plain("\nAvailable bitbake configurations:") - for n, config_data in enumerated_configs: - logger.plain("{}. {}\t{}".format(n, config_data["name"], config_data["description"])) + logger.plain("") + print_configs("Available bitbake configurations", + [c["name"] for c in flattened_configs], + [c["description"] for c in flattened_configs]) config_n = int_input([i[0] for i in enumerated_configs], "\nPlease select one of the above bitbake configurations by its number: ") - 1 return flattened_configs[config_n] def choose_config(configs, non_interactive): not_expired_configs = [k for k in sorted(configs.keys()) if not has_expired(configs[k].get("expires", None))] - config_list = list(enumerate(not_expired_configs, 1)) - if len(config_list) == 1: - only_config = config_list[0][1] + if len(not_expired_configs) == 1: + only_config = not_expired_configs[0] logger.plain("\nSelecting the only available configuration {}\n".format(only_config)) return only_config if non_interactive: raise Exception("Unable to choose from configurations in non-interactive mode: {}".format(not_expired_configs)) - logger.plain("\nAvailable Configuration Templates:") - for n, config_name in config_list: - config_data = configs[config_name] - expiry_date = config_data.get("expires", None) - config_desc = config_data["description"] + descs = [] + for c in not_expired_configs: + d = configs[c]["description"] + expiry_date = configs[c].get("expires", None) if expiry_date: - logger.plain("{}. {}\t{} (supported until {})".format(n, config_name, config_desc, expiry_date)) - else: - logger.plain("{}. {}\t{}".format(n, config_name, config_desc)) - config_n = int_input([i[0] for i in config_list], - "\nPlease select one of the above Configuration Templates by its number: ") - 1 - return config_list[config_n][1] + d += f" (supported until {expiry_date})" + descs.append(d) + + logger.plain("") + print_configs("Available Configuration Templates", + [c for c in not_expired_configs], + descs) + config_n = int_input([i[0] for i in list(enumerate(not_expired_configs, 1))], + "\nPlease select one of the above configurations by its number: ") - 1 + return not_expired_configs[config_n] def choose_fragments(possibilities, parameters, non_interactive, skip_selection): choices = {}
@@ -547,13 +576,11 @@ def choose_fragments(possibilities, parameters, non_interactive, skip_selection) if non_interactive: raise Exception(f"Unable to choose from options in non-interactive mode: {[o['name'] for o in options]}") - logger.plain("\n" + v["description"] + ":") + logger.plain("") + print_configs(v["description"], + [o['name'] for o in options], + [o['description'] for o in options]) options_enumerated = list(enumerate(options, 1)) - for n,o in options_enumerated: - opt_str = f"{n}. {o['name']}" - if o["description"]: - opt_str += f"\t{o['description']}" - logger.plain(opt_str) option_n = int_input([i[0] for i in options_enumerated], "\nPlease select one of the above options by its number: ") - 1 choices[k] = options_enumerated[option_n][1]["name"]
Signature · 1 line
2.52.0
PATCH 3/4 bitbake-setup: print plain messages in quiet mode
In reply to Antonin GodardAt the moment, plain messages are not printed when --quiet is passed on the command line. This makes no sense since bitbake-setup prints most of the essential information with the logging.PLAIN level. Raise the quiet logger level to logging.INFO + 1, which corresponds to the PLAIN level in the BBLogFormatter class.
--- bin/bitbake-setup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
bin/bitbake-setup +1 -1
@@ -1122,7 +1122,7 @@ def main(): if args.debug: logger.setLevel(logging.DEBUG) elif args.quiet: - logger.setLevel(logging.ERROR) + logger.setLevel(logging.INFO + 1) # BBLogFormatter.PLAIN level global BBSETUP_COLOR if args.color == 'always' or (args.color == 'auto' and sys.stdout.isatty() and os.environ.get('NO_COLOR', '') == ''):
Signature · 1 line
2.52.0
Re: [bitbake-devel] [PATCH 3/4] bitbake-setup: print plain messages in quiet mode
Quoted 5 lines · Antonin Godard via lists.openembedded.org
At the moment, plain messages are not printed when --quiet is passed on the command line. This makes no sense since bitbake-setup prints most of the essential information with the logging.PLAIN level. Raise the quiet logger level to logging.INFO + 1, which corresponds to the PLAIN level in the BBLogFormatter class.
Can you provide an example of e.g. 'init' before and after the change please? The idea is that quiet mode is indeed quiet, e.g. bitbake-setup really says nothing unless there's an error or warning that the user should know about.
Alex
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [bitbake-devel] [PATCH 3/4] bitbake-setup: print plain messages in quiet mode
Quoted 10 lines · Alexander Kanavin via lists.openembedded.org
Quoted 5 lines
At the moment, plain messages are not printed when --quiet is passed on the command line. This makes no sense since bitbake-setup prints most of the essential information with the logging.PLAIN level. Raise the quiet logger level to logging.INFO + 1, which corresponds to the PLAIN level in the BBLogFormatter class.
Can you provide an example of e.g. 'init' before and after the change please? The idea is that quiet mode is indeed quiet, e.g. bitbake-setup really says nothing unless there's an error or warning that the user should know about.
Or to put it another way, does the quiet mode do anything with this change? Is there a difference between the quiet and regular mode?
Alex
Re: [bitbake-devel] [PATCH 3/4] bitbake-setup: print plain messages in quiet mode
Hi,
Quoted 9 lines · Alexander Kanavin via lists.openembedded.org
Quoted 6 lines · Alexander Kanavin via lists.openembedded.org
Quoted 5 lines
At the moment, plain messages are not printed when --quiet is passed on the command line. This makes no sense since bitbake-setup prints most of the essential information with the logging.PLAIN level. Raise the quiet logger level to logging.INFO + 1, which corresponds to the PLAIN level in the BBLogFormatter class.
Quoted 7 lines
Quoted 4 lines
Can you provide an example of e.g. 'init' before and after the change please? The idea is that quiet mode is indeed quiet, e.g. bitbake-setup really says nothing unless there's an error or warning that the user should know about.
Or to put it another way, does the quiet mode do anything with this change? Is there a difference between the quiet and regular mode?
Actually, when I was testing things I was in interactive mode. Previously, passing --quiet in interactive mode would just leave you with nothing printed on the console (only warning and errors), which is strange for an interactive progem. However I forgot than in non-interactive mode you may want that to happen (nothing printed).
So maybe, we should look at the --non-interactive option value and print nothing (i.e. leave the current state, only warnings and errors are printed) when True, but raise the loglevel to have at least PLAIN messages when True? Or give the user a warning that running the init command shouldn't be run with --quiet?
Antonin
Re: [bitbake-devel] [PATCH 3/4] bitbake-setup: print plain messages in quiet mode
Quoted 10 lines · Antonin Godard
Actually, when I was testing things I was in interactive mode. Previously, passing --quiet in interactive mode would just leave you with nothing printed on the console (only warning and errors), which is strange for an interactive progem. However I forgot than in non-interactive mode you may want that to happen (nothing printed).
So maybe, we should look at the --non-interactive option value and print nothing (i.e. leave the current state, only warnings and errors are printed) when True, but raise the loglevel to have at least PLAIN messages when True? Or give the user a warning that running the init command shouldn't be run with --quiet?
I'd suggest that we start with setting clear criteria for using various printing/logging levels:
- any interactive selections and prompts should be using logger.verbnote (which is higher level than plain or info, but isn't a warning).
- regular messages right now are using a mix of plain and info - what's the difference and criteria for picking one or the other? Should they be unified, or do we still need to use both?
Then, modes could be defined like this: quiet mode prints only verbnote and higher. Regular mode prints info and higher. Debug mode prints debug and higher.
One possibility with modes is to actually remove --quiet altogether - is there really a use case for it? Bitbake-setup doesn't flood the console, and what it prints can be useful in finding out what went wrong, in addition to the actual error.
Alex
Re: [bitbake-devel] [PATCH 3/4] bitbake-setup: print plain messages in quiet mode
Quoted 13 lines · Alexander Kanavin via lists.openembedded.org
Quoted 10 lines · Antonin Godard
Actually, when I was testing things I was in interactive mode. Previously, passing --quiet in interactive mode would just leave you with nothing printed on the console (only warning and errors), which is strange for an interactive progem. However I forgot than in non-interactive mode you may want that to happen (nothing printed).
So maybe, we should look at the --non-interactive option value and print nothing (i.e. leave the current state, only warnings and errors are printed) when True, but raise the loglevel to have at least PLAIN messages when True? Or give the user a warning that running the init command shouldn't be run with --quiet?
Quoted 6 lines
I'd suggest that we start with setting clear criteria for using various printing/logging levels:
- any interactive selections and prompts should be using logger.verbnote (which is higher level than plain or info, but isn't a warning).
Agreed. I think verbnote messages appear with a "NOTE: " prefix though, which I don't think is necessary for bitbake-setup.
Quoted 3 lines
- regular messages right now are using a mix of plain and info - what's the difference and criteria for picking one or the other? Should they be unified, or do we still need to use both?
It was a bit arbitrary, but info messages seemed less "important" to me than plain messages (their "NOTE:" prefix is what this feels like to me at least).
Quoted 8 lines
Then, modes could be defined like this: quiet mode prints only verbnote and higher. Regular mode prints info and higher. Debug mode prints debug and higher.
One possibility with modes is to actually remove --quiet altogether - is there really a use case for it? Bitbake-setup doesn't flood the console, and what it prints can be useful in finding out what went wrong, in addition to the actual error.
I think it's ok to keep it, it shouldn't be hard to support and there might be a use-case for it for some people.
I will take this out of this series, as this requires more thought-processing. I will try to follow-up with some proposal when I have some time, including verbnote, etc.
Thanks!
Antonin
PATCH 4/4 bitbake-setup: print colored diffs
In reply to Antonin GodardUse the global BBSETUP_COLOR variable to enable color or not in diff outputs. When passing "auto" to the diff or git diff commands in subprocess tasks, the color is never shown, which is why we need to force it using "always" when BBSETUP_COLOR is True.
--- bin/bitbake-setup | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
bin/bitbake-setup +7 -3
@@ -196,7 +196,9 @@ be preserved in a backup directory.""".format(r_name, r_path)) status = bb.process.run('git -C {} status --porcelain'.format(r_path))[0] if status: return True - diff = bb.process.run('git -C {} diff {}'.format(r_path, rev))[0] + diff = bb.process.run('git -C {} diff --color={} {}'.format(r_path, + "always" if BBSETUP_COLOR else "never", + rev))[0] if diff: return True return False
@@ -722,7 +724,9 @@ def init_config(top_dir, settings, args): def get_diff(file1, file2): try: - bb.process.run('diff -uNr {} {}'.format(file1, file2)) + bb.process.run('diff --color={} -uNr {} {}'.format("always" if BBSETUP_COLOR else "never", + file1, + file2)) except bb.process.ExecutionError as e: if e.exitcode == 1: return e.stdout
@@ -780,7 +784,7 @@ def build_status(top_dir, settings, args, d, update=False): new_upstream_config = obtain_config(top_dir, registry, args, source_overrides, d) write_upstream_config(confdir, new_upstream_config) - config_diff = bb.process.run('git -C {} diff'.format(confdir))[0] + config_diff = bb.process.run('git -C {} diff --color={}'.format(confdir, "always" if BBSETUP_COLOR else "never"))[0] if config_diff: logger.plain('\nConfiguration in {} has changed:\n{}'.format(setupdir, config_diff))
Signature · 1 line
2.52.0
Re: [bitbake-devel] [PATCH 4/4] bitbake-setup: print colored diffs
bin/bitbake-setup def build_status(top_dir, settings, args, d, update=False): · line 787
Quoted 2 lines · Antonin Godard via lists.openembedded.org
bin/bitbake-setup +1
-1- config_diff = bb.process.run('git -C {} diff'.format(confdir))[0] + config_diff = bb.process.run('git -C {} diff --color={}'.format(confdir, "always" if BBSETUP_COLOR else "never"))[0]
These three color-enabling changes also should be split into a helper function:
run_git_diff(path, parameters)
Alex