-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·79 lines (59 loc) · 2 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·79 lines (59 loc) · 2 KB
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
#!/usr/bin/env python3
"""Convert `data/` TOML files into bundled and per-crate JSON in `build/`."""
import json
import shutil
import sys
import tomllib
from pathlib import Path
ROOT = Path(__file__).resolve().parent
DATA_DIR = ROOT / "data"
BUILD_DIR = ROOT / "build"
REQUIRED_FIELDS = {"description", "url"}
def validate(path, entry):
"""Return a list of problems with the fields of a single entry."""
fields = set(entry)
errors = []
for name in sorted(REQUIRED_FIELDS - fields):
errors.append(f"{path.name}: missing field: {name}")
for name in sorted(fields - REQUIRED_FIELDS):
errors.append(f"{path.name}: unexpected field: {name}")
return errors
def main():
crates = {}
errors = []
for path in sorted(DATA_DIR.glob("*.toml")):
try:
with path.open("rb") as f:
entry = tomllib.load(f)
except tomllib.TOMLDecodeError as e:
errors.append(f"{path.name}: invalid TOML: {e}")
continue
problems = validate(path, entry)
if problems:
errors.extend(problems)
continue
crates[path.stem] = {
"description": entry["description"].strip(),
"url": entry["url"],
}
if errors:
for error in errors:
print(error, file=sys.stderr)
sys.exit(1)
BUILD_DIR.mkdir(exist_ok=True)
out = BUILD_DIR / "all.json"
with out.open("w") as f:
json.dump(crates, f, indent=2, sort_keys=True)
f.write("\n")
print(f"Wrote {len(crates)} crates to {out.relative_to(ROOT)}")
crates_dir = BUILD_DIR / "crates"
if crates_dir.exists():
shutil.rmtree(crates_dir)
crates_dir.mkdir()
for name, entry in crates.items():
with (crates_dir / f"{name}.json").open("w") as f:
json.dump(entry, f, indent=2, sort_keys=True)
f.write("\n")
print(f"Wrote {len(crates)} per-crate files to {crates_dir.relative_to(ROOT)}")
if __name__ == "__main__":
main()