1use std::borrow::Cow;
2use std::fmt;
3use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6use owo_colors::OwoColorize;
7use tracing::debug;
8
9use uv_cache::Cache;
10use uv_fs::{LockedFile, LockedFileError, Simplified};
11use uv_pep440::Version;
12
13use crate::discovery::find_python_installation;
14use crate::installation::PythonInstallation;
15use crate::virtualenv::{PyVenvConfiguration, virtualenv_python_executable};
16use crate::{
17 EnvironmentPreference, Error, Interpreter, Prefix, PythonNotFound, PythonPreference,
18 PythonRequest, Target,
19};
20
21#[derive(Debug, Clone)]
23pub struct PythonEnvironment(Arc<PythonEnvironmentShared>);
24
25#[derive(Debug, Clone)]
26struct PythonEnvironmentShared {
27 root: PathBuf,
28 interpreter: Interpreter,
29}
30
31#[derive(Clone, Debug, Error)]
35pub struct EnvironmentNotFound {
36 request: PythonRequest,
37 preference: EnvironmentPreference,
38}
39
40#[derive(Clone, Debug, Error)]
41pub struct InvalidEnvironment {
42 path: PathBuf,
43 pub kind: InvalidEnvironmentKind,
44}
45#[derive(Debug, Clone)]
46pub enum InvalidEnvironmentKind {
47 NotDirectory,
48 Empty,
49 MissingExecutable(PathBuf),
50}
51
52impl From<PythonNotFound> for EnvironmentNotFound {
53 fn from(value: PythonNotFound) -> Self {
54 Self {
55 request: value.request,
56 preference: value.environment_preference,
57 }
58 }
59}
60
61impl fmt::Display for EnvironmentNotFound {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 #[derive(Debug, Copy, Clone)]
64 enum SearchType {
65 Virtual,
67 System,
69 VirtualOrSystem,
71 }
72
73 impl fmt::Display for SearchType {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 match self {
76 Self::Virtual => write!(f, "virtual environment"),
77 Self::System => write!(f, "system Python installation"),
78 Self::VirtualOrSystem => {
79 write!(f, "virtual environment or system Python installation")
80 }
81 }
82 }
83 }
84
85 let search_type = match self.preference {
86 EnvironmentPreference::Any => SearchType::VirtualOrSystem,
87 EnvironmentPreference::ExplicitSystem => {
88 if self.request.is_explicit_system() {
89 SearchType::VirtualOrSystem
90 } else {
91 SearchType::Virtual
92 }
93 }
94 EnvironmentPreference::OnlySystem => SearchType::System,
95 EnvironmentPreference::OnlyVirtual => SearchType::Virtual,
96 };
97
98 if matches!(self.request, PythonRequest::Default | PythonRequest::Any) {
99 write!(f, "No {search_type} found")?;
100 } else {
101 write!(f, "No {search_type} found for {}", self.request)?;
102 }
103
104 match search_type {
105 SearchType::Virtual => write!(
108 f,
109 "; run `{}` to create an environment, or pass `{}` to install into a non-virtual environment",
110 "uv venv".green(),
111 "--system".green()
112 )?,
113 SearchType::VirtualOrSystem => {
114 write!(f, "; run `{}` to create an environment", "uv venv".green())?;
115 }
116 SearchType::System => {}
117 }
118
119 Ok(())
120 }
121}
122
123impl fmt::Display for InvalidEnvironment {
124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 write!(
126 f,
127 "Invalid environment at `{}`: {}",
128 self.path.user_display(),
129 self.kind
130 )
131 }
132}
133
134impl fmt::Display for InvalidEnvironmentKind {
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136 match self {
137 Self::NotDirectory => write!(f, "expected directory but found a file"),
138 Self::MissingExecutable(path) => {
139 write!(f, "missing Python executable at `{}`", path.user_display())
140 }
141 Self::Empty => write!(f, "directory is empty"),
142 }
143 }
144}
145
146impl PythonEnvironment {
147 pub fn find(
152 request: &PythonRequest,
153 preference: EnvironmentPreference,
154 python_preference: PythonPreference,
155 cache: &Cache,
156 ) -> Result<Self, Error> {
157 let installation =
158 match find_python_installation(request, preference, python_preference, cache)? {
159 Ok(installation) => installation,
160 Err(err) => return Err(EnvironmentNotFound::from(err).into()),
161 };
162 Ok(Self::from_installation(installation))
163 }
164
165 pub fn from_root(root: impl AsRef<Path>, cache: &Cache) -> Result<Self, Error> {
169 debug!(
170 "Checking for Python environment at: `{}`",
171 root.as_ref().user_display()
172 );
173 match root.as_ref().try_exists() {
174 Ok(true) => {}
175 Ok(false) => {
176 return Err(Error::MissingEnvironment(EnvironmentNotFound {
177 preference: EnvironmentPreference::Any,
178 request: PythonRequest::Directory(root.as_ref().to_owned()),
179 }));
180 }
181 Err(err) => return Err(Error::Discovery(err.into())),
182 }
183
184 if root.as_ref().is_file() {
185 return Err(InvalidEnvironment {
186 path: root.as_ref().to_path_buf(),
187 kind: InvalidEnvironmentKind::NotDirectory,
188 }
189 .into());
190 }
191
192 if root
193 .as_ref()
194 .read_dir()
195 .is_ok_and(|mut dir| dir.next().is_none())
196 {
197 return Err(InvalidEnvironment {
198 path: root.as_ref().to_path_buf(),
199 kind: InvalidEnvironmentKind::Empty,
200 }
201 .into());
202 }
203
204 let executable = virtualenv_python_executable(&root);
208
209 if !(executable.is_symlink() || executable.is_file()) {
211 return Err(InvalidEnvironment {
212 path: root.as_ref().to_path_buf(),
213 kind: InvalidEnvironmentKind::MissingExecutable(executable.clone()),
214 }
215 .into());
216 }
217
218 let interpreter = Interpreter::query(executable, cache)?;
219
220 Ok(Self(Arc::new(PythonEnvironmentShared {
221 root: interpreter.sys_prefix().to_path_buf(),
222 interpreter,
223 })))
224 }
225
226 pub fn from_installation(installation: PythonInstallation) -> Self {
228 Self::from_interpreter(installation.into_interpreter())
229 }
230
231 pub fn from_interpreter(interpreter: Interpreter) -> Self {
233 Self(Arc::new(PythonEnvironmentShared {
234 root: interpreter.sys_prefix().to_path_buf(),
235 interpreter,
236 }))
237 }
238
239 pub fn with_target(self, target: Target) -> std::io::Result<Self> {
241 let inner = Arc::unwrap_or_clone(self.0);
242 Ok(Self(Arc::new(PythonEnvironmentShared {
243 interpreter: inner.interpreter.with_target(target)?,
244 ..inner
245 })))
246 }
247
248 pub fn with_prefix(self, prefix: Prefix) -> std::io::Result<Self> {
250 let inner = Arc::unwrap_or_clone(self.0);
251 Ok(Self(Arc::new(PythonEnvironmentShared {
252 interpreter: inner.interpreter.with_prefix(prefix)?,
253 ..inner
254 })))
255 }
256
257 pub fn root(&self) -> &Path {
259 &self.0.root
260 }
261
262 pub fn interpreter(&self) -> &Interpreter {
266 &self.0.interpreter
267 }
268
269 pub fn cfg(&self) -> Result<PyVenvConfiguration, Error> {
272 Ok(PyVenvConfiguration::parse(self.0.root.join("pyvenv.cfg"))?)
273 }
274
275 pub fn set_pyvenv_cfg(&self, key: &str, value: &str) -> Result<(), Error> {
277 let content = fs_err::read_to_string(self.0.root.join("pyvenv.cfg"))?;
278 fs_err::write(
279 self.0.root.join("pyvenv.cfg"),
280 PyVenvConfiguration::set(&content, key, value),
281 )?;
282 Ok(())
283 }
284
285 pub fn relocatable(&self) -> bool {
287 self.cfg().is_ok_and(|cfg| cfg.is_relocatable())
288 }
289
290 pub fn python_executable(&self) -> &Path {
292 self.0.interpreter.sys_executable()
293 }
294
295 pub fn site_packages(&self) -> impl Iterator<Item = Cow<'_, Path>> {
303 self.0.interpreter.site_packages()
304 }
305
306 pub fn scripts(&self) -> &Path {
308 self.0.interpreter.scripts()
309 }
310
311 pub async fn lock(&self) -> Result<LockedFile, LockedFileError> {
313 self.0.interpreter.lock().await
314 }
315
316 pub fn into_interpreter(self) -> Interpreter {
320 Arc::unwrap_or_clone(self.0).interpreter
321 }
322
323 pub fn uses(&self, interpreter: &Interpreter) -> bool {
325 if cfg!(windows) {
328 let old_base_prefix = self.interpreter().sys_base_prefix();
332 let selected_base_prefix = interpreter.sys_base_prefix();
333 old_base_prefix == selected_base_prefix
334 } else {
335 self.interpreter().sys_executable() == interpreter.sys_executable()
337 || same_file::is_same_file(
338 self.interpreter().sys_executable(),
339 interpreter.sys_executable(),
340 )
341 .unwrap_or(false)
342 }
343 }
344
345 pub fn get_pyvenv_version_conflict(&self) -> Option<(Version, Version)> {
351 let cfg = self.cfg().ok()?;
352 let cfg_version = cfg.version?.into_version();
353
354 let exe_version = if cfg_version.release().get(2).is_none() {
356 self.interpreter().python_minor_version()
357 } else if cfg_version.pre().is_none() {
358 self.interpreter().python_patch_version()
359 } else {
360 self.interpreter().python_version().clone()
361 };
362
363 (cfg_version != exe_version).then_some((cfg_version, exe_version))
364 }
365}