|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | + |
| 4 | +from subprocess import run, CalledProcessError |
| 5 | + |
| 6 | +try: |
| 7 | + import click |
| 8 | +except ImportError: |
| 9 | + print("Unable to import required dependency: click", file=sys.stderr) |
| 10 | + sys.exit(2) |
| 11 | + raise AssertionError |
| 12 | + |
| 13 | +from click import ClickException |
| 14 | + |
| 15 | +if sys.version_info[:2] < (3, 9): |
| 16 | + print("Unsupported python version:", '.'.join(sys.version_info[:3]), file=sys.stderr) |
| 17 | + print("Requires at least Python 3.9") |
| 18 | + sys.exit(2) |
| 19 | + raise AssertionError |
| 20 | + |
| 21 | +from dataclasses import dataclass |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class ExampleData: |
| 25 | + package: str |
| 26 | + args: list[str] |
| 27 | + |
| 28 | +EXAMPLES = { |
| 29 | + "binary_trees": ExampleData(package='zerogc-simple', args=['12']), |
| 30 | + "binary_trees_parallel": ExampleData(package='zerogc-simple', args=['12']), |
| 31 | +} |
| 32 | + |
| 33 | +def normalize_example(name): |
| 34 | + return name.replace('-', '_') |
| 35 | + |
| 36 | +def print_seperator(): |
| 37 | + print() |
| 38 | + print('-' * 8) |
| 39 | + print() |
| 40 | + |
| 41 | +@click.command() |
| 42 | +@click.option('--all', is_flag=True, help="Run **all** the examples") |
| 43 | +@click.option('--example', '-e', 'examples', multiple=True, help="The name of the example to run") |
| 44 | +@click.option('--list', '-l', 'list_examples', is_flag=True, help="List available examples") |
| 45 | +@click.option('--release', is_flag=True, help="Compile code in release mode") |
| 46 | +def run_examples(list_examples: bool, examples: list[str], all: bool, release: bool): |
| 47 | + if all: |
| 48 | + if examples: |
| 49 | + raise ClickException("Should not specify explicit examples along with `-all`") |
| 50 | + else: |
| 51 | + examples = sorted(EXAMPLES.keys()) |
| 52 | + if list_examples and examples: |
| 53 | + raise ClickException("Should not specify any examples along with '--list'") |
| 54 | + if not examples: |
| 55 | + # Imply '--list' if nothing else is specified |
| 56 | + list_examples = True |
| 57 | + if list_examples: |
| 58 | + click.echo("Listing available examples: [Type --help for more info]") |
| 59 | + for example in EXAMPLES.keys(): |
| 60 | + click.echo(f" {example}") |
| 61 | + sys.exit() |
| 62 | + # Normalize all names |
| 63 | + examples = list(map(normalize_example, examples)) |
| 64 | + extra_cargo_args = [] |
| 65 | + if release: |
| 66 | + extra_cargo_args += ['--release'] |
| 67 | + for example_name in examples: |
| 68 | + if example_name not in EXAMPLES: |
| 69 | + raise ClickException("Invalid example name: {example_name}") |
| 70 | + for example_name in examples: |
| 71 | + example = EXAMPLES[example_name] |
| 72 | + print(f"Compiling example: {example_name}") |
| 73 | + try: |
| 74 | + run(["cargo", "build", "--example", example_name, '-p', example.package, *extra_cargo_args], check=True) |
| 75 | + except CalledProcessError as e: |
| 76 | + raise ClickException(f"Failed to compile {example_name}") |
| 77 | + print_seperator() |
| 78 | + for index, example_name in enumerate(examples): |
| 79 | + example = EXAMPLES[example_name] |
| 80 | + print("Running example: {example_name}") |
| 81 | + try: |
| 82 | + run(["cargo", "run", "--example", example_name, '-p', example.package, *extra_cargo_args, '--', *example.args], check=True) |
| 83 | + except CalledProcessError: |
| 84 | + raise ClickException(f"Failed to run example: {example_name}") |
| 85 | + if index + 1 != len(examples): |
| 86 | + print_seperator() |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + run_examples() |
0 commit comments