-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_examples.py
82 lines (68 loc) · 2.46 KB
/
find_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
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))