-
Notifications
You must be signed in to change notification settings - Fork 2
Dispatch layup verbs from installed metadata, not PATH (#500) #506
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c6ab1d3
Dispatch layup verbs from installed metadata, not PATH (#500)
matthewholman df8cafb
Merge main into fix/500-cli-under-test
matthewholman 796d87a
Fold the entry-point lookup into find_layup_verbs()
matthewholman c8b08bd
Rewrite find_layup_verbs's docstring for a future reader
matthewholman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,24 @@ | ||
| import argparse | ||
| import subprocess | ||
| import sys | ||
| import shutil | ||
| import os | ||
| from importlib.metadata import distribution | ||
|
|
||
| # | ||
| # Generic verb dispatcher code | ||
| # | ||
|
|
||
|
|
||
| def find_layup_verbs(): | ||
| """Find available layup commands in the system's PATH.""" | ||
| layup_verbs = [] | ||
| for directory in os.environ.get("PATH", "").split(os.pathsep): | ||
| if os.path.isdir(directory): | ||
| for item in os.listdir(directory): | ||
| if item.startswith("layup-") and os.access(os.path.join(directory, item), os.X_OK): | ||
| layup_verbs.append(item[len("layup-") :]) | ||
| return sorted(set(layup_verbs)) | ||
| """Return the verbs this installation provides, as a dict mapping verb name | ||
| to entry point. | ||
|
|
||
| The names come from the installed package's own metadata, so they are the | ||
| verbs belonging to this layup, not whichever ones happen to be first on PATH. | ||
| """ | ||
| verbs = {} | ||
| for ep in distribution("layup").entry_points: | ||
| if ep.group == "console_scripts" and ep.name.startswith("layup-"): | ||
| verbs[ep.name[len("layup-") :]] = ep | ||
| return verbs | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this in a separate function? It's a bit of Russian doll to read though. These lines could easily be in find_layup_verbs() and it would say having to dive into a separate function |
||
|
|
||
|
|
||
| def main(): | ||
|
|
@@ -58,7 +59,7 @@ def main(): | |
| action="store_true", | ||
| ) | ||
|
|
||
| parser.add_argument("verb", nargs="?", choices=available_verbs, help="Verb to execute") | ||
| parser.add_argument("verb", nargs="?", choices=sorted(available_verbs), help="Verb to execute") | ||
| parser.add_argument("args", nargs=argparse.REMAINDER, help="Arguments for the verb") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
@@ -75,21 +76,27 @@ def main(): | |
| parser.print_help() | ||
| sys.exit(1) | ||
|
|
||
| # Construct the full command name | ||
| utility = f"layup-{args.verb}" | ||
|
|
||
| # Ensure the command is available | ||
| if not shutil.which(utility): | ||
| entry = available_verbs.get(args.verb) | ||
| if entry is None: | ||
| print(f"Error: '{utility}' is not available.") | ||
| sys.exit(1) | ||
|
|
||
| # Execute the command with the remaining arguments | ||
| # Run the verb in this process. Nothing is resolved by name, so the verb | ||
| # that runs is always the one belonging to this installation. | ||
| verb_main = entry.load() | ||
| argv = sys.argv | ||
| sys.argv = [utility, *args.args] | ||
| try: | ||
| result = subprocess.run([utility] + args.args, check=True) | ||
| sys.exit(result.returncode) | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"Error: Command '{utility}' failed with exit code {e.returncode}.") | ||
| sys.exit(e.returncode) | ||
| code = verb_main() | ||
| except SystemExit as exc: # the verbs exit on their own error paths | ||
| code = exc.code | ||
| finally: | ||
| sys.argv = argv | ||
| if code not in (0, None): | ||
| print(f"Error: Command '{utility}' failed with exit code {code}.") | ||
| sys.exit(code) | ||
| sys.exit(0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This whole docstring isn't what this code is doing adn is convoluted- it's what the AI thinks it fixed. I think it needs to be rewritten by a human for who is going to edit this code in 6 months or 6 years from now
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.
Could you please take charge of that, @mschwamb ?
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.
I'd get rid of that function and that would solve the issue. As per my other feedback comment.