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
16 changes: 9 additions & 7 deletions crates/pet-conda/src/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn get_conda_environment_info(
if let Some(conda_dir) = &conda_install_folder {
if conda_dir.exists() {
trace!(
"Conda install folder {}, found, & will not be used for the Conda Env: {}",
"Conda install folder {}, found, & will be used for the Conda Env: {}",
env_path.display(),
conda_dir.display()
);
Expand Down Expand Up @@ -140,18 +140,15 @@ pub fn get_conda_environment_info(
* This function returns the path to the conda installation that was used to create the environment.
*/
pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Option<PathBuf> {
// Possible the env_path is the root conda install folder.
if is_conda_install(env_path) {
return Some(env_path.to_path_buf());
}

// If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder.
if let Some(parent) = env_path.ancestors().nth(2) {
if is_conda_install(parent) {
return Some(parent.to_path_buf());
}
}

// First look for the conda-meta/history file in the environment folder.
// This could be a conda envirment (not root) but has `conda` installed in it.
let conda_meta_history = env_path.join("conda-meta").join("history");
if let Ok(reader) = std::fs::read_to_string(conda_meta_history.clone()) {
if let Some(line) = reader.lines().map(|l| l.trim()).find(|l| {
Expand All @@ -169,7 +166,12 @@ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Optio
}
}

None
// Possible the env_path is the root conda install folder.
if is_conda_install(env_path) {
Some(env_path.to_path_buf())
} else {
None
}
}

fn get_conda_dir_from_cmd(cmd_line: String) -> Option<PathBuf> {
Expand Down
50 changes: 37 additions & 13 deletions crates/pet-conda/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Licensed under the MIT License.

use crate::{
conda_info::CondaInfo, env_variables::EnvVariables,
environments::get_conda_installation_used_to_create_conda_env, package::CondaPackageInfo,
utils::is_conda_env,
conda_info::CondaInfo,
env_variables::EnvVariables,
environments::get_conda_installation_used_to_create_conda_env,
package::CondaPackageInfo,
utils::{is_conda_env, is_conda_install},
};
use log::trace;
use pet_core::{manager::EnvManager, manager::EnvManagerType};
use std::{
env,
Expand Down Expand Up @@ -78,20 +81,41 @@ impl CondaManager {
if !is_conda_env(path) {
return None;
}
if let Some(manager) = get_conda_manager(path) {
Some(manager)

// If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder.
if let Some(parent) = path.ancestors().nth(2) {
if is_conda_install(parent) {
if let Some(manager) = get_conda_manager(parent) {
return Some(manager);
}
}
}

// Possible this is a conda environment in some other location
// Such as global env folders location configured via condarc file
// Or a conda env created using `-p` flag.
// Get the conda install folder from the history file.
// Or its in a location such as `~/.conda/envs` or `~/miniconda3/envs` where the conda install folder is not a parent of this path.
if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(path) {
get_conda_manager(&conda_install_folder)
} else {
// Possible this is a conda environment in the `envs` folder
let path = path.parent()?.parent()?;
// If this is a conda env and the parent is `.conda/envs`, then this is definitely NOT a root conda install folder.
// Hence never use conda installs from these env paths.
if let Some(parent) = path.parent() {
if parent.ends_with(".conda/envs") || parent.ends_with(".conda\\envs") {
trace!(
"Parent path ends with .conda/envs, not a root conda install folder: {:?}",
parent
);
return None;
}
}

if let Some(manager) = get_conda_manager(path) {
Some(manager)
} else {
// Possible this is a conda environment in some other location
// Such as global env folders location configured via condarc file
// Or a conda env created using `-p` flag.
// Get the conda install folder from the history file.
let conda_install_folder = get_conda_installation_used_to_create_conda_env(path)?;
get_conda_manager(&conda_install_folder)
trace!("No conda manager found for path: {:?}", path);
None
}
}
}
Expand Down
Loading