-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_gen.py
executable file
·72 lines (57 loc) · 2.31 KB
/
api_gen.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
"""Script to generate public APIs in the `keras_rs/api` directory.
Usage:
Run via `./shell/api_gen.sh`.
It generates API and formats user and generated APIs.
"""
import os
import shutil
import namex
PACKAGE = "keras_rs"
BUILD_DIR_NAME = "tmp_build_dir"
def ignore_files(_: str, filenames: list[str]) -> list[str]:
return [f for f in filenames if f.endswith("_test.py")]
def copy_source_to_build_directory(root_path: str) -> str:
# Copy sources (`keras_rs/` dir and setup files) to build dir.
build_dir = os.path.join(root_path, BUILD_DIR_NAME)
build_package_dir = os.path.join(build_dir, PACKAGE)
build_src_dir = os.path.join(build_package_dir, "src")
root_src_dir = os.path.join(root_path, PACKAGE, "src")
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
os.makedirs(build_package_dir)
shutil.copytree(root_src_dir, build_src_dir)
return build_dir
def export_version_string(api_init_fname: str) -> None:
with open(api_init_fname) as f:
contents = f.read()
with open(api_init_fname, "w") as f:
contents += (
"from keras_rs.src.version import __version__ as __version__\n"
)
f.write(contents)
def build() -> None:
root_path = os.path.dirname(os.path.abspath(__file__))
code_api_dir = os.path.join(root_path, PACKAGE, "api")
# Create temp build dir
build_dir = copy_source_to_build_directory(root_path)
build_api_dir = os.path.join(build_dir, PACKAGE)
build_src_dir = os.path.join(build_api_dir, "src")
build_api_init_fname = os.path.join(build_api_dir, "__init__.py")
try:
os.chdir(build_dir)
# Generates `keras_rs/api` directory.
open(build_api_init_fname, "w").close()
namex.generate_api_files("keras_rs", code_directory="src")
# Add `__version__` to `keras_rs/__init__.py`.
export_version_string(build_api_init_fname)
# Copy back `keras_rs` from build dir to `api` excluding `src/`.
if os.path.exists(build_src_dir):
shutil.rmtree(build_src_dir)
if os.path.exists(code_api_dir):
shutil.rmtree(code_api_dir)
shutil.copytree(build_api_dir, code_api_dir)
finally:
# Clean up: remove the build directory (no longer needed).
shutil.rmtree(build_dir)
if __name__ == "__main__":
build()