-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.py
246 lines (193 loc) · 6.48 KB
/
dev.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
"""Developper CLI: building (meson) sources in place, document, etc."""
import os
import shutil
import subprocess
import sys
from pathlib import Path
import rich_click as click
if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib
BASE_DIR = Path(__file__).parent.absolute()
BUILD_DIR = BASE_DIR / "build"
SRC_PATH = BASE_DIR / "cherab"
DOC_ROOT = BASE_DIR / "docs"
ENVS = dict(os.environ)
N_CPUs = os.cpu_count()
@click.group()
def cli():
"""Developper CLI: building (meson) sources in place, document, etc."""
pass
############
@cli.command()
@click.option("--build-dir", default=str(BUILD_DIR), help="Relative path to the build directory")
@click.option(
"-j",
"--parallel",
default=N_CPUs,
show_default=True,
help="Number of parallel jobs for building.",
)
def build(build_dir: str, parallel: int):
"""Build package using Meson build tool and install editable mode.
\b
```python
Examples:
$ python dev.py build
```
"""
# === setup build ===============================================
cmd = ["meson", "setup", build_dir]
if Path(build_dir).exists():
cmd += ["--wipe"]
click.echo(" ".join([str(p) for p in cmd]))
ret = subprocess.call(cmd, env=ENVS, cwd=BASE_DIR)
if ret == 0:
print("Meson build setup OK")
else:
print("Meson build setup failed!")
with (Path(build_dir) / "meson-logs" / "meson-log.txt").open("r") as file:
print(file.read())
sys.exit(1)
# add __init__.py under cherab/
# meson-cython compilation does not handle PEP420 even though cython is 3.0.
# TODO: PEP420 handling
temp_init = SRC_PATH / "__init__.py"
with temp_init.open(mode="w") as file:
file.write("# This file is automatically generated by dev.py build CLI.")
# === build project =============================================
cmd = ["meson", "compile", "-C", build_dir, "-j", str(parallel)]
click.echo(" ".join([str(p) for p in cmd]))
ret = subprocess.call(cmd)
if ret == 0:
print("Build OK")
else:
print("Build failed!")
# delete temporary __init__.py
os.remove(temp_init)
sys.exit(1)
# === install .so/.pyd files in source tree ==========================
ext = ".pyd" if sys.platform == "win32" else ".so"
for so_path in BUILD_DIR.glob(f"**/*{ext}"):
src = so_path.resolve()
dst = BASE_DIR / so_path.relative_to(BUILD_DIR)
shutil.copy(src, dst)
print(f"copy {src} into {dst}")
print(f"Install {ext} files in place.")
# copy version.py
for version_path in BUILD_DIR.glob("**/version.py"):
src = version_path.resolve()
dst = BASE_DIR / version_path.relative_to(BUILD_DIR)
shutil.copy(src, dst)
print(f"copy {src} into {dst}")
# delete temporary __init__.py
os.remove(temp_init)
@cli.command()
def install():
"""Install package as the editalbe mode.
This command enables us to install the packages
as an editable mode with the setuptools functionality.
\b
```python
Examples:
$ python dev.py install
```
"""
# install the package
cmd = [sys.executable, "setup.py", "develop"]
click.echo(" ".join([str(p) for p in cmd]))
ret = subprocess.call(cmd)
if ret == 0:
print("Successfully installed.")
else:
print("install failed!")
sys.exit(1)
@cli.command()
def install_deps():
"""Install build dependencies using pip.
Only pip install cannot compile cython files appropriately, so we excute this command before
installing this package.
"""
# Load requires from pyproject.toml
pyproject = BASE_DIR / "pyproject.toml"
if not pyproject.exists():
raise FileNotFoundError("pyproject.toml must be placed at the root directory.")
with open(pyproject, "rb") as file:
conf = tomllib.load(file)
requires = conf["build-system"].get("requires")
subprocess.run([sys.executable, "-m", "pip", "install"] + requires)
############
@cli.command()
@click.argument("targets", default="html")
@click.option(
"-j",
"--parallel",
default=N_CPUs,
show_default=True,
help="Number of parallel jobs for building.",
)
def doc(parallel: int, targets: str):
""":wrench: Build documentation
TARGETS: Sphinx build targets [default: 'html']
"""
# move to docs/ and run command
os.chdir("docs")
builddir = DOC_ROOT / "build"
srcdir = DOC_ROOT
SPHINXBUILD = "sphinx-build"
if targets == "html":
cmd = [SPHINXBUILD, "-b", targets, f"-j{parallel}", str(srcdir), str(builddir / "html")]
elif targets == "clean":
cmd = ["rm", "-rf", str(builddir), "&&", "rm", "-rf", str(srcdir / "_api")]
elif targets == "help":
cmd = [SPHINXBUILD, "-M", targets, str(srcdir), str(builddir)]
else:
cmd = [SPHINXBUILD, "-M", targets, f"-j{parallel}", str(srcdir), str(builddir)]
click.echo(" ".join([str(p) for p in cmd]))
ret = subprocess.call(cmd)
if ret == 0:
print("sphinx-build successfully done.")
else:
print("Sphinx-build has errors.")
sys.exit(1)
############
@cli.command()
def format():
""":art: Run ruff linting & formatting The default options are defined in pyproject.toml."""
cmd = ["ruff", "check", "--fix", str(SRC_PATH)]
click.echo(" ".join([str(p) for p in cmd]))
ret = subprocess.call(cmd)
if ret == 0:
print("ruff formated")
else:
print("ruff formatting errors!")
sys.exit(1)
@cli.command()
def cython_lint():
""":art: Cython linter.
Checking all .pyx files in the source directory. The default options are defined at the cython-
lint table in pyproject.toml
"""
# list of .pyx files
pyx_files = [str(pyx_path) for pyx_path in SRC_PATH.glob("**/*.pyx")]
cmd = ["cython-lint"] + pyx_files
ret = subprocess.call(cmd)
if ret == 0:
print("cython-lint OK")
else:
print("cython-lint errors")
sys.exit(1)
#######
def config(tool: str):
"""Load configure data from pyproject.toml for tool table."""
pyproject = BASE_DIR / "pyproject.toml"
if not pyproject.exists():
raise FileNotFoundError("pyproject.toml must be placed at the root directory.")
with open(pyproject, "rb") as file:
conf = tomllib.load(file)
if not conf["tool"].get(tool):
raise ValueError(f"{tool} config data does not exist.")
return conf["tool"].get(tool)
if __name__ == "__main__":
cli()