|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tarfile |
| 9 | +import tomllib |
| 10 | +import zipfile |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +PROJECT_ROOT = Path(__file__).resolve().parent.parent |
| 15 | +BUILD_ROOT = PROJECT_ROOT / "build" |
| 16 | +DIST_ROOT = PROJECT_ROOT / "dist" |
| 17 | +ARTIFACT_ROOT = PROJECT_ROOT / "release_artifacts" |
| 18 | +APP_NAME = "PyRAG-Kit" |
| 19 | +SPEC_PATH = PROJECT_ROOT / f"{APP_NAME}.spec" |
| 20 | +PACKAGE_FILES = [ |
| 21 | + "README.md", |
| 22 | + "LICENSE", |
| 23 | + "DIFY_LICENSE", |
| 24 | + "config.toml.example", |
| 25 | + ".env.example", |
| 26 | +] |
| 27 | +PACKAGE_DIRS = ["knowledge_base"] |
| 28 | +HIDDEN_IMPORTS = [ |
| 29 | + "src.providers.google", |
| 30 | + "src.providers.openai", |
| 31 | + "src.providers.anthropic", |
| 32 | + "src.providers.qwen", |
| 33 | + "src.providers.volcengine", |
| 34 | + "src.providers.siliconflow", |
| 35 | + "src.providers.ollama", |
| 36 | + "src.providers.lm_studio", |
| 37 | + "src.providers.deepseek", |
| 38 | + "src.providers.grok", |
| 39 | + "src.providers.local_hash", |
| 40 | + "src.providers.jina", |
| 41 | + "src.providers.siliconflow_rerank", |
| 42 | +] |
| 43 | + |
| 44 | + |
| 45 | +def load_version() -> str: |
| 46 | + with (PROJECT_ROOT / "pyproject.toml").open("rb") as handle: |
| 47 | + pyproject = tomllib.load(handle) |
| 48 | + return pyproject["project"]["version"] |
| 49 | + |
| 50 | + |
| 51 | +def parse_args() -> argparse.Namespace: |
| 52 | + parser = argparse.ArgumentParser(description="构建跨平台二进制发布包。") |
| 53 | + parser.add_argument( |
| 54 | + "--target", |
| 55 | + required=True, |
| 56 | + choices=[ |
| 57 | + "windows-x64", |
| 58 | + "macos-x64", |
| 59 | + "macos-arm64", |
| 60 | + "linux-x64", |
| 61 | + "linux-arm64", |
| 62 | + ], |
| 63 | + help="目标平台标识。", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "--validate", |
| 67 | + action="store_true", |
| 68 | + help="构建完成后执行最小烟测。", |
| 69 | + ) |
| 70 | + return parser.parse_args() |
| 71 | + |
| 72 | + |
| 73 | +def clean_output_dirs() -> None: |
| 74 | + for directory in (BUILD_ROOT, DIST_ROOT, ARTIFACT_ROOT): |
| 75 | + if directory.exists(): |
| 76 | + shutil.rmtree(directory) |
| 77 | + if SPEC_PATH.exists(): |
| 78 | + SPEC_PATH.unlink() |
| 79 | + ARTIFACT_ROOT.mkdir(parents=True, exist_ok=True) |
| 80 | + |
| 81 | + |
| 82 | +def run_pyinstaller() -> None: |
| 83 | + command = [ |
| 84 | + sys.executable, |
| 85 | + "-m", |
| 86 | + "PyInstaller", |
| 87 | + "--noconfirm", |
| 88 | + "--clean", |
| 89 | + "--onedir", |
| 90 | + "--name", |
| 91 | + APP_NAME, |
| 92 | + "--collect-data", |
| 93 | + "pyfiglet", |
| 94 | + ] |
| 95 | + for hidden_import in HIDDEN_IMPORTS: |
| 96 | + command.extend(["--hidden-import", hidden_import]) |
| 97 | + command.append("main.py") |
| 98 | + subprocess.run(command, cwd=PROJECT_ROOT, check=True) |
| 99 | + |
| 100 | + |
| 101 | +def stage_bundle(target: str, version: str) -> Path: |
| 102 | + bundle_name = f"{APP_NAME}-{version}-{target}" |
| 103 | + bundle_root = ARTIFACT_ROOT / bundle_name |
| 104 | + app_source = DIST_ROOT / APP_NAME |
| 105 | + app_target = bundle_root / APP_NAME |
| 106 | + |
| 107 | + shutil.copytree(app_source, app_target) |
| 108 | + for relative_file in PACKAGE_FILES: |
| 109 | + shutil.copy2(PROJECT_ROOT / relative_file, bundle_root / relative_file) |
| 110 | + for relative_dir in PACKAGE_DIRS: |
| 111 | + shutil.copytree(PROJECT_ROOT / relative_dir, bundle_root / relative_dir) |
| 112 | + |
| 113 | + return bundle_root |
| 114 | + |
| 115 | + |
| 116 | +def archive_bundle(bundle_root: Path, target: str) -> Path: |
| 117 | + if target == "windows-x64": |
| 118 | + archive_path = bundle_root.parent / f"{bundle_root.name}.zip" |
| 119 | + with zipfile.ZipFile(archive_path, "w", compression=zipfile.ZIP_DEFLATED) as archive: |
| 120 | + for file_path in sorted(bundle_root.rglob("*")): |
| 121 | + archive.write(file_path, file_path.relative_to(bundle_root.parent)) |
| 122 | + return archive_path |
| 123 | + |
| 124 | + archive_path = bundle_root.parent / f"{bundle_root.name}.tar.gz" |
| 125 | + with tarfile.open(archive_path, "w:gz") as archive: |
| 126 | + archive.add(bundle_root, arcname=bundle_root.name) |
| 127 | + return archive_path |
| 128 | + |
| 129 | + |
| 130 | +def executable_path(bundle_root: Path) -> Path: |
| 131 | + executable_name = APP_NAME + (".exe" if os.name == "nt" else "") |
| 132 | + return bundle_root / APP_NAME / executable_name |
| 133 | + |
| 134 | + |
| 135 | +def validate_bundle(bundle_root: Path) -> None: |
| 136 | + executable = executable_path(bundle_root) |
| 137 | + subprocess.run( |
| 138 | + [str(executable)], |
| 139 | + cwd=bundle_root, |
| 140 | + input="4\n", |
| 141 | + text=True, |
| 142 | + capture_output=True, |
| 143 | + check=True, |
| 144 | + timeout=30, |
| 145 | + ) |
| 146 | + |
| 147 | + |
| 148 | +def main() -> None: |
| 149 | + args = parse_args() |
| 150 | + version = load_version() |
| 151 | + |
| 152 | + clean_output_dirs() |
| 153 | + run_pyinstaller() |
| 154 | + bundle_root = stage_bundle(args.target, version) |
| 155 | + archive_path = archive_bundle(bundle_root, args.target) |
| 156 | + if SPEC_PATH.exists(): |
| 157 | + SPEC_PATH.unlink() |
| 158 | + |
| 159 | + if args.validate: |
| 160 | + validate_bundle(bundle_root) |
| 161 | + |
| 162 | + print(archive_path) |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + main() |
0 commit comments