import argparse
import logging
import os
import shutil
import subprocess
import sys
import traceback
from pathlib import Path
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class BuildManager:
"""
统一构建管理:依赖拉取 → 编译 → 安装 / 测试。
用法:
python build.py 完整构建(拉取依赖 + Release 编译)
python build.py local 本地构建(跳过依赖拉取, Release 编译)
python build.py test 单元测试(拉取依赖 + Debug 编译 + 执行测试)
python build.py test local 单元测试(跳过依赖拉取, Debug 编译 + 执行测试)
python build.py --version/-v <version> 指定构建版本号(用于 run/exe/dmg 包)
python build.py --extra/-e KEY=VALUE 指定额外构建选项,可多次使用
参数说明:
- 参数: command : 构建动作: 为空时为全构建, local 为跳过依赖下载, test 为运行单元测试。
- 参数: --version : 构建版本号,不传时默认 1.0.0。
- 参数: --extra : 额外构建选项,格式为 KEY=VALUE,可多次指定。
"""
def __init__(self):
self.project_root = Path(__file__).resolve().parent
ap = argparse.ArgumentParser(description='Build the project and optionally run tests.')
ap.add_argument(
'command',
nargs='*',
default=[],
choices=[[], 'local', 'test'],
help='Build action: omit for full build, "local" to skip dependency download, "test" to run unit tests',
)
ap.add_argument(
'-v', '--version', type=str, default='1.0.0', help='Build version for run/exe/dmg packages (default: 1.0.0)'
)
ap.add_argument(
'-e',
'--extra',
metavar='KEY=VALUE',
action='append',
default=[],
help='Extra build options in KEY=VALUE format, can be specified multiple times',
)
self.args = ap.parse_args()
def _execute_command(self, cmd, timeout_seconds=36000, cwd=None, env=None):
logging.info("Running: %s", " ".join(cmd))
subprocess.run(cmd, timeout=timeout_seconds, check=True, cwd=cwd, env=env)
def _archive_artifacts(self):
artifacts_dir = self.project_root / "artifacts"
artifacts_dir.mkdir(exist_ok=True)
sources = [
self.project_root / "output",
self.project_root / "build" / "output_whl_dir",
]
for src_dir in sources:
if not src_dir.is_dir():
continue
for file_path in src_dir.iterdir():
if file_path.is_file():
logging.info("Archiving %s -> %s", file_path, artifacts_dir / file_path.name)
shutil.copy2(file_path, artifacts_dir / file_path.name)
def run(self):
os.chdir(self.project_root)
if 'test' in self.args.command:
self._execute_command(["bash", "test/run_ut.sh"])
else:
if 'local' not in self.args.command:
self._execute_command(["bash", "scripts/download_thirdparty.sh"])
logging.info("--version: %s", self.args.version)
for opt in self.args.extra:
key, _, val = opt.partition('=')
logging.info("--extra: %s = %s", key, val)
self._execute_command(["bash", "scripts/build.sh", self.args.version])
for stale_whl in (self.project_root / "build" / "output_whl_dir").glob("*.whl"):
stale_whl.unlink()
self._execute_command(["bash", "scripts/build_whl.sh", self.args.version])
self._archive_artifacts()
if __name__ == "__main__":
try:
BuildManager().run()
except Exception:
logging.error("Unexpected error: %s", traceback.format_exc())
sys.exit(1)