-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrefresh_nexus_base_classes.py
More file actions
77 lines (59 loc) · 1.91 KB
/
Copy pathrefresh_nexus_base_classes.py
File metadata and controls
77 lines (59 loc) · 1.91 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
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
"""Regenerate ``src/chexus/nexus_base_classes.py`` from the upstream NeXus
definitions repository.
Requires the GitHub CLI (``gh``) to be installed and authenticated.
Usage:
python tools/refresh_nexus_base_classes.py
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
REPO = "nexusformat/definitions"
DIRECTORY = "base_classes"
SUFFIX = ".nxdl.xml"
TARGET = (
Path(__file__).resolve().parent.parent / "src" / "chexus" / "nexus_base_classes.py"
)
HEADER = '''# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
"""NeXus base class names.
Source: https://github.com/nexusformat/definitions/tree/main/base_classes
Regenerated by ``tools/refresh_nexus_base_classes.py``; do not edit by hand.
Includes deprecated classes (NXgeometry, NXorientation, NXshape, NXtranslation),
which are flagged separately by NX_class_is_legacy.
"""
nexus_base_classes = frozenset(
[
'''
FOOTER = """ ]
)
"""
def fetch_names() -> list[str]:
result = subprocess.run(
["gh", "api", f"repos/{REPO}/contents/{DIRECTORY}", "--jq", ".[].name"],
capture_output=True,
text=True,
check=True,
)
names = [
line.removesuffix(SUFFIX)
for line in result.stdout.splitlines()
if line.endswith(SUFFIX)
]
return sorted(names)
def render(names: list[str]) -> str:
body = "".join(f' "{name}",\n' for name in names)
return HEADER + body + FOOTER
def main() -> int:
names = fetch_names()
if not names:
print("No base classes returned from gh api", file=sys.stderr)
return 1
TARGET.write_text(render(names))
print(f"Wrote {len(names)} base classes to {TARGET}")
return 0
if __name__ == "__main__":
sys.exit(main())