-
-
Save pushfoo/51e1722e7a04cf8062cf2c1be636c7de to your computer and use it in GitHub Desktop.
A low-dependency Python make.py
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
#!/usr/bin/env python | |
""" | |
Low-dependency build script. | |
Run ./make.py for usage info and command listing. | |
""" | |
from __future__ import annotations | |
import subprocess | |
import sys | |
def stderr(*args, **kwargs): | |
print(*args, **kwargs, file=sys.stderr) | |
commands = {} | |
def command(f): | |
"""A minimal `typer` replacement.""" | |
commands[f.__name__.replace("_", "-")] = f | |
return f | |
@command | |
def sync_tests(**_): | |
"""Generate updated .output files for integration tests. | |
See sync_test_output.py to learn more. | |
""" | |
subprocess.run(['python', 'sync_test_output.py']) | |
@command | |
def test(**_): | |
"""Run pytest.""" | |
subprocess.run(['pytest', 'tests']) | |
@command | |
def typecheck(**_): | |
"""Type-check with pyright.""" | |
subprocess.run(['pyright', 'module_name']) | |
@command | |
def help(print_func=print, **_): | |
"""Print usage information. | |
Args: | |
print_func: | |
Allow printing usage to: | |
1. `stdout` for normal help invocations | |
2. `stderr` when something goes wrong | |
""" | |
command_width = max(len(k) for k in commands) | |
command_prefix = args.arv[0] | |
print_func(__doc__.strip(), end="\n\n") | |
for name, func in commands.items(): | |
docstring = getattr(func, '__doc__', '[ no docstring ]') | |
doc_line_0 = doc.split("\n")[0] | |
print_func(f"{command_prefix} {name:<{command_width}} {doc_line_0}") | |
def main(): | |
problem = 0 | |
kwargs = dict(print_func=print) | |
if len(sys.argv) < 2: | |
command_name = 'help' | |
elif (command_name := sys.argv[1]) not in commands: | |
problem = 1 | |
stderr(f"ERROR: unknown command {command_name!r}") | |
command_name = 'help' | |
kwargs.update(print_func=stderr) | |
run_command = commands.get(command_name) | |
run_command(**kwargs) | |
exit(problem) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment