Skip to content

Commit b4271d1

Browse files
committed
refactored initialization.rs, consolidated to BridgeError.
1 parent e39d6cb commit b4271d1

3 files changed

Lines changed: 35 additions & 47 deletions

File tree

crates/r2x-python/src/initialization.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,7 @@ impl Bridge {
6969

7070
// Add site-packages from venv to sys.path so imports work as expected
7171
let venv_path = PathBuf::from(config.get_venv_path());
72-
73-
// TODO we can move this block and the next into utils.rs
74-
let lib_dir = venv_path.join(PYTHON_LIB_DIR);
75-
logger::debug(&format!(
76-
"lib_dir: {}, exists: {}",
77-
lib_dir.display(),
78-
lib_dir.exists()
79-
));
80-
if !lib_dir.exists() {
81-
return Err(BridgeError::VenvNotFound(venv_path.to_path_buf()));
82-
}
83-
84-
// Find the python3.X directory inside lib/
85-
use std::fs;
86-
let python_version_dir = fs::read_dir(&lib_dir)
87-
.map_err(|e| {
88-
BridgeError::Initialization(format!("Failed to read lib directory: {}", e))
89-
})?
90-
.filter_map(|e| e.ok())
91-
.find(|e| e.file_name().to_string_lossy().starts_with("python"))
92-
.ok_or_else(|| {
93-
BridgeError::Initialization("No python3.X directory found in venv/lib".to_string())
94-
})?;
95-
96-
let site_packages = resolve_site_package_path(&venv_path).map_err(|e| {
97-
BridgeError::Initialization(format!("Failed to resolve site package path"))
98-
})?;
72+
let site_packages = resolve_site_package_path(&venv_path)?;
9973

10074
logger::debug(&format!(
10175
"site_packages: {}, exists: {}",

crates/r2x-python/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod utils;
1414

1515
pub use errors::BridgeError;
1616
pub use initialization::{configure_python_venv, Bridge};
17-
pub use utils::{resolve_site_package_path, PYTHON_BIN_DIR, PYTHON_EXE, PYTHON_LIB_DIR};
17+
pub use utils::{resolve_python_path, resolve_site_package_path};
1818

1919
#[cfg(test)]
2020
mod tests {

crates/r2x-python/src/utils.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ use std::path::PathBuf;
99

1010
/// The name of the library directory in a Python venv (e.g., "Lib" on Windows, "lib" on Unix)
1111
#[cfg(windows)]
12-
pub const PYTHON_LIB_DIR: &str = "Lib";
12+
const PYTHON_LIB_DIR: &str = "Lib";
1313
#[cfg(not(windows))]
14-
pub const PYTHON_LIB_DIR: &str = "lib";
14+
const PYTHON_LIB_DIR: &str = "lib";
1515

1616
/// The name of the binaries/scripts directory in a Python venv (e.g., "Scripts" on Windows, "bin" on Unix)
1717
#[cfg(windows)]
18-
pub const PYTHON_BIN_DIR: &str = "Scripts";
18+
const PYTHON_BIN_DIR: &str = "Scripts";
1919
#[cfg(not(windows))]
20-
pub const PYTHON_BIN_DIR: &str = "bin";
20+
const PYTHON_BIN_DIR: &str = "bin";
2121

2222
/// The name of the Python executable in a venv (e.g., "python.exe" on Windows, "python" on Unix)
2323
#[cfg(windows)]
24-
pub const PYTHON_EXE: &str = "python.exe";
24+
const PYTHON_EXE: &str = "python.exe";
2525
#[cfg(not(windows))]
26-
pub const PYTHON_EXE: &str = "python";
26+
const PYTHON_EXE: &str = "python";
2727

2828
// Site Packages differences.
2929
//
@@ -33,10 +33,10 @@ pub const PYTHON_EXE: &str = "python";
3333
// Windows
3434
// .venv/Lib/site-packages
3535

36-
pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr> {
36+
pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, BridgeError> {
3737
// Verify the venv_path exists and is a directory.
3838
if !venv_path.is_dir() {
39-
return Err(VenvErr::VenvDirNotFound);
39+
return Err(BridgeError::VenvNotFound(venv_path.to_path_buf()));
4040
}
4141

4242
#[cfg(windows)]
@@ -45,7 +45,10 @@ pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr
4545

4646
// verify site_package_path exists
4747
if !site_packages.is_dir() {
48-
return Err(VenvErr::PackageDirNotFound);
48+
return Err(BridgeError::Initialization(format!(
49+
"unable to locate package directory: {}",
50+
site_packages.display()
51+
)));
4952
}
5053
Ok(site_packages)
5154
}
@@ -55,37 +58,48 @@ pub fn resolve_site_package_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr
5558
let lib_dir = venv_path.join(PYTHON_LIB_DIR);
5659

5760
if !lib_dir.is_dir() {
58-
return Err(VenvErr::LibDirNotFound);
61+
return Err(BridgeError::Initialization(format!(
62+
"unable to locate lib directory: {}",
63+
lib_dir.display()
64+
)));
5965
}
6066

61-
// FIXME Return a VenvError -> Map to BridgeError in Initialization process
62-
let python_version_dir = std::fs::read_dir(&lib_dir)
63-
.map_err(|e| format!("Failed to read lib directory: {}", e))?
67+
let python_version_dir = fs::read_dir(&lib_dir)
68+
.map_err(|e| {
69+
BridgeError::Initialization(format!("Failed to read lib directory: {}", e))
70+
})?
6471
.filter_map(|e| e.ok())
6572
.find(|e| e.file_name().to_string_lossy().starts_with("python"))
66-
.ok_or_else(|| "No python directory found in venv".to_string())?;
73+
.ok_or_else(|| {
74+
BridgeError::Initialization("No python3.X directory found in venv/lib".to_string())
75+
})?;
6776

6877
let site_packages = python_version_dir.path().join("site-packages");
6978

7079
if !site_packages.is_dir() {
71-
return Err(VenvErr::LibDirNotFound);
80+
return Err(BridgeError::Initialization(format!(
81+
"unable to locate package directory: {}",
82+
site_packages.display()
83+
)));
7284
}
7385

7486
Ok(site_packages)
7587
}
7688
}
7789

78-
pub fn resolve_python_path(venv_path: &PathBuf) -> Result<PathBuf, VenvErr> {
90+
pub fn resolve_python_path(venv_path: &PathBuf) -> Result<PathBuf, BridgeError> {
7991
// validate venv path is a valid directory
8092
if !venv_path.is_dir() {
81-
return Err(VenvErr::VenvDirNotFound);
93+
return Err(BridgeError::VenvNotFound(venv_path.to_path_buf()));
8294
}
8395

8496
let python_path = venv_path.join(PYTHON_BIN_DIR).join(PYTHON_EXE);
85-
8697
// validate the interpreter path is valid
8798
if !python_path.is_file() {
88-
return Err(VenvErr::BinaryNotFound);
99+
return Err(BridgeError::Initialization(format!(
100+
"Path to python binary is not valid: {}",
101+
python_path.display()
102+
)));
89103
}
90104

91105
return Ok(python_path);

0 commit comments

Comments
 (0)