scoop-uv 0.9.0

Scoop up your Python envs — pyenv-style workflow powered by uv
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! uv CLI client

use std::path::{Path, PathBuf};
use std::process::Command;

use crate::error::{Result, ScoopError};
use crate::validate::PythonVersion;

/// Information about an installed Python version
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PythonInfo {
    /// The version string (e.g., "3.12.0")
    pub version: String,
    /// Path to the Python executable
    pub path: Option<PathBuf>,
    /// Whether this version is installed locally by uv
    pub installed: bool,
    /// Implementation (cpython, pypy, etc.)
    pub implementation: String,
}

/// Client for interacting with the uv CLI
pub struct UvClient {
    /// Path to the uv executable
    path: PathBuf,
}

impl UvClient {
    /// Create a new UvClient, finding uv in PATH
    pub fn new() -> Result<Self> {
        let path = which::which("uv").map_err(|_| ScoopError::UvNotFound)?;
        Ok(Self { path })
    }

    /// Create a new UvClient with a specific path
    pub fn with_path(path: PathBuf) -> Self {
        Self { path }
    }

    /// Get the uv version
    pub fn version(&self) -> Result<String> {
        let output = Command::new(&self.path)
            .arg("--version")
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: "uv --version".to_string(),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: "uv --version".to_string(),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }

    /// Create a virtual environment
    pub fn create_venv(&self, path: &Path, python_version: &str) -> Result<()> {
        let output = Command::new(&self.path)
            .arg("venv")
            .arg(path)
            .arg("--python")
            .arg(python_version)
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: format!("uv venv {} --python {}", path.display(), python_version),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: format!("uv venv {} --python {}", path.display(), python_version),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(())
    }

    /// Install a Python version
    pub fn install_python(&self, version: &str) -> Result<()> {
        let output = Command::new(&self.path)
            .arg("python")
            .arg("install")
            .arg(version)
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: format!("uv python install {version}"),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: format!("uv python install {version}"),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(())
    }

    /// List installed Python versions (raw output)
    pub fn list_pythons(&self) -> Result<Vec<String>> {
        let output = Command::new(&self.path)
            .arg("python")
            .arg("list")
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: "uv python list".to_string(),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: "uv python list".to_string(),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let versions: Vec<String> = stdout.lines().map(|s| s.trim().to_string()).collect();

        Ok(versions)
    }

    /// List installed Python versions with structured info
    pub fn list_installed_pythons(&self) -> Result<Vec<PythonInfo>> {
        let output = Command::new(&self.path)
            .arg("python")
            .arg("list")
            .arg("--only-installed")
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: "uv python list --only-installed".to_string(),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: "uv python list --only-installed".to_string(),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let pythons = parse_python_list(&stdout);

        Ok(pythons)
    }

