|
| 1 | +""" |
| 2 | +Script wrapper for scripts. |
| 3 | +
|
| 4 | +This allows scripts to be invoked through the scripts module. |
| 5 | +
|
| 6 | +The exposed interface is just running the internal files. |
| 7 | +Whatever interface they have, is what is shown. |
| 8 | +""" |
| 9 | + |
| 10 | +import functools |
| 11 | +import runpy |
| 12 | +import sys |
| 13 | + |
| 14 | +import click |
| 15 | + |
| 16 | + |
| 17 | +# key: alias |
| 18 | +# value: tuple of module name, help description |
| 19 | +commands: "dict[str, tuple[str, str | None]]" = { |
| 20 | + "export_req": ("scripts.export_requirements", "Export requirements to requirements.txt"), |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +def run_script(module_name: str, *args, **kwargs) -> None: |
| 25 | + """ |
| 26 | + Run the provided module, with the provided args and kwargs. |
| 27 | +
|
| 28 | + The provided defaults are what makes the environment nearly the same as if module was invoked directly. |
| 29 | + """ |
| 30 | + kwargs.setdefault("run_name", "__main__") |
| 31 | + kwargs.setdefault("alter_sys", True) |
| 32 | + runpy.run_module(module_name, **kwargs) |
| 33 | + |
| 34 | + |
| 35 | +@click.group() |
| 36 | +@click.help_option("-h", "--help") |
| 37 | +def cli() -> None: |
| 38 | + """ |
| 39 | + Custom scripts which help modmail development. |
| 40 | +
|
| 41 | + All custom scripts should be listed below as a command, with a description. |
| 42 | + In addition, some built in modules may be listed below as well. |
| 43 | + If a custom script is not shown below please open an issue. |
| 44 | + """ |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +def main(cmd: str = None) -> None: |
| 49 | + """Add the commands and run the cli.""" |
| 50 | + if cmd is None: |
| 51 | + cmd = [] |
| 52 | + for k, v in commands.items(): |
| 53 | + func = functools.partial(run_script, v[0]) |
| 54 | + cli.add_command(click.Command(k, help=v[1], callback=func)) |
| 55 | + cli.main(cmd, standalone_mode=False) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + try: |
| 60 | + cmd = [sys.argv[1]] |
| 61 | + sys.argv.pop(1) # pop the first arg out of sys.argv since its being used to get the name |
| 62 | + except IndexError: |
| 63 | + cmd = [] |
| 64 | + try: |
| 65 | + main(cmd) |
| 66 | + except click.ClickException as e: |
| 67 | + e.show() |
| 68 | + sys.exit() |
0 commit comments