uv_python/
prefix.rs

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