| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | parser = ArgumentParser(description=__doc__) |
| 46 | parser.add_argument( |
| 47 | "--backend", action="append", |
| 48 | help=("backend to test; can be passed multiple times; defaults to the " |
| 49 | "default backend")) |
| 50 | parser.add_argument( |
| 51 | "--include-sgskip", action="store_true", |
| 52 | help="do not filter out *_sgskip.py examples") |
| 53 | parser.add_argument( |
| 54 | "--rundir", type=Path, |
| 55 | help=("directory from where the tests are run; defaults to a " |
| 56 | "temporary directory")) |
| 57 | parser.add_argument( |
| 58 | "paths", nargs="*", type=Path, |
| 59 | help="examples to run; defaults to all examples (except *_sgskip.py)") |
| 60 | args = parser.parse_args() |
| 61 | |
| 62 | root = Path(__file__).resolve().parent.parent / "examples" |
| 63 | paths = args.paths if args.paths else sorted(root.glob("**/*.py")) |
| 64 | if not args.include_sgskip: |
| 65 | paths = [path for path in paths if not path.stem.endswith("sgskip")] |
| 66 | relpaths = [path.resolve().relative_to(root) for path in paths] |
| 67 | width = max(len(str(relpath)) for relpath in relpaths) |
| 68 | for relpath in relpaths: |
| 69 | print(str(relpath).ljust(width + 1), end="", flush=True) |
| 70 | runinfos = [] |
| 71 | with ExitStack() as stack: |
| 72 | if args.rundir: |
| 73 | cwd = args.rundir / relpath.with_suffix("") |
| 74 | cwd.mkdir(parents=True) |
| 75 | else: |
| 76 | cwd = stack.enter_context(TemporaryDirectory()) |
| 77 | with tokenize.open(root / relpath) as src: |
| 78 | Path(cwd, relpath.name).write_text( |
| 79 | _preamble + src.read(), encoding="utf-8") |
| 80 | for backend in args.backend or [None]: |
| 81 | env = {**os.environ} |
| 82 | if backend is not None: |
| 83 | env["MPLBACKEND"] = backend |
| 84 | start = time.perf_counter() |
| 85 | proc = subprocess.run([sys.executable, relpath.name], |
| 86 | cwd=cwd, env=env) |
| 87 | elapsed = round(1000 * (time.perf_counter() - start)) |
| 88 | runinfos.append(RunInfo(backend, elapsed, proc.returncode)) |
| 89 | print("\t".join(map(str, runinfos))) |
| 90 | |
| 91 | |
| 92 | if __name__ == "__main__": |