Skip to content

Commit

Permalink
add example finder
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs committed Aug 28, 2024
1 parent f562eba commit 7d57b82
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions find_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Script to make .sh file to run all examples.
Type of run can be controlled by including a comment starting with `//?` in
a file. For example, including:
```rust
//? mpirun -n {{NPROCESSES}} --features "mpi"
```
would make this script use mpirun with the -n flag set to a number of processes
and the "mpi" features turned on.
"""

import os
import argparse

parser = argparse.ArgumentParser(description='Parse inputs.')
parser.add_argument('--features', default=None,
help='feature flags to pass to the examples')

raw_features = parser.parse_args().features

features = []
if raw_features is not None:
features += raw_features.split(",")

root_dir = os.path.dirname(os.path.realpath(__file__))

files = []
example_dir = os.path.join(root_dir, "examples")
for file in os.listdir(example_dir):
if (
not file.startswith(".")
and file.endswith(".rs")
and os.path.isfile(os.path.join(example_dir, file))
):
files.append((os.path.join(example_dir, file), file[:-3]))

# Check that two examples do not share a name
for i, j in files:
assert len([a for a, b in files if b == j]) == 1

lines = []
for file, example_name in files:
with open(file) as f:
for line in f:
if line.startswith("//?"):
line = line[3:].strip()
if " " in line:
cmd, options = line.split(" ", 1)
else:
cmd = line
options = None
break
else:
cmd = "run"
options = None

if len(features) > 0:
if options is None:
options = ""
if "--features" in options:
a, b = options.split("--features \"")
options = f"{a}--features \"{','.join(features)},{b}"
else:
options += f" --features \"{','.join(features)}\""

command = f"cargo {cmd} --example {example_name} --release"
if options is not None:
command += f" {options}"
if "{{NPROCESSES}}" in command:
for n in [2, 4]:
info = f"Running {example_name} on {n} processes"
lines.append(f"echo \"\n{'=' * len(info)}\n{info}\n{'=' * len(info)}\n\"")
lines.append(command.replace("{{NPROCESSES}}", f"{n}"))
else:
info = f"Running {example_name}"
lines.append(f"echo \"\n{'=' * len(info)}\n{info}\n{'=' * len(info)}\n\"")
lines.append(command)

with open("examples.sh", "w") as f:
f.write(" && \\\n".join(lines))

0 comments on commit 7d57b82

Please sign in to comment.