Spaces:
Paused
Paused
Download console/src-tauri/src/backend/command.rs from mysticaldragon/HF: direct link, hf CLI and curl.
- Browser
- Download file 7.75 kB
-
https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src-tauri/src/backend/command.rs
- Command line
-
hf download hf://spaces/mysticaldragon/HF/console/src-tauri/src/backend/command.rs
-
curl -L -o command.rs https://huggingface.co/spaces/mysticaldragon/HF/resolve/main/console/src-tauri/src/backend/command.rs
7.75 kB
| //! Backend command construction for development and packaged builds. | |
| use std::path::{Path, PathBuf}; | |
| use std::process::{Command as StdCommand, Stdio}; | |
| use tauri::Manager; | |
| use tauri_plugin_shell::{process::Command, ShellExt}; | |
| /// Builds the command used to start the Python backend sidecar. | |
| pub(super) fn create(app: &tauri::AppHandle) -> Result<Command, String> { | |
| let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../.."); | |
| let source_path = repo_root.join("src"); | |
| let command = if command_exists("uv") { | |
| log::info!( | |
| "[backend] dev command: uv run python -m qwenpaw.tauri.entry cwd={}", | |
| repo_root.display(), | |
| ); | |
| app.shell() | |
| .command("uv") | |
| .args(["run", "python", "-m", "qwenpaw.tauri.entry"]) | |
| .current_dir(repo_root) | |
| .env("PYTHONPATH", source_path.display().to_string()) | |
| } else { | |
| let (python, prefix_args) = python_command(&repo_root); | |
| let mut args = prefix_args; | |
| args.extend(["-m", "qwenpaw.tauri.entry"]); | |
| log::info!( | |
| "[backend] dev command: {} {} cwd={}", | |
| python, | |
| args.join(" "), | |
| repo_root.display(), | |
| ); | |
| app.shell() | |
| .command(python) | |
| .args(args) | |
| .current_dir(repo_root) | |
| .env("PYTHONPATH", source_path.display().to_string()) | |
| }; | |
| Ok(apply_contributed_environment(app, command)) | |
| } | |
| /// Builds the command used to start the packaged Python backend sidecar. | |
| pub(super) fn create(app: &tauri::AppHandle) -> Result<Command, String> { | |
| let backend = packaged_backend_executable(app)?; | |
| let resource_dir = app | |
| .path() | |
| .resource_dir() | |
| .map_err(|err| format!("failed to resolve resource directory: {err}"))?; | |
| let backend_dir = backend | |
| .parent() | |
| .ok_or_else(|| format!("backend executable has no parent: {}", backend.display()))? | |
| .to_path_buf(); | |
| log::info!( | |
| "[backend] packaged command: {} cwd={}", | |
| backend.display(), | |
| backend_dir.display(), | |
| ); | |
| let command = app | |
| .shell() | |
| .command(backend) | |
| .current_dir(&backend_dir) | |
| .env(path_env_key(), path_with_backend_dir(&backend_dir)?) | |
| .env( | |
| "QWENPAW_TAURI_RESOURCE_DIR", | |
| resource_dir.to_string_lossy().to_string(), | |
| ); | |
| let mut command = apply_contributed_environment(app, command); | |
| // A complete Playwright Chromium payload exceeds the practical NSIS | |
| // installer mapping limit on Windows. The sidecar downloads the exact | |
| // driver-matched revision into the user's QwenPaw data directory instead. | |
| if cfg!(windows) { | |
| command = command.env("QWENPAW_DESKTOP_MANAGED_PLAYWRIGHT", "1"); | |
| } | |
| // Bundled standalone Python used by the backend to install third-party | |
| // plugin dependencies (sys.executable is the frozen backend, not Python). | |
| if let Some(python) = packaged_python_runtime(app) { | |
| log::info!("[backend] bundled python runtime: {}", python.display()); | |
| command = command.env( | |
| "QWENPAW_DESKTOP_PY_RUNTIME", | |
| python.to_string_lossy().to_string(), | |
| ); | |
| } else { | |
| log::warn!( | |
| "[backend] bundled python runtime not found; plugin dependency \ | |
| installation will be unavailable" | |
| ); | |
| } | |
| if let Some(node_runtime) = packaged_node_runtime(app) { | |
| log::info!("[backend] bundled node runtime: {}", node_runtime.display()); | |
| command = command.env( | |
| "QWENPAW_DESKTOP_NODE_RUNTIME", | |
| node_runtime.to_string_lossy().to_string(), | |
| ); | |
| } else { | |
| log::warn!("[backend] bundled node runtime not found"); | |
| } | |
| Ok(command) | |
| } | |
| fn packaged_python_runtime(app: &tauri::AppHandle) -> Option<PathBuf> { | |
| let base = app | |
| .path() | |
| .resource_dir() | |
| .ok()? | |
| .join("binaries") | |
| .join("python-runtime") | |
| .join("python"); | |
| let candidates = if cfg!(windows) { | |
| vec![base.join("python.exe")] | |
| } else { | |
| vec![ | |
| base.join("bin").join("python3"), | |
| base.join("bin").join("python"), | |
| ] | |
| }; | |
| candidates.into_iter().find(|path| path.is_file()) | |
| } | |
| /// Add the variables desktop features contribute to the backend's environment. | |
| /// | |
| /// The set comes from [`crate::runtime_env`], so this stays independent of which | |
| /// feature needs what. | |
| fn apply_contributed_environment(app: &tauri::AppHandle, mut command: Command) -> Command { | |
| for (key, value) in crate::runtime_env::collect(app) { | |
| command = command.env(key, value); | |
| } | |
| command | |
| } | |
| fn packaged_node_runtime(app: &tauri::AppHandle) -> Option<PathBuf> { | |
| let root = app | |
| .path() | |
| .resource_dir() | |
| .ok()? | |
| .join("binaries") | |
| .join("node-runtime"); | |
| let node = if cfg!(windows) { | |
| root.join("node.exe") | |
| } else { | |
| root.join("bin").join("node") | |
| }; | |
| node.is_file().then_some(root) | |
| } | |
| fn packaged_backend_executable(app: &tauri::AppHandle) -> Result<PathBuf, String> { | |
| let executable_name = if cfg!(windows) { | |
| "qwenpaw-backend.exe" | |
| } else { | |
| "qwenpaw-backend" | |
| }; | |
| let path = app | |
| .path() | |
| .resource_dir() | |
| .map_err(|err| format!("failed to resolve resource directory: {err}"))? | |
| .join("binaries") | |
| .join("qwenpaw-backend") | |
| .join(executable_name); | |
| if path.is_file() { | |
| Ok(path) | |
| } else { | |
| Err(format!( | |
| "backend executable not found at {}", | |
| path.display() | |
| )) | |
| } | |
| } | |
| fn path_with_backend_dir(backend_dir: &Path) -> Result<String, String> { | |
| let mut paths = vec![backend_dir.to_path_buf()]; | |
| if let Some(existing) = std::env::var_os(path_env_key()) { | |
| paths.extend(std::env::split_paths(&existing)); | |
| } | |
| std::env::join_paths(paths) | |
| .map_err(|err| format!("failed to join backend PATH entries: {err}"))? | |
| .into_string() | |
| .map_err(|_| "backend PATH contains non-Unicode data".to_string()) | |
| } | |
| fn path_env_key() -> &'static str { | |
| "Path" | |
| } | |
| fn path_env_key() -> &'static str { | |
| "PATH" | |
| } | |
| fn command_exists(command: &str) -> bool { | |
| StdCommand::new(command) | |
| .arg("--version") | |
| .stdout(Stdio::null()) | |
| .stderr(Stdio::null()) | |
| .status() | |
| .is_ok_and(|status| status.success()) | |
| } | |
| fn local_python(repo_root: &Path) -> Option<String> { | |
| let candidates = if cfg!(windows) { | |
| vec![ | |
| repo_root.join(".venv/Scripts/python.exe"), | |
| repo_root.join("venv/Scripts/python.exe"), | |
| ] | |
| } else { | |
| vec![ | |
| repo_root.join(".venv/bin/python"), | |
| repo_root.join("venv/bin/python"), | |
| ] | |
| }; | |
| candidates | |
| .into_iter() | |
| .find(|path| path.is_file()) | |
| .map(|path| path.display().to_string()) | |
| } | |
| fn python_command(repo_root: &Path) -> (String, Vec<&'static str>) { | |
| if let Some(local) = local_python(repo_root) { | |
| return (local, vec![]); | |
| } | |
| { | |
| if command_exists("py") { | |
| return ("py".to_string(), vec!["-3"]); | |
| } | |
| } | |
| if command_exists("python3") { | |
| ("python3".to_string(), vec![]) | |
| } else { | |
| ("python".to_string(), vec![]) | |
| } | |
| } | |