11
This commit is contained in:
98
src-tauri/src/cover_python.rs
Normal file
98
src-tauri/src/cover_python.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
//! 封面 Python 运行时与脚本目录定位(对齐 Electron bundled python-runtime)
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn exe_name() -> &'static str {
|
||||
if cfg!(windows) {
|
||||
"python.exe"
|
||||
} else {
|
||||
"python3"
|
||||
}
|
||||
}
|
||||
|
||||
/// bundled `python-runtimebackup` 或系统 / 环境变量 Python。
|
||||
pub fn locate_python() -> Option<PathBuf> {
|
||||
if let Ok(p) = std::env::var("AICLIENT_PYTHON_PATH") {
|
||||
let pb = PathBuf::from(&p);
|
||||
if pb.is_file() {
|
||||
return Some(pb);
|
||||
}
|
||||
}
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
if let Some(dir) = exe.parent() {
|
||||
let bundled = dir
|
||||
.join("resources")
|
||||
.join("resources-bundles")
|
||||
.join("python-runtimebackup")
|
||||
.join(exe_name());
|
||||
if bundled.exists() {
|
||||
return Some(bundled);
|
||||
}
|
||||
let cover_only = dir.join("resources").join("cover-python").join(exe_name());
|
||||
if cover_only.exists() {
|
||||
return Some(cover_only);
|
||||
}
|
||||
}
|
||||
}
|
||||
let dev_bundled = PathBuf::from("src-tauri/resources/resources-bundles/python-runtimebackup")
|
||||
.join(exe_name());
|
||||
if dev_bundled.exists() {
|
||||
return Some(dev_bundled);
|
||||
}
|
||||
let dev_cover = PathBuf::from("src-tauri/resources/cover-python").join(exe_name());
|
||||
if dev_cover.exists() {
|
||||
return Some(dev_cover);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// 封面脚本目录(`advanced_cover_generator.py` 等)。
|
||||
pub fn locate_cover_scripts_dir() -> Option<PathBuf> {
|
||||
if let Ok(p) = std::env::var("AICLIENT_COVER_SCRIPTS_DIR") {
|
||||
let pb = PathBuf::from(&p);
|
||||
if pb.join("advanced_cover_generator.py").is_file() {
|
||||
return Some(pb);
|
||||
}
|
||||
}
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
if let Some(dir) = exe.parent() {
|
||||
let bundled = dir.join("resources").join("cover-python");
|
||||
if bundled.join("advanced_cover_generator.py").is_file() {
|
||||
return Some(bundled);
|
||||
}
|
||||
}
|
||||
}
|
||||
let dev = PathBuf::from("src-tauri/resources/cover-python");
|
||||
if dev.join("advanced_cover_generator.py").is_file() {
|
||||
return Some(dev);
|
||||
}
|
||||
let backend = PathBuf::from("../pythonbackend/scripts/cover");
|
||||
if backend.join("advanced_cover_generator.py").is_file() {
|
||||
return backend.canonicalize().ok();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// 将 ffmpeg 所在目录 prepend 到 PATH(供 Python 子进程调用 ffmpeg)。
|
||||
pub fn prepend_ffmpeg_to_path(env_path: &str) -> String {
|
||||
let sep = if cfg!(windows) { ";" } else { ":" };
|
||||
if let Some(ffmpeg) = crate::ffmpeg::locate_ffmpeg() {
|
||||
if let Some(dir) = ffmpeg.parent() {
|
||||
return format!("{}{}{}", dir.display(), sep, env_path);
|
||||
}
|
||||
}
|
||||
env_path.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cover_scripts_dev_path_exists() {
|
||||
let dev = Path::new("src-tauri/resources/cover-python/advanced_cover_generator.py");
|
||||
if dev.is_file() {
|
||||
assert!(locate_cover_scripts_dir().is_some());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user