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 super::errors::BridgeError;
7use r2x_logger as logger;
8use std::fs;
9use std::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: &PathBuf) -> 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: &PathBuf) -> 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                .map(|name| name.contains("python"))
165                .unwrap_or(false)
166                && p.is_file()
167        }) {
168            return Ok(candidate);
169        }
170    }
171
172    Err(BridgeError::Initialization(format!(
173        "Path to python binary is not valid in {}",
174        venv_path.display()
175    )))
176}
177
178#[cfg(test)]
179mod tests {
180    use super::*;
181    use std::fs;
182    use tempfile::TempDir;
183
184    /// Helper to create a mock venv structure for testing
185    #[allow(dead_code)]
186    fn create_mock_venv_unix(python_version: &str) -> TempDir {
187        let temp_dir = TempDir::new().unwrap();
188        let venv_path = temp_dir.path();
189
190        // Create Unix structure: .venv/lib/python3.X/site-packages
191        let lib_dir = venv_path.join("lib");
192        let python_dir = lib_dir.join(python_version);
193        let site_packages = python_dir.join("site-packages");
194        fs::create_dir_all(&site_packages).unwrap();
195
196        // Create bin directory with python executable
197        let bin_dir = venv_path.join("bin");
198        fs::create_dir_all(&bin_dir).unwrap();
199        fs::write(bin_dir.join("python3"), "").unwrap();
200
201        temp_dir
202    }
203
204    /// Helper to create a mock Windows venv structure for testing
205    #[allow(dead_code)]
206    fn create_mock_venv_windows() -> TempDir {
207        let temp_dir = TempDir::new().unwrap();
208        let venv_path = temp_dir.path();
209
210        // Create Windows structure: .venv/Lib/site-packages
211        let lib_dir = venv_path.join("Lib");
212        let site_packages = lib_dir.join("site-packages");
213        fs::create_dir_all(&site_packages).unwrap();
214
215        // Create Scripts directory with python executable
216        let scripts_dir = venv_path.join("Scripts");
217        fs::create_dir_all(&scripts_dir).unwrap();
218        fs::write(scripts_dir.join("python.exe"), "").unwrap();
219
220        temp_dir
221    }
222
223    #[test]
224    #[cfg(unix)]
225    fn test_resolve_site_package_path_unix() {
226        let temp_venv = create_mock_venv_unix("python3.12");
227        let venv_path = temp_venv.path().to_path_buf();
228
229        let result = resolve_site_package_path(&venv_path);
230        assert!(result.is_ok());
231
232        let site_packages = result.unwrap();
233        assert!(site_packages.ends_with("lib/python3.12/site-packages"));
234        assert!(site_packages.exists());
235    }
236
237    #[test]
238    #[cfg(unix)]
239    fn test_resolve_site_package_path_unix_different_version() {
240        let temp_venv = create_mock_venv_unix("python3.11");
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
246        let site_packages = result.unwrap();
247        assert!(site_packages.ends_with("lib/python3.11/site-packages"));
248    }
249
250    #[test]
251    #[cfg(windows)]
252    fn test_resolve_site_package_path_windows() {
253        let temp_venv = create_mock_venv_windows();
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
259        let site_packages = result.unwrap();
260        assert!(site_packages.ends_with("Lib\\site-packages"));
261        assert!(site_packages.exists());
262    }
263
264    #[test]
265    fn test_resolve_site_package_path_venv_not_found() {
266        let non_existent_path = PathBuf::from("/tmp/non_existent_venv_12345");
267
268        let result = resolve_site_package_path(&non_existent_path);
269        assert!(result.is_err());
270
271        match result {
272            Err(BridgeError::VenvNotFound(path)) => {
273                assert_eq!(path, non_existent_path);
274            }
275            _ => panic!("Expected VenvNotFound error"),
276        }
277    }
278
279    #[test]
280    #[cfg(unix)]
281    fn test_resolve_site_package_path_missing_python_dir() {
282        let temp_dir = TempDir::new().unwrap();
283        let venv_path = temp_dir.path();
284
285        // Create lib dir but no python3.X subdirectory
286        let lib_dir = venv_path.join("lib");
287        fs::create_dir_all(&lib_dir).unwrap();
288
289        let result = resolve_site_package_path(&venv_path.to_path_buf());
290        assert!(result.is_err());
291
292        match result {
293            Err(BridgeError::Initialization(msg)) => {
294                assert!(msg.contains("No python3.X directory found"));
295            }
296            _ => panic!("Expected Initialization error"),
297        }
298    }
299
300    #[test]
301    #[cfg(unix)]
302    fn test_resolve_python_path_unix() {
303        let temp_venv = create_mock_venv_unix("python3.12");
304        let venv_path = temp_venv.path().to_path_buf();
305
306        let result = resolve_python_path(&venv_path);
307        assert!(result.is_ok());
308
309        let python_path = result.unwrap();
310        assert!(python_path.ends_with("bin/python3"));
311    }
312
313    #[test]
314    #[cfg(windows)]
315    fn test_resolve_python_path_windows() {
316        let temp_venv = create_mock_venv_windows();
317        let venv_path = temp_venv.path().to_path_buf();
318
319        let result = resolve_python_path(&venv_path);
320        assert!(result.is_ok());
321
322        let python_path = result.unwrap();
323        assert!(python_path.ends_with("Scripts\\python.exe"));
324    }
325
326    #[test]
327    fn test_python_lib_dir_constant() {
328        // Test that the compile-time constant is correct for the platform
329        #[cfg(unix)]
330        assert_eq!(PYTHON_LIB_DIR, "lib");
331
332        #[cfg(windows)]
333        assert_eq!(PYTHON_LIB_DIR, "Lib");
334    }
335
336    #[test]
337    fn test_python_bin_dir_constant() {
338        // Test that the compile-time constant is correct for the platform
339        #[cfg(unix)]
340        assert_eq!(PYTHON_BIN_DIR, "bin");
341
342        #[cfg(windows)]
343        assert_eq!(PYTHON_BIN_DIR, "Scripts");
344    }
345
346    #[test]
347    #[cfg(unix)]
348    fn test_resolve_site_package_path_with_multiple_python_versions() {
349        let temp_dir = TempDir::new().unwrap();
350        let venv_path = temp_dir.path();
351
352        // Create lib dir with multiple python versions
353        let lib_dir = venv_path.join("lib");
354        fs::create_dir_all(&lib_dir).unwrap();
355
356        // Create python3.11
357        let python_311 = lib_dir.join("python3.11");
358        let site_packages_311 = python_311.join("site-packages");
359        fs::create_dir_all(&site_packages_311).unwrap();
360
361        // Create python3.12 (should find the first one)
362        let python_312 = lib_dir.join("python3.12");
363        let site_packages_312 = python_312.join("site-packages");
364        fs::create_dir_all(&site_packages_312).unwrap();
365
366        let result = resolve_site_package_path(&venv_path.to_path_buf());
367        assert!(result.is_ok());
368
369        let site_packages = result.unwrap();
370        // Should find one of them (implementation finds first match)
371        assert!(site_packages.to_string_lossy().contains("python3.1"));
372        assert!(site_packages.ends_with("site-packages"));
373    }
374}