refactor: use external moviepilot rust package

This commit is contained in:
jxxghp
2026-05-25 20:51:59 +08:00
parent 05943287c0
commit c33c62b938
20 changed files with 28 additions and 7556 deletions

View File

@@ -45,9 +45,6 @@ COOKIE_DIR = CONFIG_DIR / "cookies"
ENV_FILE = CONFIG_DIR / "app.env"
DEFAULT_NODE_VERSION = "20.12.1"
RUST_ACCEL_DIR = ROOT / "rust" / "moviepilot_rust"
RUST_ACCEL_MANIFEST = RUST_ACCEL_DIR / "Cargo.toml"
RUST_ACCEL_SKIP_ENV = "MOVIEPILOT_SKIP_RUST_ACCEL"
FRONTEND_LATEST_API = (
"https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest"
)
@@ -637,98 +634,6 @@ def configure_venv_pip_compat(venv_dir: Path, venv_python: Path) -> Path:
return get_venv_pip(venv_dir)
def _rust_accel_should_skip() -> bool:
"""
判断当前安装是否显式跳过 Rust 加速扩展构建。
"""
raw_value = os.getenv(RUST_ACCEL_SKIP_ENV, "").strip().lower()
return raw_value in {"1", "true", "yes", "on"}
def _cargo_env_path() -> str:
"""
组合 PATH兼容 rustup 默认安装到用户目录但当前 shell 未刷新环境的场景。
"""
extra_paths = [str(Path.home() / ".cargo" / "bin")]
current_path = os.environ.get("PATH", "")
return os.pathsep.join([*extra_paths, current_path])
def _find_cargo() -> Optional[str]:
"""
查找 Rust cargo 可执行文件,供本地 CLI 安装构建 PyO3 扩展。
"""
return shutil.which("cargo", path=_cargo_env_path())
def _find_native_linker() -> Optional[str]:
"""
查找 Rust 扩展构建所需的本机链接器。
"""
if os.name == "nt":
return "windows-msvc"
for candidate in ("cc", "gcc", "clang"):
linker = shutil.which(candidate)
if linker:
return linker
return None
def ensure_rust_accel_ready() -> bool:
"""
确认 Rust 加速扩展源码存在且本机具备 cargo 与链接器。
"""
if not RUST_ACCEL_MANIFEST.exists():
return False
if _rust_accel_should_skip():
print_step(f"已跳过 Rust 加速扩展构建:{RUST_ACCEL_SKIP_ENV}=1")
return False
if not _find_cargo():
print_step(
"未找到 Rust cargo已跳过 Rust 加速扩展构建;"
"应用将继续使用 Python 实现。"
)
return False
if not _find_native_linker():
print_step(
"未找到本机 C 编译器/链接器,已跳过 Rust 加速扩展构建;"
"应用将继续使用 Python 实现。"
)
return False
return True
def install_rust_accel(venv_python: Path) -> None:
"""
构建并安装 MoviePilot Rust 加速扩展到当前虚拟环境。
"""
if not RUST_ACCEL_MANIFEST.exists():
return
if _rust_accel_should_skip():
return
if not ensure_rust_accel_ready():
return
print_step("构建并安装 Rust 加速扩展")
env = os.environ.copy()
env["PATH"] = _cargo_env_path()
try:
run(
[
str(venv_python),
"-m",
"maturin",
"develop",
"--release",
"--manifest-path",
str(RUST_ACCEL_MANIFEST),
],
env=env,
)
except subprocess.CalledProcessError as exc:
print_step(f"Rust 加速扩展构建失败,已跳过;应用将继续使用 Python 实现:{exc}")
def ensure_supported_python(python_bin: str) -> None:
version = get_python_version(python_bin)
if version < MIN_PYTHON_VERSION:
@@ -2752,7 +2657,7 @@ def init_local(
def install_deps(*, python_bin: str, venv_dir: Path, recreate: bool) -> Path:
"""
创建或复用本地虚拟环境,并安装后端依赖、Rust 扩展和浏览器运行时。
创建或复用本地虚拟环境,并安装后端依赖和浏览器运行时。
"""
ensure_supported_python(python_bin)
venv_dir = venv_dir.expanduser().resolve()
@@ -2779,7 +2684,6 @@ def install_deps(*, python_bin: str, venv_dir: Path, recreate: bool) -> Path:
print_step("安装项目依赖")
run([str(venv_pip), "install", "-r", str(ROOT / "requirements.txt")])
install_rust_accel(venv_python)
install_browser_runtime(venv_python)
return venv_python