update test and build scripts

This commit is contained in:
ykcheungab
2026-03-09 13:33:18 +08:00
parent 36118ab9cd
commit fa76d66162
19 changed files with 472 additions and 234 deletions
+61 -70
View File
@@ -1,42 +1,63 @@
import os
import sys
import subprocess
import shutil
import subprocess
from pathlib import Path
from importlib.machinery import EXTENSION_SUFFIXES
def install_nuitka():
print("Checking Nuitka installation...")
try:
import nuitka
import nuitka # noqa: F401
print("Nuitka is installed.")
except ImportError:
print("Installing Nuitka...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "nuitka", "ordered-set", "zstandard"])
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "nuitka", "ordered-set", "zstandard"]
)
def get_version() -> str:
init_file = Path("src/orisys/__init__.py")
if not init_file.exists():
return "0.3.0"
for line in init_file.read_text(encoding="utf-8").splitlines():
if line.startswith("__version__"):
return line.split("=", 1)[1].strip().strip('"').strip("'")
return "0.3.0"
def find_compiled_extension(output_dir: Path) -> Path:
candidates = []
for suffix in EXTENSION_SUFFIXES:
candidates.extend(output_dir.glob(f"*{suffix}"))
if not candidates:
all_files = ", ".join(p.name for p in output_dir.iterdir()) if output_dir.exists() else "(none)"
raise RuntimeError(f"Nuitka did not produce a compiled extension. Found: {all_files}")
# Prefer the shortest filename (usually the package extension itself)
return sorted(candidates, key=lambda p: len(p.name))[0]
def build():
install_nuitka()
version = "0.3.0"
version = get_version()
src_dir = Path("src/orisys")
output_dir = Path("build_nuitka")
dist_dir = Path("dist_nuitka")
# Clean previous builds
if output_dir.exists(): shutil.rmtree(output_dir)
if dist_dir.exists(): shutil.rmtree(dist_dir)
if output_dir.exists():
shutil.rmtree(output_dir)
if dist_dir.exists():
shutil.rmtree(dist_dir)
output_dir.mkdir()
dist_dir.mkdir()
print("\n=== Compiling with Nuitka ===")
# Set PYTHONPATH to include src so nuitka finds the package
env = os.environ.copy()
env["PYTHONPATH"] = "src" + os.pathsep + env.get("PYTHONPATH", "")
# Nuitka arguments
# --module: compile as module/package
# --include-package=orisys: include the whole package
# --output-dir: where to put result
# src/orisys: the target directory
cmd = [
sys.executable, "-m", "nuitka",
"--module",
@@ -44,76 +65,48 @@ def build():
"--output-dir=" + str(output_dir),
"--remove-output",
"--no-pyi-file",
"src/orisys"
"src/orisys",
]
print(f"Running: {' '.join(cmd)}")
subprocess.check_call(cmd, env=env)
# Find the generated .pyd file
# It should be something like orisys.cp310-win_amd64.pyd
pyd_files = list(output_dir.glob("*.pyd"))
if not pyd_files:
raise RuntimeError("Nuitka did not produce a .pyd file!")
pyd_file = pyd_files[0]
print(f"Generated compiled extension: {pyd_file}")
ext_file = find_compiled_extension(output_dir)
ext_suffix = "".join(ext_file.suffixes)
print(f"Generated compiled extension: {ext_file}")
print("\n=== Preparing Wheel Structure ===")
# We want the structure:
# orisys/
# __init__.pyd <-- renamed from orisys.pyd
# configs/
# *.json
wheel_root = dist_dir / "wheel_root"
pkg_dir = wheel_root / "orisys"
pkg_dir.mkdir(parents=True)
# Move and rename pyd
dest_pyd = pkg_dir / "__init__.pyd"
shutil.copy(pyd_file, dest_pyd)
print(f"Copied to {dest_pyd}")
# Copy JSON configs
dest_ext = pkg_dir / f"__init__{ext_suffix}"
shutil.copy(ext_file, dest_ext)
print(f"Copied compiled package to {dest_ext}")
configs_src = src_dir / "configs"
configs_dst = pkg_dir / "configs"
configs_dst.mkdir()
json_count = 0
for json_file in configs_src.glob("*.json"):
shutil.copy(json_file, configs_dst)
json_count += 1
print(f"Copied {json_count} JSON config files")
# Create and copy examples folder (as part of the package)
examples_dst = pkg_dir / "examples"
examples_dst.mkdir(exist_ok=True)
# Create __init__.py for examples package
examples_init = examples_dst / "__init__.py"
with open(examples_init, 'w') as f:
f.write("# Examples for orisys package\n")
example_files = [
Path("example1_basic_pipeline.py"),
Path("example2_two_sensor_pipeline.py"),
]
(examples_dst / "__init__.py").write_text("# Examples for orisys package\n", encoding="utf-8")
example_files = [Path("example1_basic_pipeline.py"), Path("example2_two_sensor_pipeline.py")]
example_count = 0
for example_file in example_files:
if example_file.exists():
shutil.copy(example_file, examples_dst)
example_count += 1
print(f"Copied example: {example_file.name}")
else:
print(f"Warning: Example file not found: {example_file}")
print(f"Copied {example_count} example files to orisys/examples/")
# Create setup.py for packaging
setup_py_path = dist_dir / "setup.py"
setup_content = f"""
from setuptools import setup, Distribution, find_packages
from setuptools import setup, Distribution
class BinaryDistribution(Distribution):
def has_ext_modules(self):
@@ -127,7 +120,7 @@ setup(
packages=['orisys', 'orisys.examples'],
package_dir={{'': 'wheel_root'}},
package_data={{
'orisys': ['*.pyd', 'configs/*.json'],
'orisys': ['*{ext_suffix}', 'configs/*.json'],
'orisys.examples': ['*.py'],
}},
include_package_data=True,
@@ -139,26 +132,24 @@ setup(
extras_require={{
"cuda12": ["cupy-cuda12x>=12.3.0"],
"cuda13": ["cupy-cuda13x>=13.0.0"],
"cuda": ["cupy-cuda12x>=12.3.0"], # For backward compatibility, defaults to CUDA 12.x
"cuda": ["cupy-cuda12x>=12.3.0"],
}},
distclass=BinaryDistribution,
zip_safe=False,
)
"""
with open(setup_py_path, "w") as f:
f.write(setup_content)
setup_py_path.write_text(setup_content, encoding="utf-8")
print("\n=== Building Wheel ===")
subprocess.check_call([sys.executable, "setup.py", "bdist_wheel"], cwd=dist_dir)
# Find the wheel
dist_output = dist_dir / "dist"
wheels = list(dist_output.glob("*.whl"))
wheels = list((dist_dir / "dist").glob("*.whl"))
if wheels:
print(f"\n[SUCCESS] Wheel created: {wheels[0]}")
else:
print("\n[ERROR] Wheel not found.")
if __name__ == "__main__":
build()