    /// Uninstall a Python version
    pub fn uninstall_python(&self, version: &str) -> Result<()> {
        let output = Command::new(&self.path)
            .arg("python")
            .arg("uninstall")
            .arg(version)
            .output()
            .map_err(|e| ScoopError::PythonUninstallFailed {
                version: version.to_string(),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::PythonUninstallFailed {
                version: version.to_string(),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(())
    }

    /// Find an installed Python matching the version pattern
    pub fn find_python(&self, version_pattern: &str) -> Result<Option<PythonInfo>> {
        let installed = self.list_installed_pythons()?;

        if let Some(pattern) = PythonVersion::parse(version_pattern) {
            for info in installed {
                if let Some(ver) = PythonVersion::parse(&info.version) {
                    if pattern.matches(&ver) {
                        return Ok(Some(info));
                    }
                }
            }
        }

        Ok(None)
    }

    /// Install packages into a virtual environment.
    ///
    /// # Arguments
    ///
    /// * `venv_path` - Path to the virtual environment
    /// * `packages` - List of package specifications (e.g., "requests==2.31.0")
    ///
    /// # Errors
    ///
    /// Returns [`ScoopError::UvCommandFailed`] if installation fails.
    pub fn pip_install(&self, venv_path: &Path, packages: &[String]) -> Result<()> {
        if packages.is_empty() {
            return Ok(());
        }

        let mut cmd = Command::new(&self.path);
        cmd.arg("pip")
            .arg("install")
            .arg("--python")
            .arg(venv_path.join("bin").join("python"));

        for package in packages {
            cmd.arg(package);
        }

        let output = cmd.output().map_err(|e| ScoopError::UvCommandFailed {
            command: format!("uv pip install (into {})", venv_path.display()),
            message: e.to_string(),
        })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: format!("uv pip install (into {})", venv_path.display()),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(())
    }

    /// Install packages from a requirements file into a virtual environment.
    ///
    /// # Errors
    ///
    /// Returns [`ScoopError::UvCommandFailed`] if installation fails.
    pub fn pip_install_requirements(
        &self,
        venv_path: &Path,
        requirements_path: &Path,
    ) -> Result<()> {
        let output = Command::new(&self.path)
            .arg("pip")
            .arg("install")
            .arg("--python")
            .arg(venv_path.join("bin").join("python"))
            .arg("-r")
            .arg(requirements_path)
            .output()
            .map_err(|e| ScoopError::UvCommandFailed {
                command: format!("uv pip install -r {}", requirements_path.display()),
                message: e.to_string(),
            })?;

        if !output.status.success() {
            return Err(ScoopError::UvCommandFailed {
                command: format!("uv pip install -r {}", requirements_path.display()),
                message: String::from_utf8_lossy(&output.stderr).to_string(),
            });
        }

        Ok(())
    }

    /// Get the latest installed Python version
    pub fn latest_installed_python(&self) -> Result<Option<PythonInfo>> {
        let mut installed = self.list_installed_pythons()?;

        // Sort by version descending
        installed.sort_by(|a, b| {
            let va = PythonVersion::parse(&a.version);
            let vb = PythonVersion::parse(&b.version);
            match (va, vb) {
                (Some(a), Some(b)) => match b.major.cmp(&a.major) {
                    std::cmp::Ordering::Equal => match (b.minor, a.minor) {
                        (Some(bm), Some(am)) => bm.cmp(&am),
                        _ => std::cmp::Ordering::Equal,
                    },
                    other => other,
                },
                _ => std::cmp::Ordering::Equal,
            }
        });

        Ok(installed.into_iter().next())
    }
}

/// Parse uv python list output into structured info
fn parse_python_list(output: &str) -> Vec<PythonInfo> {
    let mut pythons = Vec::new();

    for line in output.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        // uv python list output format varies:
        // "cpython-3.12.0-macos-aarch64-none    /path/to/python"
        // "cpython-3.12.0    <download available>"
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        let version_part = parts[0];
        let path = if parts.len() > 1 && !parts[1].starts_with('<') {
            Some(PathBuf::from(parts[1]))
        } else {
            None
        };

        // Parse "cpython-3.12.0-macos-aarch64-none" format
        let segments: Vec<&str> = version_part.split('-').collect();
        if segments.is_empty() {
            continue;
        }

        let implementation = segments[0].to_string();
        let version = if segments.len() > 1 {
            segments[1].to_string()
        } else {
            version_part.to_string()
        };

        let installed = path.is_some();

        pythons.push(PythonInfo {
            version,
            path,
            installed,
            implementation,
        });
    }

    pythons
}

impl Default for UvClient {
    fn default() -> Self {
        Self::new().expect("uv not found in PATH")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_uv_client_creation() {
        // This test will only pass if uv is installed
        if which::which("uv").is_ok() {
            let client = UvClient::new();
            assert!(client.is_ok());
        }
    }

    #[test]
    fn test_parse_python_list_with_paths() {
        let output = r#"
cpython-3.12.0-macos-aarch64-none    /Users/test/.local/share/uv/python/cpython-3.12.0-macos-aarch64-none/bin/python3
cpython-3.11.8-macos-aarch64-none    /Users/test/.local/share/uv/python/cpython-3.11.8-macos-aarch64-none/bin/python3
"#;
        let pythons = parse_python_list(output);
        assert_eq!(pythons.len(), 2);

        assert_eq!(pythons[0].version, "3.12.0");
        assert_eq!(pythons[0].implementation, "cpython");
        assert!(pythons[0].installed);
        assert!(pythons[0].path.is_some());

        assert_eq!(pythons[1].version, "3.11.8");
        assert_eq!(pythons[1].implementation, "cpython");
    }

    #[test]
    fn test_parse_python_list_without_paths() {
        let output = r#"
cpython-3.13.0    <download available>
cpython-3.12.0    <download available>
"#;
        let pythons = parse_python_list(output);
        assert_eq!(pythons.len(), 2);

        assert_eq!(pythons[0].version, "3.13.0");
        assert!(!pythons[0].installed);
        assert!(pythons[0].path.is_none());
    }

    #[test]
    fn test_parse_python_list_mixed() {
        let output = r#"
cpython-3.12.0-macos-aarch64-none    /path/to/python3
cpython-3.11.0    <download available>
pypy-3.10.0-macos-aarch64-none    /path/to/pypy
"#;
        let pythons = parse_python_list(output);
        assert_eq!(pythons.len(), 3);

        assert_eq!(pythons[0].implementation, "cpython");
        assert!(pythons[0].installed);

        assert_eq!(pythons[1].implementation, "cpython");
        assert!(!pythons[1].installed);

        assert_eq!(pythons[2].implementation, "pypy");
        assert!(pythons[2].installed);
    }

    #[test]
    fn test_parse_python_list_empty() {
        let output = "";
        let pythons = parse_python_list(output);
        assert!(pythons.is_empty());
    }

    #[test]
    fn test_python_info_equality() {
        let info1 = PythonInfo {
            version: "3.12.0".to_string(),
            path: Some(PathBuf::from("/path/to/python")),
            installed: true,
            implementation: "cpython".to_string(),
        };

        let info2 = PythonInfo {
            version: "3.12.0".to_string(),
            path: Some(PathBuf::from("/path/to/python")),
            installed: true,
            implementation: "cpython".to_string(),
        };

        assert_eq!(info1, info2);
    }

    #[test]
    fn test_uv_client_with_path() {
        let client = UvClient::with_path(PathBuf::from("/usr/bin/uv"));
        assert_eq!(client.path, PathBuf::from("/usr/bin/uv"));
    }

    // =========================================================================
    // parse_python_list Security & Edge Case Tests
    // =========================================================================

    /// Path traversal attempt in python path - should be parsed as-is (no sanitization needed)
    /// The path is used for display only, not for execution
    #[test]
    fn test_parse_python_list_path_traversal_attempt() {
        let output = "cpython-3.12.0-macos-aarch64-none    ../../../etc/passwd\n";
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 1);
        assert_eq!(pythons[0].version, "3.12.0");
        // Path is stored as-is - validation happens elsewhere
        assert_eq!(pythons[0].path, Some(PathBuf::from("../../../etc/passwd")));
    }

    /// Very long input line - DoS resistance
    #[test]
    fn test_parse_python_list_very_long_line() {
        let long_path = "/very/long/".to_string() + &"x".repeat(10_000);
        let output = format!("cpython-3.12.0-macos-aarch64-none    {}\n", long_path);
        let pythons = parse_python_list(&output);

        assert_eq!(pythons.len(), 1);
        assert_eq!(pythons[0].version, "3.12.0");
        assert!(pythons[0].path.is_some());
    }

    /// Unicode in paths - should handle properly
    #[test]
    fn test_parse_python_list_unicode_path() {
        let output = "cpython-3.12.0-macos-aarch64-none    /Users/한글/python\n";
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 1);
        assert_eq!(pythons[0].path, Some(PathBuf::from("/Users/한글/python")));
    }

    /// Spaces in path (quoted) - note: current parser splits on whitespace
    #[test]
    fn test_parse_python_list_multiple_whitespace() {
        // Multiple spaces between version and path
        let output = "cpython-3.12.0-macos-aarch64-none        /path/to/python\n";
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 1);
        assert_eq!(pythons[0].path, Some(PathBuf::from("/path/to/python")));
    }

