uv_python/
target.rs

1use std::path::{Path, PathBuf};
2
3use uv_pypi_types::Scheme;
4
5/// A `--target` directory into which packages can be installed, separate from a virtual environment
6/// or system Python interpreter.
7#[derive(Debug, Clone)]
8pub struct Target(PathBuf);
9
10impl Target {
11    /// Return the [`Scheme`] for the `--target` directory.
12    pub fn scheme(&self) -> Scheme {
13        Scheme {
14            purelib: self.0.clone(),
15            platlib: self.0.clone(),
16            scripts: self.0.join("bin"),
17            data: self.0.clone(),
18            include: self.0.join("include"),
19        }
20    }
21
22    /// Return an iterator over the `site-packages` directories inside the environment.
23    pub fn site_packages(&self) -> impl Iterator<Item = &Path> {
24        std::iter::once(self.0.as_path())
25    }
26
27    /// Initialize the `--target` directory.
28    pub fn init(&self) -> std::io::Result<()> {
29        fs_err::create_dir_all(&self.0)?;
30        Ok(())
31    }
32
33    /// Return the path to the `--target` directory.
34    pub fn root(&self) -> &Path {
35        &self.0
36    }
37}
38
39impl From<PathBuf> for Target {
40    fn from(path: PathBuf) -> Self {
41        Self(path)
42    }
43}