|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use std::fs; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +use crate::known; |
| 8 | +use crate::messaging; |
| 9 | +use crate::utils::find_python_binary_path; |
| 10 | + |
| 11 | +#[cfg(windows)] |
| 12 | +fn get_home_pyenv_dir(environment: &impl known::Environment) -> Option<String> { |
| 13 | + let home = environment.get_user_home()?; |
| 14 | + PathBuf::from(home) |
| 15 | + .join(".pyenv") |
| 16 | + .join("pyenv-win") |
| 17 | + .into_os_string() |
| 18 | + .into_string() |
| 19 | + .ok() |
| 20 | +} |
| 21 | + |
| 22 | +#[cfg(unix)] |
| 23 | +fn get_home_pyenv_dir(environment: &impl known::Environment) -> Option<String> { |
| 24 | + let home = environment.get_user_home()?; |
| 25 | + PathBuf::from(home) |
| 26 | + .join(".pyenv") |
| 27 | + .into_os_string() |
| 28 | + .into_string() |
| 29 | + .ok() |
| 30 | +} |
| 31 | + |
| 32 | +fn get_binary_from_known_paths(environment: &impl known::Environment) -> Option<String> { |
| 33 | + for known_path in environment.get_know_global_search_locations() { |
| 34 | + let bin = known_path.join("pyenv"); |
| 35 | + if bin.exists() { |
| 36 | + return bin.into_os_string().into_string().ok(); |
| 37 | + } |
| 38 | + } |
| 39 | + None |
| 40 | +} |
| 41 | + |
| 42 | +fn get_pyenv_dir(environment: &impl known::Environment) -> Option<String> { |
| 43 | + // Check if the pyenv environment variables exist: PYENV on Windows, PYENV_ROOT on Unix. |
| 44 | + // They contain the path to pyenv's installation folder. |
| 45 | + // If they don't exist, use the default path: ~/.pyenv/pyenv-win on Windows, ~/.pyenv on Unix. |
| 46 | + // If the interpreter path starts with the path to the pyenv folder, then it is a pyenv environment. |
| 47 | + // See https://github.com/pyenv/pyenv#locating-the-python-installation for general usage, |
| 48 | + // And https://github.com/pyenv-win/pyenv-win for Windows specifics. |
| 49 | + |
| 50 | + match environment.get_env_var("PYENV_ROOT".to_string()) { |
| 51 | + Some(dir) => Some(dir), |
| 52 | + None => match environment.get_env_var("PYENV".to_string()) { |
| 53 | + Some(dir) => Some(dir), |
| 54 | + None => get_home_pyenv_dir(environment), |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn get_pyenv_binary(environment: &impl known::Environment) -> Option<String> { |
| 60 | + let dir = get_pyenv_dir(environment)?; |
| 61 | + let exe = PathBuf::from(dir).join("bin").join("pyenv"); |
| 62 | + if fs::metadata(&exe).is_ok() { |
| 63 | + exe.into_os_string().into_string().ok() |
| 64 | + } else { |
| 65 | + get_binary_from_known_paths(environment) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub fn find_and_report( |
| 70 | + dispatcher: &mut impl messaging::MessageDispatcher, |
| 71 | + environment: &impl known::Environment, |
| 72 | +) -> Option<()> { |
| 73 | + let pyenv_dir = get_pyenv_dir(environment)?; |
| 74 | + |
| 75 | + if let Some(pyenv_binary) = get_pyenv_binary(environment) { |
| 76 | + let params = messaging::EnvManager::new(vec![pyenv_binary], None); |
| 77 | + let message = messaging::EnvManagerMessage::new(params); |
| 78 | + dispatcher.send_message(message); |
| 79 | + } |
| 80 | + |
| 81 | + let versions_dir = PathBuf::from(&pyenv_dir) |
| 82 | + .join("versions") |
| 83 | + .into_os_string() |
| 84 | + .into_string() |
| 85 | + .ok()?; |
| 86 | + |
| 87 | + let pyenv_binary_for_activation = match get_pyenv_binary(environment) { |
| 88 | + Some(binary) => binary, |
| 89 | + None => "pyenv".to_string(), |
| 90 | + }; |
| 91 | + for entry in fs::read_dir(&versions_dir).ok()? { |
| 92 | + if let Ok(path) = entry { |
| 93 | + let path = path.path(); |
| 94 | + if path.is_dir() { |
| 95 | + if let Some(executable) = find_python_binary_path(&path) { |
| 96 | + let version = path.file_name().unwrap().to_string_lossy().to_string(); |
| 97 | + dispatcher.send_message(messaging::PythonEnvironment::new( |
| 98 | + "Python".to_string(), |
| 99 | + vec![executable.into_os_string().into_string().unwrap()], |
| 100 | + "Pyenv".to_string(), |
| 101 | + Some(version.clone()), |
| 102 | + Some(vec![ |
| 103 | + pyenv_binary_for_activation.clone(), |
| 104 | + "shell".to_string(), |
| 105 | + version, |
| 106 | + ]), |
| 107 | + Some(path.into_os_string().into_string().unwrap()), |
| 108 | + )); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + None |
| 115 | +} |
0 commit comments