-
Notifications
You must be signed in to change notification settings - Fork 1
Automatically add reload-* cmd that performs teardown-* + setup-*
#348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||||||||||||||||||||||||||
| import shutil | ||||||||||||||||||||||||||||||||||||||||||||
| import socket | ||||||||||||||||||||||||||||||||||||||||||||
| import pathlib | ||||||||||||||||||||||||||||||||||||||||||||
| import inspect | ||||||||||||||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||||||||||||||
| import datetime | ||||||||||||||||||||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -49,9 +50,31 @@ | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| COMMANDS = {} | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def add_reload_function(cmd): | ||||||||||||||||||||||||||||||||||||||||||||
| """Add a reload-cmd function that performs teardown-cmd + setup-cmd.""" | ||||||||||||||||||||||||||||||||||||||||||||
| teardown_func = COMMANDS.get(f'teardown_{cmd}') | ||||||||||||||||||||||||||||||||||||||||||||
| setup_func = COMMANDS.get(f'setup_{cmd}') | ||||||||||||||||||||||||||||||||||||||||||||
| def reload_func(*args, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||||
| if inspect.signature(teardown_func).parameters: | ||||||||||||||||||||||||||||||||||||||||||||
| teardown_func(*args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||
| teardown_func() | ||||||||||||||||||||||||||||||||||||||||||||
| return setup_func(*args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||
| reload_func_name = f'reload_{cmd}' | ||||||||||||||||||||||||||||||||||||||||||||
| hyphen_cmd = cmd.replace('_', '-') | ||||||||||||||||||||||||||||||||||||||||||||
| reload_func.__name__ = reload_func_name | ||||||||||||||||||||||||||||||||||||||||||||
| reload_func.__doc__ = f"Same as teardown-{hyphen_cmd} + setup-{hyphen_cmd}." | ||||||||||||||||||||||||||||||||||||||||||||
| COMMANDS[reload_func_name] = reload_func | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def cmd(func): | ||||||||||||||||||||||||||||||||||||||||||||
| """Decorator to add commands to the COMMANDS dict.""" | ||||||||||||||||||||||||||||||||||||||||||||
| COMMANDS[func.__name__] = func | ||||||||||||||||||||||||||||||||||||||||||||
| func_name = func.__name__ | ||||||||||||||||||||||||||||||||||||||||||||
| COMMANDS[func_name] = func | ||||||||||||||||||||||||||||||||||||||||||||
| if func_name.startswith('teardown_'): | ||||||||||||||||||||||||||||||||||||||||||||
| suffix = func_name.removeprefix('teardown_') | ||||||||||||||||||||||||||||||||||||||||||||
| if f'setup_{suffix}' in COMMANDS: | ||||||||||||||||||||||||||||||||||||||||||||
| # if both setup-* and teardown-* exist, add reload-* | ||||||||||||||||||||||||||||||||||||||||||||
| add_reload_function(suffix) | ||||||||||||||||||||||||||||||||||||||||||||
|
ezio-melotti marked this conversation as resolved.
Comment on lines
+73
to
+77
|
||||||||||||||||||||||||||||||||||||||||||||
| if func_name.startswith('teardown_'): | |
| suffix = func_name.removeprefix('teardown_') | |
| if f'setup_{suffix}' in COMMANDS: | |
| # if both setup-* and teardown-* exist, add reload-* | |
| add_reload_function(suffix) | |
| # If this is a setup_* or teardown_* command, and both sides of the pair | |
| # are registered, automatically add a corresponding reload_* command. | |
| suffix = None | |
| if func_name.startswith('teardown_'): | |
| suffix = func_name.removeprefix('teardown_') | |
| elif func_name.startswith('setup_'): | |
| suffix = func_name.removeprefix('setup_') | |
| if suffix: | |
| setup_name = f'setup_{suffix}' | |
| teardown_name = f'teardown_{suffix}' | |
| reload_name = f'reload_{suffix}' | |
| if setup_name in COMMANDS and teardown_name in COMMANDS and reload_name not in COMMANDS: | |
| # if both setup-* and teardown-* exist, add reload-* (once) | |
| add_reload_function(suffix) |
Copilot
AI
Mar 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that setup_or_teardown_sensors is no longer a CLI command, keeping @needs_root on it is redundant (it’s only called from @needs_root commands) and can be surprising if the helper is reused elsewhere (it may execvp sudo). Consider removing @needs_root from the helper and letting only the top-level commands enforce privilege escalation.
| @needs_root |
Copilot
AI
Mar 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that setup_or_teardown_display is no longer a CLI command, @needs_root on this helper is redundant (callers already enforce root) and can be surprising if reused (it may execvp sudo). Consider moving the root requirement to only the public setup-*/teardown-* commands and keeping helpers undecorated.
| @needs_root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reload_funcignores the result ofteardown_*and always proceeds tosetup_*, returning only the setup result. This can mask teardown failures (and can also run setup against a partially-torn-down state). Consider capturing the teardown return value and short-circuiting on an explicitFalseresult (while treatingNoneas success), and combining teardown+setup outcomes in the returned status.