lssa/keycard_wallet/src/python_path.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2026-04-23 17:45:43 -04:00
use std::{env, path::PathBuf};
use pyo3::{prelude::*, types::PyList};
2026-04-26 22:50:16 -04:00
/// Adds the project's `python/` directory and venv site-packages to Python's sys.path.
pub fn add_python_path(py: Python) -> PyResult<()> {
2026-04-23 17:45:43 -04:00
let current_dir = env::current_dir().expect("Failed to get current working directory");
let paths_to_add: Vec<PathBuf> = vec![
current_dir.join("python"),
current_dir.join("python").join("keycard-py"),
];
// Sanity check — warns early if a path doesn't exist
for path in &paths_to_add {
if !path.exists() {
2026-04-26 22:50:16 -04:00
eprintln!("Warning: Python path does not exist: {path:?}");
}
}
let sys = py.import_bound("sys")?;
let sys_path: &PyList = sys.getattr("path")?.extract()?;
for path in &paths_to_add {
2026-04-23 17:45:43 -04:00
let path_str = path.to_str().expect("Invalid path");
// Avoid duplicating the path
2026-04-23 17:45:43 -04:00
if !sys_path
.iter()
.any(|p| p.extract::<&str>().unwrap_or("") == path_str)
{
sys_path.insert(0, path_str)?;
}
}
Ok(())
2026-04-23 17:45:43 -04:00
}