-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx.py
354 lines (302 loc) · 10.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
self_path = __import__("pathlib").Path(__file__).parent.resolve()
python = __import__("shlex").quote(__import__("sys").executable)
__effect = lambda effect: lambda func: [func, effect(func.__dict__)][0]
cmd = lambda **kw: __effect(lambda d: d.setdefault("@cmd", {}).update(kw))
arg = lambda *a, **kw: __effect(lambda d: d.setdefault("@arg", []).append((a, kw)))
all_arrow_features = [
# arrow-version:insert: "arrow-{version}",
"arrow-54",
"arrow-53",
"arrow-52",
"arrow-51",
"arrow-50",
"arrow-50",
"arrow-49",
"arrow-48",
"arrow-47",
"arrow-46",
"arrow-45",
"arrow-44",
"arrow-43",
"arrow-42",
"arrow-41",
"arrow-40",
"arrow-39",
"arrow-38",
"arrow-37",
]
all_arrow2_features = ["arrow2-0-17", "arrow2-0-16"]
default_features = f"serde,{all_arrow2_features[0]},{all_arrow_features[0]}"
workflow_test_template = lambda: {
"name": "Test",
"on": {
"workflow_dispatch": {},
"pull_request": {
"branches": ["main", "develop-*"],
"types": [
"opened",
"edited",
"reopened",
"ready_for_review",
"synchronize",
],
},
},
"env": {"CARGO_TERM_COLOR": "always"},
"jobs": {
"test": {
"runs-on": "ubuntu-latest",
"steps": [
{"uses": "actions/checkout@v4"},
*_workflow_check_steps(),
],
}
},
}
workflow_release_template = lambda: {
"name": "Release",
"on": {
"release": {"types": ["published"]},
},
"env": {"CARGO_TERM_COLOR": "always"},
"jobs": {
"release": {
"runs-on": "ubuntu-latest",
"env": {
"CARGO_REGISTRY_TOKEN": "${{ secrets.CARGO_REGISTRY_TOKEN }}",
},
"steps": [
{"uses": "actions/checkout@v4"},
*_workflow_check_steps(),
{
"name": "Publish to crates.io",
"working-directory": "marrow",
"run": "cargo publish",
},
],
}
},
}
@cmd(help="Run all common development tasks before a commit")
@arg("--backtrace", action="store_true", default=False)
def precommit(backtrace=False):
update_workflows()
format()
check()
test(backtrace=backtrace)
@cmd(help="Update the github workflows")
def update_workflows():
_update_json_file(
self_path / ".github" / "workflows" / "test.yml",
workflow_test_template(),
)
_update_json_file(
self_path / ".github" / "workflows" / "release.yml",
workflow_release_template(),
)
def _update_json_file(path, content):
import json
print(f":: update {path}")
with open(path, "wt", encoding="utf8", newline="\n") as fobj:
json.dump(content, fobj, indent=2)
def _workflow_check_steps():
return [
{"name": "system", "run": "uname -a"},
{"name": "rustc", "run": "rustc --version"},
{"name": "cargo", "run": "cargo --version"},
{
"name": "Check format",
"run": "cargo fmt --check",
},
{"name": "Check", "run": "cargo check"},
*(
{
"name": f"Check {feature}",
"run": f"cargo check -p marrow --features {feature}",
}
for feature in ("serde", *all_arrow2_features, *all_arrow_features)
),
{"name": "Check", "run": "cargo check --all-features"},
{
"name": "Build",
"run": "cargo build --all-features",
},
{
"name": "Test",
"run": "cargo test --all-features",
},
]
@cmd(help="Format the code")
def format():
_sh(f"{python} -m black {_q(__file__)}")
_sh("cargo fmt")
# the impl files are not found by cargo fmt
impl_files = [
*self_path.joinpath("marrow", "src", "impl_arrow").glob("impl*.rs"),
*self_path.joinpath("marrow", "src", "impl_arrow2").glob("impl*.rs"),
*self_path.joinpath("test_with_arrow", "src", "tests").glob("*.rs"),
]
_sh(f"rustfmt {_q(impl_files)}")
@cmd(help="Run the linters")
@arg("--all", action="store_true")
def check(all=False):
check_cargo_toml()
_sh(f"cargo check --all-features")
_sh(f"cargo clippy --all-features")
if all:
for features in ("serde", *all_arrow2_features, *all_arrow_features):
_sh(f"cargo check -p marrow --features {features}")
@cmd(help="Run the tests")
@arg(
"--backtrace",
action="store_true",
default=False,
help="If given, print a backtrace on error",
)
@arg(
"--all",
action="store_true",
default=False,
help="If given, run all feature combinations",
)
def test(backtrace=False, all=False):
feature_selection = (
f"--features {default_features}" if not all else "--all-features"
)
env = {"RUST_BACKTRACE": "1"} if backtrace else {}
_sh("cargo test --features serde", env=env)
_sh(f"cargo test {feature_selection}", env=env)
@cmd(help="Generate the documentation")
@arg("--private", action="store_true", default=False)
@arg("--open", action="store_true", default=False)
def doc(private=False, open=False):
_sh(
f"""
cargo doc
--features {default_features}
{'--document-private-items' if private else ''}
{'--open' if open else ''}
""",
)
@cmd()
def check_cargo_toml():
import tomli
print(":: check Cargo.toml")
with open(self_path / "marrow" / "Cargo.toml", "rb") as fobj:
config = tomli.load(fobj)
for label, features in [
(
"docs.rs configuration",
config["package"]["metadata"]["docs"]["rs"]["features"],
),
*[
(f"test {target['name']}", target["required-features"])
for target in config.get("test", [])
],
*[
(f"bench {target['name']}", target["required-features"])
for target in config.get("bench", [])
],
]:
actual_features = sorted(features)
expected_features = sorted(default_features.split(","))
if actual_features != expected_features:
raise ValueError(
f"Invalid {label}. "
f"Expected: {expected_features}, found: {actual_features}"
)
# TODO: check the features / dependencies
for feature in all_arrow_features:
*_, version = feature.partition("-")
actual_feature_def = sorted(config["features"][feature])
expected_feature_def = sorted(
[
f"dep:arrow-array-{version}",
f"dep:arrow-schema-{version}",
f"dep:arrow-data-{version}",
f"dep:arrow-buffer-{version}",
]
)
if actual_feature_def != expected_feature_def:
raise ValueError(
f"Invalid feature definition for {feature}. "
f"Expected: {expected_feature_def}, found: {actual_feature_def}"
)
for component in ["arrow-array", "arrow-schema", "arrow-data", "arrow-buffer"]:
expected_dep = {
"package": component,
"version": version,
"optional": True,
"default-features": False,
}
actual_dep = config["dependencies"].get(f"{component}-{version}")
if actual_dep is None:
raise ValueError(f"Missing dependency {component}-{version}")
if actual_dep != expected_dep:
raise ValueError(
f"Invalid dependency {component}-{version}. "
f"Expected: {expected_dep}, found: {actual_dep}"
)
for name, dep in config["dependencies"].items():
if dep.get("default-features", True):
raise ValueError(f"Default features for {name} not deactivated")
@cmd(help="Add a new arrow version")
@arg("version")
def add_arrow_version(version):
import re
if _sh("git diff-files --quiet", check=False).returncode != 0:
print(
"WARNING: potentially destructive changes. "
"Please stage or commit the working tree first."
)
raise SystemExit(1)
for p in [
self_path / "x.py",
*self_path.glob("*/**/*.rs"),
*self_path.glob("*/**/*.toml"),
]:
content = p.read_text()
if "arrow-version" not in content:
continue
print(f"process {p}")
new_content = []
include_next = True
for line in content.splitlines():
if (
m := re.match(r"^.*(//|#) arrow-version:(replace|insert): (.*)$", line)
) is not None:
new_content.append(line)
new_content.append(
m.group(3).format_map({"version": version, "\\n": "\n"})
)
include_next = m.group(2) != "replace"
else:
if include_next:
new_content.append(line)
include_next = True
with open(p, "wt", newline="\n", encoding="utf-8") as fobj:
fobj.write("\n".join(new_content))
format()
update_workflows()
_sh = lambda c, env=(), **kw: __import__("subprocess").run(
[args := __import__("shlex").split(c.replace("\n", " ")), print("::", *args)][0],
**{
"check": True,
"cwd": self_path,
"encoding": "utf-8",
"env": {**__import__("os").environ, **dict(env)},
**kw,
},
)
_q = (
lambda a: __import__("shlex").quote(str(a))
if not isinstance(a, (tuple, list))
else " ".join(_q(i) for i in a)
)
if __name__ == "__main__":
_sps = (_p := __import__("argparse").ArgumentParser()).add_subparsers()
for _f in (f for _, f in sorted(globals().items()) if hasattr(f, "@cmd")):
_kw = {"name": _f.__name__.replace("_", "-"), **getattr(_f, "@cmd")}
(_sp := _sps.add_parser(**_kw)).set_defaults(_=_f)
[_sp.add_argument(*a, **kw) for a, kw in reversed(getattr(_f, "@arg", []))]
(_a := vars(_p.parse_args())).pop("_", _p.print_help)(**_a)