Skip to main content

r2x_python/
utils.rs

1//! Utility constants and functions for platform-specific Python venv path handling
2//!
3//! This module provides compile-time constants for directories and files that differ
4//! between Windows and Unix-like systems in Python virtual environments.
5
6use crate::errors::BridgeError;
7use r2x_logger as logger;
8use std::fs;
9use std::path::{Path, PathBuf};
10
11/// The name of the library directory in a Python venv (e.g., "Lib" on Windows, "lib" on Unix)
12#[cfg(windows)]
13pub const PYTHON_LIB_DIR: &str = "Lib";
14#[cfg(unix)]
15pub const PYTHON_LIB_DIR: &str = "lib";
16
17/// The name of the binaries/scripts directory in a Python venv (e.g., "Scripts" on Windows, "bin" on Unix)
18#[cfg(windows)]
19const PYTHON_BIN_DIR: &str = "Scripts";
20#[cfg(unix)]
21const PYTHON_BIN_DIR: &str = "bin";
22
23/// Candidate executable names in a venv
24#[cfg(unix)]
25const PYTHON_EXE_CANDIDATES: &[&str] = &["python3", "python"];
26#[cfg(windows)]
27const PYTHON_EXE_CANDIDATES: &[&str] = &["python.exe", "python3.exe", "python3.12.exe"];
28
29// Site Packages differences.
30//
31// MacOS
32// .venv/lib/python {version}/site-packages
33//
34// Windows
35// .venv/Lib/site-packages
36
37pub fn resolve_site_package_path(venv_path: &Path) -> Result<PathBuf, BridgeError> {
38    logger::debug(&format!(
39        "Resolving site-packages path for venv: {}",
40        venv_path.display()
41    ));
42
43    // Verify the venv_path exists and is a directory.
44    if !venv_path.is_dir() {
45        logger::debug(&format!(
46            "Venv path does not exist or is not a directory: {}",
47            venv_path.display()
48        ));
49        return Err(BridgeError::VenvNotFound(venv_path.to_path_buf()));
50    }
51
52    #[cfg(windows)]
53    {
54        let site_packages = venv_path.join(PYTHON_LIB_DIR).join("site-packages");
55        logger::debug(&format!(
56            "Windows: Looking for site-packages at: {}",
57            site_packages.display()
58        ));
59
60        // verify site_package_path exists
61        if !site_packages.is_dir() {
62            logger::debug(&format!(
63                "Windows: site-packages directory not found at: {}",
64                site_packages.display()
65            ));
66            return Err(BridgeError::Initialization(format!(
67                "unable to locate package directory: {}",
68                site_packages.display()
69            )));
70        }
71        logger::debug(&format!(
72            "Windows: Successfully resolved site-packages: {}",
73            site_packages.display()
74        ));
75        Ok(site_packages)
76    }
77
78    #[cfg(not(windows))]
79    {
80        let lib_dir = venv_path.join(PYTHON_LIB_DIR);
81        logger::debug(&format!(
82            "Unix: Looking for lib directory at: {}",
83            lib_dir.display()
84        ));
85
86        if !lib_dir.is_dir() {
87            logger::debug(&format!(
88                "Unix: lib directory not found at: {}",
89                lib_dir.display()
90            ));
91            return Err(BridgeError::Initialization(format!(
92                "unable to locate lib directory: {}",
93                lib_dir.display()
94            )));
95        }
96
97        let python_version_dir = fs::read_dir(&lib_dir)
98            .map_err(|e| {
99                logger::debug(&format!("Unix: Failed to read lib directory: {}", e));
100                BridgeError::Initialization(format!("Failed to read lib directory: {}", e))
101            })?
102            .filter_map(|e| e.ok())
103            .find(|e| e.file_name().to_string_lossy().starts_with("python"))
104            .ok_or_else(|| {
105                logger::debug("Unix: No python3.X directory found in venv/lib");
106                BridgeError::Initialization("No python3.X directory found in venv/lib".to_string())
107            })?;
108
109        logger::debug(&format!(
110            "Unix: Found python version directory: {}",
111            python_version_dir.path().display()
112        ));
113
114        let site_packages = python_version_dir.path().join("site-packages");
115        logger::debug(&format!(
116            "Unix: Looking for site-packages at: {}",
117            site_packages.display()
118        ));
119
120        if !site_packages.is_dir() {
121            logger::debug(&format!(
122                "Unix: site-packages directory not found at: {}",
123                site_packages.display()
124            ));
125            return Err(BridgeError::Initialization(format!(
126                "unable to locate package directory: {}",
127                site_packages.display()
128            )));
129        }
130
131        logger::debug(&format!(
132            "Unix: Successfully resolved site-packages: {}",
133            site_packages.display()
134        ));
135        Ok(site_packages)
136    }
137}
138
139pub fn resolve_python_path(venv_path: &Path) -> Result<PathBuf, BridgeError> {
140    // validate venv path is a valid directory
141    if !venv_path.is_dir() {
142        return Err(BridgeError::VenvNotFound(venv_path.to_path_buf()));
143    }
144
145    let bin_dir = venv_path.join(PYTHON_BIN_DIR);
146    if !bin_dir.is_dir() {
147        return Err(BridgeError::Initialization(format!(
148            "Python bin directory missing: {}",
149            bin_dir.display()
150        )));
151    }
152
153    for exe in PYTHON_EXE_CANDIDATES {
154        let candidate = bin_dir.join(exe);
155        if candidate.is_file() {
156            return Ok(candidate);
157        }
158    }
159
160    if let Ok(entries) = fs::read_dir(&bin_dir) {
161        if let Some(candidate) = entries.filter_map(|e| e.ok()).map(|e| e.path()).find(|p| {
162            p.file_name()
163                .and_then(|n| n.to_str())
164                .is_some_and(|name| name.contains("python"))
165                && p.is_file()
166        }) {
167            return Ok(candidate);
168        }
169    }
170
171    Err(BridgeError::Initialization(format!(
172        "Path to python binary is not valid in {}",
173        venv_path.display()
174    )))
175}
176
177#[cfg(test)]
178mod tests {
179    use crate::utils::*;
180    use std::fs;
181    use tempfile::TempDir;
182
183    /// Helper to create a mock venv structure for testing
184    #[allow(dead_code)]
185    fn create_mock_venv_unix(python_version: &str) -> Option<TempDir> {
186        let temp_dir = TempDir::new().ok()?;
187        let venv_path = temp_dir.path();
188
189        // Create Unix structure: .venv/lib/python3.X/site-packages
190        let lib_dir = venv_path.join("lib");
191        let python_dir = lib_dir.join(python_version);
192        let site_packages = python_dir.join("site-packages");
193        fs::create_dir_all(&site_packages).ok()?;
194
195        // Create bin directory with python executable
196        let bin_dir = venv_path.join("bin");
197        fs::create_dir_all(&bin_dir).ok()?;
198        fs::write(bin_dir.join("python3"), "").ok()?;
199
200        Some(temp_dir)
201    }
202
203    /// Helper to create a mock Windows venv structure for testing
204    #[allow(dead_code)]
205    fn create_mock_venv_windows() -> Option<TempDir> {
206        let temp_dir = TempDir::new().ok()?;
207        let venv_path = temp_dir.path();
208
209        // Create Windows structure: .venv/Lib/site-packages
210        let lib_dir = venv_path.join("Lib");
211        let site_packages = lib_dir.join("site-packages");
212        fs::create_dir_all(&site_packages).ok()?;
213
214        // Create Scripts directory with python executable
215        let scripts_dir = venv_path.join("Scripts");
216        fs::create_dir_all(&scripts_dir).ok()?;
217        fs::write(scripts_dir.join("python.exe"), "").ok()?;
218
219        Some(temp_dir)
220    }
221
222    #[test]
223    #[cfg(unix)]
224    fn test_resolve_site_package_path_unix() {
225        let Some(temp_venv) = create_mock_venv_unix("python3.12") else {
226            return;
227        };
228        let venv_path = temp_venv.path().to_path_buf();
229
230        let result = resolve_site_package_path(&venv_path);
231        assert!(result.is_ok());
232        assert!(result.is_ok_and(|sp| sp.ends_with("lib/python3.12/site-packages") && sp.exists()));
233    }
234
235    #[test]
236    #[cfg(unix)]
237    fn test_resolve_site_package_path_unix_different_version() {
238        let Some(temp_venv) = create_mock_venv_unix("python3.11") else {
239            return;
240        };
241        let venv_path = temp_venv.path().to_path_buf();
242
243        let result = resolve_site_package_path(&venv_path);
244        assert!(result.is_ok());
245        assert!(result.is_ok_and(|sp| sp.ends_with("lib/python3.11/site-packages")));
246    }
247
248    #[test]
249    #[cfg(windows)]
250    fn test_resolve_site_package_path_windows() {
251        let Some(temp_venv) = create_mock_venv_windows() else {
252            return;
253        };
254        let venv_path = temp_venv.path().to_path_buf();
255
256        let result = resolve_site_package_path(&venv_path);
257        assert!(result.is_ok());
258        assert!(result.is_ok_and(|sp| sp.ends_with("Lib\\site-packages") && sp.exists()));
259    }
260
261    #[test]
262    fn test_resolve_site_package_path_venv_not_found() {
263        let non_existent_path = PathBuf::from("/tmp/non_existent_venv_12345");
264
265        let result = resolve_site_package_path(&non_existent_path);
266        assert!(result.is_err());
267
268        assert!(matches!(
269            result,
270            Err(BridgeError::VenvNotFound(path)) if path == non_existent_path
271        ));
272    }
273
274    #[test]
275    #[cfg(unix)]
276    fn test_resolve_site_package_path_missing_python_dir() {
277        let Ok(temp_dir) = TempDir::new() else {
278            return;
279        };
280        let venv_path = temp_dir.path();
281
282        // Create lib dir but no python3.X subdirectory
283        let lib_dir = venv_path.join("lib");
284        if fs::create_dir_all(&lib_dir).is_err() {
285            return;
286        }
287
288        let result = resolve_site_package_path(venv_path);
289        assert!(result.is_err());
290        assert!(result.is_err_and(|e| matches!(e, BridgeError::Initialization(msg) if msg.contains("No python3.X directory found"))));
291    }
292
293    #[test]
294    #[cfg(unix)]
295    fn test_resolve_python_path_unix() {
296        let Some(temp_venv) = create_mock_venv_unix("python3.12") else {
297            return;
298        };
299        let venv_path = temp_venv.path().to_path_buf();
300
301        let result = resolve_python_path(&venv_path);
302        assert!(result.is_ok());
303        assert!(result.is_ok_and(|pp| pp.ends_with("bin/python3")));
304    }
305
306    #[test]
307    #[cfg(windows)]
308    fn test_resolve_python_path_windows() {
309        let Some(temp_venv) = create_mock_venv_windows() else {
310            return;
311        };
312        let venv_path = temp_venv.path().to_path_buf();
313
314        let result = resolve_python_path(&venv_path);
315        assert!(result.is_ok());
316        assert!(result.is_ok_and(|pp| pp.ends_with("Scripts\\python.exe")));
317    }
318
319    #[test]
320    fn test_python_lib_dir_constant() {
321        // Test that the compile-time constant is correct for the platform
322        #[cfg(unix)]
323        assert_eq!(PYTHON_LIB_DIR, "lib");
324
325        #[cfg(windows)]
326        assert_eq!(PYTHON_LIB_DIR, "Lib");
327    }
328
329    #[test]
330    fn test_python_bin_dir_constant() {
331        // Test that the compile-time constant is correct for the platform
332        #[cfg(unix)]
333        assert_eq!(PYTHON_BIN_DIR, "bin");
334
335        #[cfg(windows)]
336        assert_eq!(PYTHON_BIN_DIR, "Scripts");
337    }
338
339    #[test]
340    #[cfg(unix)]
341    fn test_resolve_site_package_path_with_multiple_python_versions() {
342        let Ok(temp_dir) = TempDir::new() else {
343            return;
344        };
345        let venv_path = temp_dir.path();
346
347        // Create lib dir with multiple python versions
348        let lib_dir = venv_path.join("lib");
349        if fs::create_dir_all(&lib_dir).is_err() {
350            return;
351        }
352
353        // Create python3.11
354        let python_311 = lib_dir.join("python3.11");
355        let site_packages_311 = python_311.join("site-packages");
356        if fs::create_dir_all(&site_packages_311).is_err() {
357            return;
358        }
359
360        // Create python3.12 (should find the first one)
361        let python_312 = lib_dir.join("python3.12");
362        let site_packages_312 = python_312.join("site-packages");
363        if fs::create_dir_all(&site_packages_312).is_err() {
364            return;
365        }
366
367        let result = resolve_site_package_path(venv_path);
368        assert!(result.is_ok());
369        // Should find one of them (implementation finds first match)
370        assert!(result.is_ok_and(
371            |sp| sp.to_string_lossy().contains("python3.1") && sp.ends_with("site-packages")
372        ));
373    }
374}