Skip to content

Commit e39d6cb

Browse files
committed
work in progress
1 parent f7d31fb commit e39d6cb

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

crates/r2x-python/src/initialization.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module handles all Python interpreter initialization, virtual environment
44
//! configuration, and environment setup required before the bridge can be used.
55
6-
use super::utils::*;
6+
use super::utils::{resolve_python_path, resolve_site_package_path};
77
use crate::errors::BridgeError;
88
use once_cell::sync::OnceCell;
99
use pyo3::prelude::*;
@@ -70,6 +70,7 @@ impl Bridge {
7070
// Add site-packages from venv to sys.path so imports work as expected
7171
let venv_path = PathBuf::from(config.get_venv_path());
7272

73+
// TODO we can move this block and the next into utils.rs
7374
let lib_dir = venv_path.join(PYTHON_LIB_DIR);
7475
logger::debug(&format!(
7576
"lib_dir: {}, exists: {}",
@@ -320,7 +321,13 @@ pub fn configure_python_venv() -> Result<PathBuf, BridgeError> {
320321

321322
let venv_path = PathBuf::from(config.get_venv_path());
322323

323-
let python_path = venv_path.join(PYTHON_BIN_DIR).join(PYTHON_EXE);
324+
let python_path_result = resolve_python_path(&venv_path);
325+
326+
if python_path_result.is_err() {
327+
logger::debug("Could not resolve Python path");
328+
}
329+
330+
let python_path = python_path_result.unwrap_or_else(|_| PathBuf::new());
324331

325332
// Create venv if it doesn't exist
326333
if !venv_path.exists() || !python_path.exists() {

crates/r2x-python/src/utils.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33
//! This module provides compile-time constants for directories and files that differ
44
//! between Windows and Unix-like systems in Python virtual environments.
55
6+
use super::errors::BridgeError;
7+
use std::fs;
68
use std::path::PathBuf;
79

8-
#[derive(Debug)]
9-
pub enum VenvErr {
10-
BinaryNotFound,
11-
PackageNotFound,
12-
PackageDirNotFound,
13-
LibDirNotFound,
14-
VenvDirNotFound,
15-
IOError(std::io::Error),
16-
}
17-
1810
/// The name of the library directory in a Python venv (e.g., "Lib" on Windows, "lib" on Unix)
1911
#[cfg(windows)]
2012
pub const PYTHON_LIB_DIR: &str = "Lib";
@@ -66,6 +58,7 @@ pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr
6658
return Err(VenvErr::LibDirNotFound);
6759
}
6860

61+
// FIXME Return a VenvError -> Map to BridgeError in Initialization process
6962
let python_version_dir = std::fs::read_dir(&lib_dir)
7063
.map_err(|e| format!("Failed to read lib directory: {}", e))?
7164
.filter_map(|e| e.ok())
@@ -81,3 +74,19 @@ pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr
8174
Ok(site_packages)
8275
}
8376
}
77+
78+
pub fn resolve_python_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr> {
79+
// validate venv path is a valid directory
80+
if !venv_path.is_dir() {
81+
return Err(VenvErr::VenvDirNotFound);
82+
}
83+
84+
let python_path = venv_path.join(PYTHON_BIN_DIR).join(PYTHON_EXE);
85+
86+
// validate the interpreter path is valid
87+
if !python_path.is_file() {
88+
return Err(VenvErr::BinaryNotFound);
89+
}
90+
91+
return Ok(python_path);
92+
}

0 commit comments

Comments
 (0)