Skip to content

Get names of conda envs when conda manager is known #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
46 changes: 21 additions & 25 deletions crates/pet-conda/src/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,27 @@ impl CondaEnvironment {
get_conda_environment_info(path, manager)
}

pub fn to_python_environment(
&self,
conda_dir: Option<PathBuf>,
conda_manager: Option<EnvManager>,
) -> PythonEnvironment {
pub fn to_python_environment(&self, conda_manager: Option<EnvManager>) -> PythonEnvironment {
#[allow(unused_assignments)]
let mut name: Option<String> = None;
if is_conda_install(&self.prefix) {
name = Some("base".to_string());
} else {
name = self
.prefix
.file_name()
.map(|name| name.to_str().unwrap_or_default().to_string());
}
// if the conda install folder is parent of the env folder, then we can use named activation.
// E.g. conda env is = <conda install>/envs/<env name>
// Then we can use `<conda install>/bin/conda activate -n <env name>`
if let Some(conda_dir) = conda_dir {
if !self.prefix.starts_with(conda_dir) {
name = None;

// We can name the conda envs only if we have a conda manager.
if let Some(conda_manager) = &conda_manager {
// If the conda manager for this environment is in the same folder as the conda environment,
// Then this is a root conda environment.
if conda_manager.executable.starts_with(&self.prefix)
&& is_conda_install(&self.prefix)
&& self.conda_dir.is_some()
{
name = Some("base".to_string());
} else {
name = self
.prefix
.file_name()
.map(|name| name.to_str().unwrap_or_default().to_string());
}
}
// This is a root env.

let builder = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Conda))
.executable(self.executable.clone())
.version(self.version.clone())
Expand All @@ -75,11 +72,10 @@ pub fn get_conda_environment_info(
// Not a conda environment (neither root nor a separate env).
return None;
}
// If we know the conda install folder, then we can use it.
let mut conda_install_folder = manager
.clone()
.and_then(|m| m.conda_dir)
.or_else(|| get_conda_installation_used_to_create_conda_env(env_path));
// Even if we have the conda manager, always fid the conda manager based on the env.
// & then use the given conda manager as a fallback.
let mut conda_install_folder = get_conda_installation_used_to_create_conda_env(env_path)
.or_else(|| manager.clone().and_then(|m| m.conda_dir));

if let Some(conda_dir) = &conda_install_folder {
if conda_dir.exists() {
Expand Down
17 changes: 6 additions & 11 deletions crates/pet-conda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ impl CondaLocator for Conda {
if environments.contains_key(&conda_env.prefix) {
continue;
}
let env = conda_env
.to_python_environment(Some(conda_dir.clone()), Some(manager.to_manager()));
let env = conda_env.to_python_environment(Some(manager.to_manager()));
environments.insert(conda_env.prefix.clone(), env.clone());
reporter.report_manager(&manager.to_manager());
reporter.report_environment(&env);
Expand Down Expand Up @@ -248,18 +247,15 @@ impl Locator for Conda {
if let Some(env) = get_conda_environment_info(path, &None) {
if let Some(conda_dir) = &env.conda_dir {
if let Some(manager) = self.get_manager(conda_dir) {
let env = env.to_python_environment(
Some(conda_dir.clone()),
Some(manager.to_manager()),
);
let env = env.to_python_environment(Some(manager.to_manager()));
environments.insert(path.clone(), env.clone());
return Some(env);
} else {
// We will still return the conda env even though we do not have the manager.
// This might seem incorrect, however the tool is about discovering environments.
// The client can activate this env either using another conda manager or using the activation scripts
error!("Unable to find Conda Manager for env (even though we have a conda_dir): {:?}", env);
let env = env.to_python_environment(Some(conda_dir.clone()), None);
let env = env.to_python_environment(None);
environments.insert(path.clone(), env.clone());
return Some(env);
}
Expand All @@ -268,7 +264,7 @@ impl Locator for Conda {
// This might seem incorrect, however the tool is about discovering environments.
// The client can activate this env either using another conda manager or using the activation scripts
error!("Unable to find Conda Manager for env: {:?}", env);
let env = env.to_python_environment(None, None);
let env = env.to_python_environment(None);
environments.insert(path.clone(), env.clone());
return Some(env);
}
Expand Down Expand Up @@ -301,7 +297,7 @@ impl Locator for Conda {
// The client can activate this env either using another conda manager or using the activation scripts
error!("Unable to find Conda Manager for the Conda env: {:?}", env);
let prefix = env.prefix.clone();
let env = env.to_python_environment(None, None);
let env = env.to_python_environment(None);
let mut environments = self.environments.lock().unwrap();
environments.insert(prefix, env.clone());
reporter.report_environment(&env);
Expand Down Expand Up @@ -340,7 +336,6 @@ impl Locator for Conda {
// 5. Report this env.
if let Some(manager) = manager {
let env = env.to_python_environment(
manager.conda_dir.clone(),
Some(manager.to_manager()),
);
let mut environments = self.environments.lock().unwrap();
Expand All @@ -352,7 +347,7 @@ impl Locator for Conda {
// This might seem incorrect, however the tool is about discovering environments.
// The client can activate this env either using another conda manager or using the activation scripts
error!("Unable to find Conda Manager for Conda env (even though we have a conda_dir {:?}): Env Details = {:?}", conda_dir, env);
let env = env.to_python_environment(Some(conda_dir.clone()), None);
let env = env.to_python_environment(None);
let mut environments = self.environments.lock().unwrap();
environments.insert(prefix.clone(), env.clone());
reporter.report_environment(&env);
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-conda/tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn find_conda_env_without_manager() {
assert_eq!(env.executable, path.join("bin").join("python").into());
assert_eq!(env.version, "3.12.2".to_string().into());
assert_eq!(env.manager, None);
assert_eq!(env.name, "env_python_3".to_string().into());
assert_eq!(env.name, None);
}

#[cfg(unix)]
Expand Down Expand Up @@ -90,5 +90,5 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() {
env.manager.clone().unwrap().version,
"23.1.0".to_string().into()
);
assert_eq!(env.name, None);
assert_eq!(env.name, "env_python_3".to_string().into());
}
Loading