-
-
Notifications
You must be signed in to change notification settings - Fork 134
Print Makefile help via Python; reorganize targets #526
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
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| Modified from https://github.com/cesium-ml/baselayer | ||
|
|
||
| Convert ## style comments after a Makefile target into help text. | ||
|
|
||
| Usage: makefile_to_help.py <MAKEFILE> | ||
|
|
||
| """ | ||
|
|
||
| import sys | ||
| import re | ||
|
|
||
|
|
||
| if not sys.argv: | ||
| print("Usage: makefile_to_help.py <MAKEFILE>") | ||
| sys.exit(0) | ||
|
|
||
|
|
||
| def describe_targets(lines): | ||
| matches = [re.match('^([\w-]+): +##(.*)', line) for line in lines] | ||
| groups = [m.groups(0) for m in matches if m] | ||
| targets = {target: desc for (target, desc) in groups} | ||
|
|
||
| N = max(len(target) for (target, desc) in targets.items()) | ||
|
|
||
| for (target, desc) in targets.items(): | ||
| print(f'{target:{N}} {desc}') | ||
|
|
||
|
|
||
| fname = sys.argv[1] | ||
|
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. If sys.argv[1] is a path that doesn't exist, Python raises an unguarded FileNotFoundError. A simple check would give a cleaner error message. |
||
| describe_targets(open(fname).readlines()) | ||
|
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. The file handle is never explicitly closed. Use a with block. |
||
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.
sys.argv always contains at least the script name (sys.argv[0]), so this guard is never triggered. If the script is
called with no argument, it falls through to sys.argv[1] on the last line and crashes with an IndexError instead of printing the usage message.
Suggested fix
if len(sys.argv) < 2:
print("Usage: makefile_to_help.py ")
sys.exit(1) # exit code 1 for error, not 0