Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pet-core/src/os_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Environment for EnvironmentApi {
.into_iter()
.filter(|p| p.exists())
.collect::<Vec<PathBuf>>();

trace!("Env PATH: {:?}", paths);
self.global_search_locations
.lock()
.unwrap()
Expand Down
7 changes: 5 additions & 2 deletions crates/pet-homebrew/src/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub fn get_python_info(
None => None,
};

let mut symlinks = vec![python_exe_from_bin_dir.to_path_buf()];
let mut symlinks = vec![
python_exe_from_bin_dir.to_path_buf(),
resolved_exe.to_path_buf(),
];
if let Some(version) = &version {
symlinks.append(&mut get_known_symlinks(resolved_exe, version));
}
Expand Down Expand Up @@ -92,7 +95,7 @@ fn get_prefix(_resolved_file: &Path) -> Option<PathBuf> {
// }

// // 2. Linux
// if resolved_file.starts_with("/home/linuxbrew/.linuxbrew/Cellar") {
// if resolved_file.starts_with("/home/linuxbrew/.linuxbrew") {
// // Resolved exe is something like `/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.12.3/bin/python3.12`
// let reg_ex = Regex::new("/home/linuxbrew/.linuxbrew/Cellar/python@(\\d+\\.?\\d+\\.?)/(\\d+\\.?\\d+\\.?\\d+\\.?)/bin/python.*").unwrap();
// let captures = reg_ex.captures(resolved_file)?;
Expand Down
17 changes: 14 additions & 3 deletions crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use pet_core::{
use pet_fs::path::resolve_symlink;
use pet_python_utils::{env::PythonEnv, executable::find_executables};
use pet_virtualenv::is_virtualenv;
use std::{path::PathBuf, thread};
use std::{fs, path::PathBuf, thread};
use sym_links::is_homebrew_python;

mod env_variables;
mod environment_locations;
Expand Down Expand Up @@ -52,7 +53,17 @@ fn from(env: &PythonEnv) -> Option<PythonEnvironment> {
// Hence we never end up reporting 3.10 for home brew (as mentioned when you try to resolve the exe it points to existing install, now homebrew).
let exe = env.executable.clone();
let exe_file_name = exe.file_name()?;
let resolved_file = resolve_symlink(&exe).unwrap_or(exe.clone());
let mut resolved_file = resolve_symlink(&exe).unwrap_or(exe.clone());

// Possible the resolve exe needs to be resolved once again using canonicalize.
// Sometimes a symlink points to another symlink, and we need to resolve it to get the real exe.
// And for some reason even though they are symlinks, they are not resolved by `resolve_symlink`.
if let Some(resolved) = resolve_symlink(&exe).or(fs::canonicalize(&exe).ok()) {
if is_homebrew_python(&resolved) {
resolved_file = resolved;
}
}

// Cellar is where the executables will be installed, see below link
// https://docs.brew.sh/Formula-Cookbook#an-introduction
// From above link > Homebrew installs formulae to the Cellar at $(brew --cellar)
Expand All @@ -72,7 +83,7 @@ fn from(env: &PythonEnv) -> Option<PythonEnvironment> {
&PathBuf::from("/opt/homebrew/bin").join(exe_file_name),
&resolved_file,
)
} else if resolved_file.starts_with("/home/linuxbrew/.linuxbrew/Cellar") {
} else if resolved_file.starts_with("/home/linuxbrew/.linuxbrew") {
// Symlink - /home/linuxbrew/.linuxbrew/bin/python3.12
// Symlink - /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.12
// Real exe - /home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.12.3/bin/python3.12
Expand Down
61 changes: 47 additions & 14 deletions crates/pet-homebrew/src/sym_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ use lazy_static::lazy_static;
use pet_fs::path::resolve_symlink;
use pet_python_utils::executable::find_executables;
use regex::Regex;
use std::path::{Path, PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

lazy_static! {
static ref PYTHON_VERSION: Regex =
Regex::new(r"/python@((\d+\.?)*)/").expect("error parsing Version regex for Homebrew");
}

pub fn is_homebrew_python(exe: &Path) -> bool {
exe.starts_with("/opt/homebrew/Cellar")
|| exe.starts_with("/usr/local/Cellar")
|| exe.starts_with("/home/linuxbrew/.linuxbrew")
}

pub fn get_known_symlinks(
symlink_resolved_python_exe: &Path,
full_version: &String,
Expand Down Expand Up @@ -85,6 +94,10 @@ pub fn get_known_symlinks_impl(
PathBuf::from(format!("/opt/homebrew/Cellar/python@{}/{}/Frameworks/Python.framework/Versions/Current/bin/python{}", version, full_version, version)),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/{}/bin/python{}", version, version)),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/Current/bin/python{}", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python3", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python{}", version, version)),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{}", version)),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from(format!("/opt/homebrew/opt/python3/bin/python{}", version)),
// Check if this symlink is pointing to the same place as the resolved python exe
Expand All @@ -94,7 +107,11 @@ pub fn get_known_symlinks_impl(
] {

// Validate the symlinks
if possible_symlink == symlink_resolved_python_exe || resolve_symlink(&possible_symlink).unwrap_or_default() == symlink_resolved_python_exe {
if symlinks.contains(
&resolve_symlink(&possible_symlink)
.or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
Expand Down Expand Up @@ -125,12 +142,10 @@ pub fn get_known_symlinks_impl(
// 1. python 3.8 has sysprefix in /usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9
// 2. python 3.9 has sysprefix in /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9
// 3. python 3.11 has sysprefix in /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.11
// Hence till we know more about it, this path is not included, but left as commented
// So we can add it later if needed & tested
// PathBuf::from(format!(
// "/usr/local/opt/python@{}/bin/python{}",
// version, version
// )),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python3", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python{}", version, version)),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{}", version)),
PathBuf::from(format!(
"/usr/local/Cellar/python@{}/{}/bin/python{}",
version, full_version, version
Expand All @@ -149,7 +164,11 @@ pub fn get_known_symlinks_impl(
] {

// Validate the symlinks
if possible_symlink == symlink_resolved_python_exe || resolve_symlink(&possible_symlink).unwrap_or_default() == symlink_resolved_python_exe {
if symlinks.contains(
&resolve_symlink(&possible_symlink)
// .or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
Expand All @@ -160,7 +179,7 @@ pub fn get_known_symlinks_impl(
},
None => vec![],
}
} else if symlink_resolved_python_exe.starts_with("/home/linuxbrew/.linuxbrew/Cellar") {
} else if symlink_resolved_python_exe.starts_with("/home/linuxbrew/.linuxbrew") {
// Real exe - /home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.12.3/bin/python3.12

// Known symlinks include
Expand All @@ -175,11 +194,24 @@ pub fn get_known_symlinks_impl(
// See previous explanation
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3"),
PathBuf::from(format!("/home/linuxbrew/.linuxbrew/bin/python{}", version)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{}/{}/bin/python{}",
version, full_version, version
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{}/{}/bin/python3",
version, full_version
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{}/bin/python{}",
version, version
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{}/bin/python3",
version
)),
// This is a special folder, if users install python using other means, this file
// might get overridden. So we should only add this if this files points to the same place
PathBuf::from(format!("/usr/local/bin/python{}", version)),
Expand All @@ -189,10 +221,11 @@ pub fn get_known_symlinks_impl(
PathBuf::from("/usr/local/bin/python"),
] {
// Validate the symlinks
if possible_symlink == symlink_resolved_python_exe
|| resolve_symlink(&possible_symlink).unwrap_or_default()
== symlink_resolved_python_exe
{
if symlinks.contains(
&resolve_symlink(&possible_symlink)
.or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/pet/tests/ci_homebrew_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ fn verify_python_in_homebrew_contaner() {
symlinks: Some(vec![
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3"),
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3.12"),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3"),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.12"),
// On CI the Python version can change with minor updates, so we don't check the full version.
// PathBuf::from("/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.12.4/bin/python3.12"),
]),
Expand All @@ -48,6 +50,7 @@ fn verify_python_in_homebrew_contaner() {
version: Some("3.11.9".to_string()), // This can change on CI, so we don't check it
symlinks: Some(vec![
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3.11"),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.11"),
// On CI the Python version can change with minor updates, so we don't check the full version.
// PathBuf::from("/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.11.9/bin/python3.11"),
]),
Expand Down
Loading