Skip to content

Add rpc method to get conda info #106

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

Merged
merged 1 commit into from
Jul 11, 2024
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
27 changes: 26 additions & 1 deletion crates/pet-conda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use pet_core::{
Locator, LocatorResult,
};
use pet_python_utils::env::PythonEnv;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{Arc, Mutex},
thread,
};
use telemetry::report_missing_envs;
use telemetry::{get_conda_rcs_and_env_dirs, report_missing_envs};
use utils::{is_conda_env, is_conda_install};

mod conda_info;
Expand All @@ -40,7 +41,17 @@ pub trait CondaLocator: Send + Sync {
reporter: &dyn Reporter,
conda_executable: Option<PathBuf>,
) -> Option<()>;
fn get_info_for_telemetry(&self, conda_executable: Option<PathBuf>) -> CondaTelemetryInfo;
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CondaTelemetryInfo {
pub can_spawn_conda: bool,
pub conda_rcs: Vec<PathBuf>,
pub env_dirs: Vec<PathBuf>,
}

pub struct Conda {
/// Directories where conda environments are found (env_dirs returned from `conda info --json`)
pub env_dirs: Arc<Mutex<Vec<PathBuf>>>,
Expand Down Expand Up @@ -108,6 +119,20 @@ impl CondaLocator for Conda {
Some(())
}

fn get_info_for_telemetry(&self, conda_executable: Option<PathBuf>) -> CondaTelemetryInfo {
let can_spawn_conda = CondaInfo::from(conda_executable).is_some();
let environments = self.environments.lock().unwrap().clone();
let environments = environments
.into_values()
.collect::<Vec<PythonEnvironment>>();
let (conda_rcs, env_dirs) = get_conda_rcs_and_env_dirs(&self.env_vars, &environments);
CondaTelemetryInfo {
can_spawn_conda,
conda_rcs,
env_dirs,
}
}

fn find_in(&self, conda_dir: &Path) -> Option<LocatorResult> {
if !is_conda_install(conda_dir) {
return None;
Expand Down
17 changes: 17 additions & 0 deletions crates/pet-conda/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ pub fn report_missing_envs(
Some(())
}

pub fn get_conda_rcs_and_env_dirs(
env_vars: &EnvVariables,
known_envs: &[PythonEnvironment],
) -> (Vec<PathBuf>, Vec<PathBuf>) {
let known_conda_rcs = get_all_known_conda_rc(env_vars, known_envs);
let discovered_conda_rcs = known_conda_rcs
.iter()
.flat_map(|rc| rc.files.clone().into_iter())
.collect();
let discovered_env_dirs = known_conda_rcs
.iter()
.flat_map(|rc| rc.env_dirs.clone().into_iter())
.collect();

(discovered_conda_rcs, discovered_env_dirs)
}

fn log_and_find_missing_envs(
possibly_missing_envs: &[PathBuf],
known_envs: &[PythonEnvironment],
Expand Down
15 changes: 15 additions & 0 deletions crates/pet/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn start_jsonrpc_server() {
handlers.add_request_handler("configure", handle_configure);
handlers.add_request_handler("refresh", handle_refresh);
handlers.add_request_handler("resolve", handle_resolve);
handlers.add_request_handler("condaInfo", handle_conda_telemetry);
start_server(&handlers)
}

Expand Down Expand Up @@ -249,3 +250,17 @@ pub fn handle_resolve(context: Arc<Context>, id: u32, params: Value) {
}
}
}

pub fn handle_conda_telemetry(context: Arc<Context>, id: u32, _params: Value) {
thread::spawn(move || {
let conda_locator = context.conda_locator.clone();
let conda_executable = context
.configuration
.read()
.unwrap()
.conda_executable
.clone();
let info = conda_locator.get_info_for_telemetry(conda_executable);
send_reply(id, info.into());
});
}
Loading