1use std::borrow::Cow;
2use std::str::FromStr;
3use std::{
4 env, io,
5 path::{Path, PathBuf},
6};
7
8use fs_err as fs;
9use thiserror::Error;
10
11use uv_preview::PreviewFeature;
12use uv_pypi_types::Scheme;
13use uv_static::EnvVars;
14
15use crate::PythonVersion;
16
17#[derive(Debug)]
19pub struct VirtualEnvironment {
20 pub root: PathBuf,
22
23 pub executable: PathBuf,
26
27 pub base_executable: PathBuf,
29
30 pub scheme: Scheme,
32}
33
34#[derive(Debug, Clone)]
36pub struct PyVenvConfiguration {
37 pub(crate) home: Option<PathBuf>,
39 pub(crate) virtualenv: bool,
41 pub(crate) uv: bool,
43 pub(crate) relocatable: bool,
45 pub(crate) seed: bool,
47 pub(crate) include_system_site_packages: bool,
49 pub(crate) version: Option<PythonVersion>,
51}
52
53#[derive(Debug, Error)]
54pub enum Error {
55 #[error(transparent)]
56 Io(#[from] io::Error),
57 #[error("Broken virtual environment `{0}`: `pyvenv.cfg` is missing")]
58 MissingPyVenvCfg(PathBuf),
59 #[error("Broken virtual environment `{0}`: `pyvenv.cfg` could not be parsed")]
60 ParsePyVenvCfg(PathBuf, #[source] io::Error),
61}
62
63pub(crate) fn virtualenv_from_env() -> Option<PathBuf> {
67 if let Some(dir) = env::var_os(EnvVars::VIRTUAL_ENV).filter(|value| !value.is_empty()) {
68 return Some(PathBuf::from(dir));
69 }
70
71 None
72}
73
74#[derive(Debug, PartialEq, Eq, Copy, Clone)]
75pub(crate) enum CondaEnvironmentKind {
76 Base,
78 Child,
80}
81
82impl CondaEnvironmentKind {
83 fn from_prefix_path(path: &Path) -> Self {
92 if is_pixi_environment(path) {
95 return Self::Child;
96 }
97
98 if let Ok(conda_root) = env::var(EnvVars::CONDA_ROOT) {
100 if path == Path::new(&conda_root) {
101 return Self::Base;
102 }
103 }
104
105 let Ok(current_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
107 return Self::Child;
108 };
109
110 if path == Path::new(¤t_env) {
113 return Self::Child;
114 }
115
116 if !uv_preview::is_enabled(PreviewFeature::SpecialCondaEnvNames)
121 && (current_env == "base" || current_env == "root")
122 {
123 return Self::Base;
124 }
125
126 let Some(name) = path.file_name() else {
128 return Self::Child;
129 };
130
131 if name.to_str().is_some_and(|name| name == current_env) {
134 Self::Child
135 } else {
136 Self::Base
137 }
138 }
139}
140
141fn is_pixi_environment(path: &Path) -> bool {
143 path.join("conda-meta").join("pixi").is_file()
144}
145
146pub(crate) fn conda_environment_from_env(kind: CondaEnvironmentKind) -> Option<PathBuf> {
151 let dir = env::var_os(EnvVars::CONDA_PREFIX).filter(|value| !value.is_empty())?;
152 let path = PathBuf::from(dir);
153
154 if kind != CondaEnvironmentKind::from_prefix_path(&path) {
155 return None;
156 }
157
158 Some(path)
159}
160
161pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
167 let current_dir = crate::current_dir()?;
168
169 for dir in current_dir.ancestors() {
170 if uv_fs::is_virtualenv_base(dir) {
172 return Ok(Some(dir.to_path_buf()));
173 }
174
175 let dot_venv = dir.join(".venv");
177 if dot_venv.is_dir() {
178 if !uv_fs::is_virtualenv_base(&dot_venv) {
179 return Err(Error::MissingPyVenvCfg(dot_venv));
180 }
181 return Ok(Some(dot_venv));
182 }
183 }
184
185 Ok(None)
186}
187
188pub(crate) fn virtualenv_python_executable(venv: impl AsRef<Path>) -> PathBuf {
190 let venv = venv.as_ref();
191 if cfg!(windows) {
192 let default_executable = venv.join("Scripts").join("python.exe");
194 if default_executable.exists() {
195 return default_executable;
196 }
197
198 let executable = venv.join("bin").join("python.exe");
201 if executable.exists() {
202 return executable;
203 }
204
205 let executable = venv.join("python.exe");
207 if executable.exists() {
208 return executable;
209 }
210
211 default_executable
213 } else {
214 let default_executable = venv.join("bin").join("python3");
216 if default_executable.exists() {
217 return default_executable;
218 }
219
220 let executable = venv.join("bin").join("python");
221 if executable.exists() {
222 return executable;
223 }
224
225 default_executable
227 }
228}
229
230impl PyVenvConfiguration {
231 pub fn parse(cfg: impl AsRef<Path>) -> Result<Self, Error> {
233 let mut home = None;
234 let mut virtualenv = false;
235 let mut uv = false;
236 let mut relocatable = false;
237 let mut seed = false;
238 let mut include_system_site_packages = true;
239 let mut version = None;
240
241 let content = fs::read_to_string(&cfg)
245 .map_err(|err| Error::ParsePyVenvCfg(cfg.as_ref().to_path_buf(), err))?;
246 for line in content.lines() {
247 let Some((key, value)) = line.split_once('=') else {
248 continue;
249 };
250 match key.trim() {
251 "home" => {
252 home = Some(PathBuf::from(value.trim()));
253 }
254 "virtualenv" => {
255 virtualenv = true;
256 }
257 "uv" => {
258 uv = true;
259 }
260 "relocatable" => {
261 relocatable = value.trim().to_lowercase() == "true";
262 }
263 "seed" => {
264 seed = value.trim().to_lowercase() == "true";
265 }
266 "include-system-site-packages" => {
267 include_system_site_packages = value.trim().to_lowercase() == "true";
268 }
269 "version" | "version_info" => {
270 version = Some(
271 PythonVersion::from_str(value.trim())
272 .map_err(|e| io::Error::new(std::io::ErrorKind::InvalidData, e))?,
273 );
274 }
275 _ => {}
276 }
277 }
278
279 Ok(Self {
280 home,
281 virtualenv,
282 uv,
283 relocatable,
284 seed,
285 include_system_site_packages,
286 version,
287 })
288 }
289
290 pub fn is_virtualenv(&self) -> bool {
292 self.virtualenv
293 }
294
295 pub fn is_uv(&self) -> bool {
297 self.uv
298 }
299
300 pub(crate) fn is_relocatable(&self) -> bool {
302 self.relocatable
303 }
304
305 pub fn is_seed(&self) -> bool {
307 self.seed
308 }
309
310 pub fn include_system_site_packages(&self) -> bool {
312 self.include_system_site_packages
313 }
314
315 pub fn set(content: &str, key: &str, value: &str) -> String {
317 let mut lines = content.lines().map(Cow::Borrowed).collect::<Vec<_>>();
318 let mut found = false;
319 for line in &mut lines {
320 if let Some((lhs, _)) = line.split_once('=') {
321 if lhs.trim() == key {
322 *line = Cow::Owned(format!("{key} = {value}"));
323 found = true;
324 break;
325 }
326 }
327 }
328 if !found {
329 lines.push(Cow::Owned(format!("{key} = {value}")));
330 }
331 if lines.is_empty() {
332 String::new()
333 } else {
334 format!("{}\n", lines.join("\n"))
335 }
336 }
337}
338
339#[cfg(test)]
340mod tests {
341 use std::ffi::OsStr;
342
343 use indoc::indoc;
344 use temp_env::with_vars;
345 use tempfile::tempdir;
346
347 use super::*;
348
349 #[test]
350 fn pixi_environment_is_treated_as_child() {
351 let tempdir = tempdir().unwrap();
352 let prefix = tempdir.path();
353 let conda_meta = prefix.join("conda-meta");
354
355 fs::create_dir_all(&conda_meta).unwrap();
356 fs::write(conda_meta.join("pixi"), []).unwrap();
357
358 let vars = [
359 (EnvVars::CONDA_ROOT, None),
360 (EnvVars::CONDA_PREFIX, Some(prefix.as_os_str())),
361 (EnvVars::CONDA_DEFAULT_ENV, Some(OsStr::new("example"))),
362 ];
363
364 with_vars(vars, || {
365 assert_eq!(
366 CondaEnvironmentKind::from_prefix_path(prefix),
367 CondaEnvironmentKind::Child
368 );
369 });
370 }
371
372 #[test]
373 fn test_set_existing_key() {
374 let content = indoc! {"
375 home = /path/to/python
376 version = 3.8.0
377 include-system-site-packages = false
378 "};
379 let result = PyVenvConfiguration::set(content, "version", "3.9.0");
380 assert_eq!(
381 result,
382 indoc! {"
383 home = /path/to/python
384 version = 3.9.0
385 include-system-site-packages = false
386 "}
387 );
388 }
389
390 #[test]
391 fn test_set_new_key() {
392 let content = indoc! {"
393 home = /path/to/python
394 version = 3.8.0
395 "};
396 let result = PyVenvConfiguration::set(content, "include-system-site-packages", "false");
397 assert_eq!(
398 result,
399 indoc! {"
400 home = /path/to/python
401 version = 3.8.0
402 include-system-site-packages = false
403 "}
404 );
405 }
406
407 #[test]
408 fn test_set_key_no_spaces() {
409 let content = indoc! {"
410 home=/path/to/python
411 version=3.8.0
412 "};
413 let result = PyVenvConfiguration::set(content, "include-system-site-packages", "false");
414 assert_eq!(
415 result,
416 indoc! {"
417 home=/path/to/python
418 version=3.8.0
419 include-system-site-packages = false
420 "}
421 );
422 }
423
424 #[test]
425 fn test_set_key_prefix() {
426 let content = indoc! {"
427 home = /path/to/python
428 home_dir = /other/path
429 "};
430 let result = PyVenvConfiguration::set(content, "home", "new/path");
431 assert_eq!(
432 result,
433 indoc! {"
434 home = new/path
435 home_dir = /other/path
436 "}
437 );
438 }
439
440 #[test]
441 fn test_set_empty_content() {
442 let content = "";
443 let result = PyVenvConfiguration::set(content, "version", "3.9.0");
444 assert_eq!(
445 result,
446 indoc! {"
447 version = 3.9.0
448 "}
449 );
450 }
451}