|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -pub mod messaging; |
5 | | -pub mod utils; |
| 4 | +use global_virtualenvs::list_global_virtual_envs; |
| 5 | +use known::EnvironmentApi; |
| 6 | +use locator::{Locator, LocatorResult}; |
| 7 | +use messaging::{create_dispatcher, JsonRpcDispatcher, MessageDispatcher}; |
| 8 | +use std::thread::{self, JoinHandle}; |
| 9 | +use utils::PythonEnv; |
| 10 | + |
6 | 11 | pub mod common_python; |
7 | | -pub mod logging; |
8 | 12 | pub mod conda; |
9 | | -pub mod known; |
10 | | -pub mod pyenv; |
11 | 13 | pub mod global_virtualenvs; |
12 | | -pub mod virtualenvwrapper; |
| 14 | +pub mod homebrew; |
| 15 | +pub mod known; |
| 16 | +pub mod locator; |
| 17 | +pub mod logging; |
| 18 | +pub mod messaging; |
13 | 19 | pub mod pipenv; |
14 | | -pub mod virtualenv; |
| 20 | +pub mod pyenv; |
| 21 | +pub mod utils; |
15 | 22 | pub mod venv; |
16 | | -pub mod locator; |
| 23 | +pub mod virtualenv; |
| 24 | +pub mod virtualenvwrapper; |
17 | 25 | pub mod windows_registry; |
18 | 26 | pub mod windows_store; |
| 27 | + |
| 28 | +pub fn find_and_report_envs() { |
| 29 | + let mut dispatcher: JsonRpcDispatcher = create_dispatcher(); |
| 30 | + |
| 31 | + // 1. Find using known global locators. |
| 32 | + find_using_global_finders(&mut dispatcher); |
| 33 | + |
| 34 | + // Step 2: Search in some global locations for virtual envs. |
| 35 | + find_in_global_virtual_env_dirs(&mut dispatcher); |
| 36 | + |
| 37 | + // Step 3: Finally find in the current PATH variable |
| 38 | + let environment = EnvironmentApi::new(); |
| 39 | + let mut path_locator = common_python::PythonOnPath::with(&environment); |
| 40 | + report_result(path_locator.find(), &mut dispatcher) |
| 41 | +} |
| 42 | + |
| 43 | +fn find_using_global_finders(dispatcher: &mut JsonRpcDispatcher) { |
| 44 | + // Step 1: These environments take precedence over all others. |
| 45 | + // As they are very specific and guaranteed to be specific type. |
| 46 | + #[cfg(windows)] |
| 47 | + fn find() -> Vec<JoinHandle<std::option::Option<LocatorResult>>> { |
| 48 | + // The order matters, |
| 49 | + // Windows store can sometimes get detected via registry locator (but we want to avoid that), |
| 50 | + // difficult to repro, but we have see this on Karthiks machine |
| 51 | + // Windows registry can contain conda envs (e.g. installing Ananconda will result in registry entries). |
| 52 | + // Conda is best done last, as Windows Registry and Pyenv can also contain conda envs, |
| 53 | + // Thus lets leave the generic conda locator to last to find all remaining conda envs. |
| 54 | + // pyenv can be treated as a virtualenvwrapper environment, hence virtualenvwrapper needs to be detected first |
| 55 | + vec![ |
| 56 | + // 1. windows store |
| 57 | + thread::spawn(|| { |
| 58 | + let environment = EnvironmentApi::new(); |
| 59 | + let mut windows_store = windows_store::WindowsStore::with(&environment); |
| 60 | + windows_store.find() |
| 61 | + }), |
| 62 | + // 2. windows registry |
| 63 | + thread::spawn(|| { |
| 64 | + let environment = EnvironmentApi::new(); |
| 65 | + let mut conda_locator = conda::Conda::with(&environment); |
| 66 | + windows_registry::WindowsRegistry::with(&mut conda_locator).find() |
| 67 | + }), |
| 68 | + // 3. virtualenvwrapper |
| 69 | + thread::spawn(|| { |
| 70 | + let environment = EnvironmentApi::new(); |
| 71 | + virtualenvwrapper::VirtualEnvWrapper::with(&environment).find() |
| 72 | + }), |
| 73 | + // 4. pyenv |
| 74 | + thread::spawn(|| { |
| 75 | + let environment = EnvironmentApi::new(); |
| 76 | + let mut conda_locator = conda::Conda::with(&environment); |
| 77 | + pyenv::PyEnv::with(&environment, &mut conda_locator).find() |
| 78 | + }), |
| 79 | + // 5. conda |
| 80 | + thread::spawn(|| { |
| 81 | + let environment = EnvironmentApi::new(); |
| 82 | + conda::Conda::with(&environment).find() |
| 83 | + }), |
| 84 | + ] |
| 85 | + } |
| 86 | + |
| 87 | + #[cfg(unix)] |
| 88 | + fn find() -> Vec<JoinHandle<std::option::Option<LocatorResult>>> { |
| 89 | + // The order matters, |
| 90 | + // pyenv can be treated as a virtualenvwrapper environment, hence virtualenvwrapper needs to be detected first |
| 91 | + // Homebrew can happen anytime |
| 92 | + // Conda is best done last, as pyenv can also contain conda envs, |
| 93 | + // Thus lets leave the generic conda locator to last to find all remaining conda envs. |
| 94 | + |
| 95 | + vec![ |
| 96 | + // 1. virtualenvwrapper |
| 97 | + thread::spawn(|| { |
| 98 | + let environment = EnvironmentApi::new(); |
| 99 | + virtualenvwrapper::VirtualEnvWrapper::with(&environment).find() |
| 100 | + }), |
| 101 | + // 2. pyenv |
| 102 | + thread::spawn(|| { |
| 103 | + let environment = EnvironmentApi::new(); |
| 104 | + let mut conda_locator = conda::Conda::with(&environment); |
| 105 | + pyenv::PyEnv::with(&environment, &mut conda_locator).find() |
| 106 | + }), |
| 107 | + // 3. homebrew |
| 108 | + thread::spawn(|| { |
| 109 | + let environment = EnvironmentApi::new(); |
| 110 | + homebrew::Homebrew::with(&environment).find() |
| 111 | + }), |
| 112 | + // 4. conda |
| 113 | + thread::spawn(|| { |
| 114 | + let environment = EnvironmentApi::new(); |
| 115 | + conda::Conda::with(&environment).find() |
| 116 | + }), |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + for handle in find() { |
| 121 | + if let Ok(result) = handle.join() { |
| 122 | + report_result(result, dispatcher); |
| 123 | + } else { |
| 124 | + log::error!("Error getting result from thread."); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn find_in_global_virtual_env_dirs(dispatcher: &mut JsonRpcDispatcher) -> Option<LocatorResult> { |
| 130 | + // Step 1: These environments take precedence over all others. |
| 131 | + // As they are very specific and guaranteed to be specific type. |
| 132 | + |
| 133 | + let environment = EnvironmentApi::new(); |
| 134 | + let virtualenv_locator = virtualenv::VirtualEnv::new(); |
| 135 | + let venv_locator = venv::Venv::new(); |
| 136 | + let virtualenvwrapper = virtualenvwrapper::VirtualEnvWrapper::with(&environment); |
| 137 | + let pipenv_locator = pipenv::PipEnv::new(); |
| 138 | + #[cfg(unix)] |
| 139 | + let homebrew_locator = homebrew::Homebrew::with(&environment); |
| 140 | + |
| 141 | + let venv_type_locators = vec![ |
| 142 | + Box::new(pipenv_locator) as Box<dyn Locator>, |
| 143 | + Box::new(virtualenvwrapper) as Box<dyn Locator>, |
| 144 | + Box::new(venv_locator) as Box<dyn Locator>, |
| 145 | + Box::new(virtualenv_locator) as Box<dyn Locator>, |
| 146 | + ]; |
| 147 | + |
| 148 | + // Step 2: Search in some global locations for virtual envs. |
| 149 | + for env in list_global_virtual_envs(&environment) { |
| 150 | + if dispatcher.was_environment_reported(&env) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + // 1. First must be homebrew, as it is the most specific and supports symlinks |
| 155 | + #[cfg(unix)] |
| 156 | + if resolve_and_report_environment(&homebrew_locator, &env, dispatcher) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + // 3. Finally Check if these are some kind of virtual env or pipenv. |
| 161 | + // Pipeenv before virtualenvwrapper as it is more specific. |
| 162 | + // Because pipenv environments are also virtualenvwrapper environments. |
| 163 | + // Before venv, as all venvs are also virtualenvwrapper environments. |
| 164 | + // Before virtualenv as this is more specific. |
| 165 | + // All venvs are also virtualenvs environments. |
| 166 | + for locator in &venv_type_locators { |
| 167 | + if resolve_and_report_environment(locator.as_ref(), &env, dispatcher) { |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + None |
| 173 | +} |
| 174 | + |
| 175 | +fn resolve_and_report_environment( |
| 176 | + locator: &dyn Locator, |
| 177 | + env: &PythonEnv, |
| 178 | + dispatcher: &mut JsonRpcDispatcher, |
| 179 | +) -> bool { |
| 180 | + if let Some(env) = locator.resolve(env) { |
| 181 | + dispatcher.report_environment(env); |
| 182 | + return true; |
| 183 | + } |
| 184 | + false |
| 185 | +} |
| 186 | + |
| 187 | +fn report_result(result: Option<LocatorResult>, dispatcher: &mut JsonRpcDispatcher) { |
| 188 | + if let Some(result) = result { |
| 189 | + result |
| 190 | + .environments |
| 191 | + .iter() |
| 192 | + .for_each(|e| dispatcher.report_environment(e.clone())); |
| 193 | + result |
| 194 | + .managers |
| 195 | + .iter() |
| 196 | + .for_each(|m| dispatcher.report_environment_manager(m.clone())); |
| 197 | + } |
| 198 | +} |
0 commit comments