    /// Only whitespace lines
    #[test]
    fn test_parse_python_list_only_whitespace() {
        let output = "   \n\t\n  \t  \n";
        let pythons = parse_python_list(output);
        assert!(pythons.is_empty());
    }

    /// Malformed version string without hyphen
    #[test]
    fn test_parse_python_list_malformed_version() {
        let output = "cpython\n";
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 1);
        // When no hyphen, the entire string becomes implementation
        assert_eq!(pythons[0].implementation, "cpython");
        assert_eq!(pythons[0].version, "cpython"); // Fallback to full string
    }

    /// Version with many segments (e.g., 3.12.1.post1)
    #[test]
    fn test_parse_python_list_version_with_suffix() {
        let output = "cpython-3.12.1-macos-aarch64-none    /path/to/python\n";
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 1);
        assert_eq!(pythons[0].version, "3.12.1");
        assert_eq!(pythons[0].implementation, "cpython");
    }

    /// Different Python implementations
    #[test]
    fn test_parse_python_list_various_implementations() {
        let output = r#"
cpython-3.12.0    /path/cpython
pypy-3.10.0    /path/pypy
graalpy-3.11.0    /path/graalpy
"#;
        let pythons = parse_python_list(output);

        assert_eq!(pythons.len(), 3);
        assert_eq!(pythons[0].implementation, "cpython");
        assert_eq!(pythons[1].implementation, "pypy");
        assert_eq!(pythons[2].implementation, "graalpy");
    }

    /// Null bytes and control characters - should not panic
    #[test]
    fn test_parse_python_list_control_characters() {
        // Lines are split by \n, so embedded \0 stays in line
        let output = "cpython-3.12.0\x00-injected    /path\n";
        let result = std::panic::catch_unwind(|| parse_python_list(output));
        assert!(result.is_ok()); // Should not panic
    }

    // =========================================================================
    // PythonInfo Tests
    // =========================================================================

    /// PythonInfo with None path
    #[test]
    fn test_python_info_none_path() {
        let info = PythonInfo {
            version: "3.12.0".to_string(),
            path: None,
            installed: false,
            implementation: "cpython".to_string(),
        };

        assert!(info.path.is_none());
        assert!(!info.installed);
    }
}