idt/downloaders/
pip.rs

1use std::path::Path;
2use std::path::PathBuf;
3
4/// Downloads and installs Python packages using pip in a virtual environment.
5///
6/// # Errors
7/// - Executing the `python3 -m venv` command fails or returns a non-zero exit status.
8/// - Executing the shell pipeline to activate the venv and install packages fails.
9/// - A filesystem operation (create/read/write/remove) fails.
10pub fn run(dev_tools_dir: &Path, tool: &str, packages: &[&str]) -> color_eyre::Result<PathBuf> {
11    let dev_tools_repo_dir = dev_tools_dir.join(tool);
12
13    std::fs::create_dir_all(&dev_tools_repo_dir)?;
14
15    ytil_cmd::silent_cmd("python3")
16        .args(["-m", "venv", &dev_tools_repo_dir.join(".venv").to_string_lossy()])
17        .status()?
18        .exit_ok()?;
19
20    ytil_cmd::silent_cmd("sh")
21        .args([
22            "-c",
23            &format!(
24                r"
25                    source {}/.venv/bin/activate && \
26                    pip install pip {packages} --upgrade
27                ",
28                dev_tools_repo_dir.display(),
29                packages = packages.join(" "),
30            ),
31        ])
32        .status()?
33        .exit_ok()?;
34
35    Ok(dev_tools_repo_dir.join(".venv").join("bin"))
36}