forked from PyO3/rust-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.py
executable file
·199 lines (149 loc) · 5.05 KB
/
x.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
import argparse
import os
import subprocess
import sys
from pathlib import Path
def run(*args):
subprocess.run([*args], check=True)
def can_run(*args):
try:
subprocess.run([*args], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return True
except OSError:
return False
def nightly():
proc = subprocess.run(["rustc", "--version"], capture_output=True)
return b"-nightly " in proc.stdout
def gen_examples(manifest):
for dir_ in Path("examples").iterdir():
yield dir_ / manifest
LINT_CONFIG = (
"--deny",
"warnings",
# We usually want to make the GIL lifetime explicit.
"--deny",
"elided-lifetimes-in-paths",
"--allow",
"clippy::needless-lifetimes",
)
def default(args):
format_(args)
if nightly():
run(
"cargo",
"clippy",
"--all-features",
"--tests",
"--benches",
"--",
*LINT_CONFIG,
)
else:
run("cargo", "clippy", "--all-features", "--tests", "--", *LINT_CONFIG)
for manifest in gen_examples("Cargo.toml"):
run("cargo", "clippy", "--manifest-path", manifest, "--", *LINT_CONFIG)
run("cargo", "test", "--all-features", "--lib", "--tests")
def check(args):
run("cargo", "fmt", "--", "--check")
run(
"cargo",
"clippy",
"--all-features",
"--tests",
"--",
*LINT_CONFIG,
)
for manifest in gen_examples("Cargo.toml"):
run("cargo", "fmt", "--manifest-path", manifest, "--", "--check")
run(
"cargo",
"clippy",
"--manifest-path",
manifest,
"--",
*LINT_CONFIG,
)
def doc(args):
if args.name is None:
run("cargo", "test", "--all-features", "--doc")
else:
run("cargo", "test", "--all-features", "--doc", args.name)
if args.open:
run("cargo", "doc", "--all-features", "--open")
else:
run("cargo", "doc", "--all-features")
def test(args):
run("cargo", "test", "--all-features", "--lib")
if args.name is None:
run("cargo", "test", "--all-features", "--tests")
else:
run("cargo", "test", "--all-features", "--test", args.name)
def bench(args):
if not nightly():
sys.exit("Benchmarks require a nightly build of the Rust compiler.")
if args.name is None:
run("cargo", "bench", "--all-features", "--benches")
else:
run("cargo", "bench", "--all-features", "--bench", args.name)
def examples(args):
if not can_run("nox", "--version"):
sys.exit("Examples require the Nox tool (https://nox.thea.codes)")
if args.name is None:
for manifest in gen_examples("noxfile.py"):
run("nox", "--noxfile", manifest)
else:
run("nox", "--noxfile", f"examples/{args.name}/noxfile.py")
def format_(args):
if not can_run("black", "--version"):
sys.exit(
"Formatting requires the Black formatter (https://github.com/psf/black)"
)
run("cargo", "fmt")
for manifest in gen_examples("Cargo.toml"):
run("cargo", "fmt", "--manifest-path", manifest)
run("black", ".")
def prune(args):
run("git", "clean", "--force", "-x", ".")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
help="Default action is Rustfmt, Clippy and tests"
)
parser.set_defaults(func=default)
check_parser = subparsers.add_parser(
"check", aliases=["c"], help="Rustfmt and Clippy (as in the CI)"
)
check_parser.set_defaults(func=check)
doc_parser = subparsers.add_parser(
"doc", aliases=["d"], help="Rustdoc and doctests"
)
doc_parser.set_defaults(func=doc)
doc_parser.add_argument("name", nargs="?", help="Test case name")
doc_parser.add_argument(
"--open", "-o", action="store_true", help="Open documentation in browser"
)
test_parser = subparsers.add_parser("test", aliases=["t"], help="Integration tests")
test_parser.set_defaults(func=test)
test_parser.add_argument("name", nargs="?", help="Test target name")
bench_parser = subparsers.add_parser(
"bench", aliases=["b"], help="Benchmarks (requires nightly)"
)
bench_parser.set_defaults(func=bench)
bench_parser.add_argument("name", nargs="?", help="Benchmark target name")
examples_parser = subparsers.add_parser(
"examples", aliases=["e"], help="Examples (requires Nox)"
)
examples_parser.set_defaults(func=examples)
examples_parser.add_argument("name", nargs="?", help="Example directory name")
format_parser = subparsers.add_parser(
"format", aliases=["f"], help="Format Rust and Python code (requires Black)"
)
format_parser.set_defaults(func=format_)
prune_parser = subparsers.add_parser(
"prune", aliases=["p"], help="Remove target and venv directories"
)
prune_parser.set_defaults(func=prune)
args = parser.parse_args()
os.chdir(Path(__file__).parent)
args.func(args)