Skip to content

Commit

Permalink
Changed the order of locaters (#136)
Browse files Browse the repository at this point in the history
Closes #135 

Rearranged the order of locater since Homebrew can be used to install
Poetry, Anoconda, etc.

---------

Co-authored-by: Don Jayamanne <don.jayamanne@outlook.com>
  • Loading branch information
Armd04 and DonJayamanne authored Aug 5, 2024
1 parent f8fa71c commit 8ea2f30
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/pet-conda/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn get_conda_envs_from_environment_txt(env_vars: &EnvVariables) -> Vec<PathB
if let Ok(reader) = fs::read_to_string(environment_txt.clone()) {
trace!("Found environments.txt file {:?}", environment_txt);
for line in reader.lines() {
let line = norm_case(&PathBuf::from(line.to_string()));
let line = norm_case(PathBuf::from(line.to_string()));
trace!("Conda env in environments.txt file {:?}", line);
if line.exists() {
envs.push(line);
Expand Down
1 change: 1 addition & 0 deletions crates/pet-homebrew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ msvc_spectre_libs = { version = "0.1.1", features = ["error"] }
pet-fs = { path = "../pet-fs" }
pet-python-utils = { path = "../pet-python-utils" }
pet-virtualenv = { path = "../pet-virtualenv" }
pet-conda = { path = "../pet-conda" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
lazy_static = "1.4.0"
Expand Down
15 changes: 15 additions & 0 deletions crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use env_variables::EnvVariables;
use environment_locations::get_homebrew_prefix_bin;
use environments::get_python_info;
use pet_conda::utils::is_conda_env;
use pet_core::{
env::PythonEnv,
os_environment::Environment,
Expand Down Expand Up @@ -47,6 +48,20 @@ fn from(env: &PythonEnv) -> Option<PythonEnvironment> {
return None;
}

if let Some(prefix) = &env.prefix {
if is_conda_env(prefix) {
return None;
}
}
// Possible this is a root conda env (hence parent directory is conda install dir).
if is_conda_env(env.executable.parent()?) {
return None;
}
// Possible this is a conda env (hence parent directory is Scripts/bin dir).
if is_conda_env(env.executable.parent()?.parent()?) {
return None;
}

// Note: Sometimes if Python 3.10 was installed by other means (e.g. from python.org or other)
// & then you install Python 3.10 via Homebrew, then some files will get installed via homebrew,
// However everything (symlinks, Python executable `sys.executable`, `sys.prefix`) eventually point back to the existing installation.
Expand Down
25 changes: 14 additions & 11 deletions crates/pet/src/locators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,11 @@ pub fn create_locators(
}
// 3. Pyenv Python
locators.push(Arc::new(PyEnv::from(environment, conda_locator.clone())));
// 4. Homebrew Python
if cfg!(unix) {
#[cfg(unix)]
use pet_homebrew::Homebrew;
#[cfg(unix)]
let homebrew_locator = Homebrew::from(environment);
#[cfg(unix)]
locators.push(Arc::new(homebrew_locator));
}
// 5. Conda Python

// 4. Conda Python
locators.push(conda_locator);
// 6. Support for Virtual Envs

// 5. Support for Virtual Envs
// The order of these matter.
// Basically PipEnv is a superset of VirtualEnvWrapper, which is a superset of Venv, which is a superset of VirtualEnv.
locators.push(poetry_locator);
Expand All @@ -68,6 +61,16 @@ pub fn create_locators(
// VirtualEnv is the most generic, hence should be the last.
locators.push(Arc::new(VirtualEnv::new()));

// 6. Homebrew Python
if cfg!(unix) {
#[cfg(unix)]
use pet_homebrew::Homebrew;
#[cfg(unix)]
let homebrew_locator = Homebrew::from(environment);
#[cfg(unix)]
locators.push(Arc::new(homebrew_locator));
}

// 7. Global Mac Python
// 8. CommandLineTools Python & xcode
if std::env::consts::OS == "macos" {
Expand Down

0 comments on commit 8ea2f30

Please sign in to comment.