|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use pet_conda::package::{CondaPackageInfo, Package}; |
| 7 | +use pet_core::{ |
| 8 | + env::PythonEnv, |
| 9 | + python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind}, |
| 10 | + reporter::Reporter, |
| 11 | + Locator, LocatorKind, |
| 12 | +}; |
| 13 | +use pet_python_utils::executable::find_executables; |
| 14 | + |
| 15 | +pub fn is_pixi_env(path: &Path) -> bool { |
| 16 | + path.join("conda-meta").join("pixi").is_file() |
| 17 | +} |
| 18 | + |
| 19 | +fn get_pixi_prefix(env: &PythonEnv) -> Option<PathBuf> { |
| 20 | + env.prefix.clone().or_else(|| { |
| 21 | + env.executable.parent().and_then(|parent_dir| { |
| 22 | + if is_pixi_env(parent_dir) { |
| 23 | + Some(parent_dir.to_path_buf()) |
| 24 | + } else if parent_dir.ends_with("bin") || parent_dir.ends_with("Scripts") { |
| 25 | + parent_dir |
| 26 | + .parent() |
| 27 | + .filter(|parent| is_pixi_env(parent)) |
| 28 | + .map(|parent| parent.to_path_buf()) |
| 29 | + } else { |
| 30 | + None |
| 31 | + } |
| 32 | + }) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +pub struct Pixi {} |
| 37 | + |
| 38 | +impl Pixi { |
| 39 | + pub fn new() -> Pixi { |
| 40 | + Pixi {} |
| 41 | + } |
| 42 | +} |
| 43 | +impl Default for Pixi { |
| 44 | + fn default() -> Self { |
| 45 | + Self::new() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Locator for Pixi { |
| 50 | + fn get_kind(&self) -> LocatorKind { |
| 51 | + LocatorKind::Pixi |
| 52 | + } |
| 53 | + fn supported_categories(&self) -> Vec<PythonEnvironmentKind> { |
| 54 | + vec![PythonEnvironmentKind::Pixi] |
| 55 | + } |
| 56 | + |
| 57 | + fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> { |
| 58 | + get_pixi_prefix(env).and_then(|prefix| { |
| 59 | + if !is_pixi_env(&prefix) { |
| 60 | + return None; |
| 61 | + } |
| 62 | + |
| 63 | + let name = prefix |
| 64 | + .file_name() |
| 65 | + .and_then(|name| name.to_str()) |
| 66 | + .unwrap_or_default() |
| 67 | + .to_string(); |
| 68 | + |
| 69 | + let symlinks = find_executables(&prefix); |
| 70 | + |
| 71 | + let version = CondaPackageInfo::from(&prefix, &Package::Python) |
| 72 | + .map(|package_info| package_info.version); |
| 73 | + |
| 74 | + Some( |
| 75 | + PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Pixi)) |
| 76 | + .executable(Some(env.executable.clone())) |
| 77 | + .name(Some(name)) |
| 78 | + .prefix(Some(prefix)) |
| 79 | + .symlinks(Some(symlinks)) |
| 80 | + .version(version) |
| 81 | + .build(), |
| 82 | + ) |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + fn find(&self, _reporter: &dyn Reporter) {} |
| 87 | +} |
0 commit comments