Skip to content

Commit 9b9f66f

Browse files
authored
Add rpc method to get conda info (#106)
1 parent 5ede542 commit 9b9f66f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

crates/pet-conda/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ use pet_core::{
1414
Locator, LocatorResult,
1515
};
1616
use pet_python_utils::env::PythonEnv;
17+
use serde::{Deserialize, Serialize};
1718
use std::{
1819
collections::HashMap,
1920
path::{Path, PathBuf},
2021
sync::{Arc, Mutex},
2122
thread,
2223
};
23-
use telemetry::report_missing_envs;
24+
use telemetry::{get_conda_rcs_and_env_dirs, report_missing_envs};
2425
use utils::{is_conda_env, is_conda_install};
2526

2627
mod conda_info;
@@ -40,7 +41,17 @@ pub trait CondaLocator: Send + Sync {
4041
reporter: &dyn Reporter,
4142
conda_executable: Option<PathBuf>,
4243
) -> Option<()>;
44+
fn get_info_for_telemetry(&self, conda_executable: Option<PathBuf>) -> CondaTelemetryInfo;
4345
}
46+
47+
#[derive(Debug, Serialize, Deserialize)]
48+
#[serde(rename_all = "camelCase")]
49+
pub struct CondaTelemetryInfo {
50+
pub can_spawn_conda: bool,
51+
pub conda_rcs: Vec<PathBuf>,
52+
pub env_dirs: Vec<PathBuf>,
53+
}
54+
4455
pub struct Conda {
4556
/// Directories where conda environments are found (env_dirs returned from `conda info --json`)
4657
pub env_dirs: Arc<Mutex<Vec<PathBuf>>>,
@@ -108,6 +119,20 @@ impl CondaLocator for Conda {
108119
Some(())
109120
}
110121

122+
fn get_info_for_telemetry(&self, conda_executable: Option<PathBuf>) -> CondaTelemetryInfo {
123+
let can_spawn_conda = CondaInfo::from(conda_executable).is_some();
124+
let environments = self.environments.lock().unwrap().clone();
125+
let environments = environments
126+
.into_values()
127+
.collect::<Vec<PythonEnvironment>>();
128+
let (conda_rcs, env_dirs) = get_conda_rcs_and_env_dirs(&self.env_vars, &environments);
129+
CondaTelemetryInfo {
130+
can_spawn_conda,
131+
conda_rcs,
132+
env_dirs,
133+
}
134+
}
135+
111136
fn find_in(&self, conda_dir: &Path) -> Option<LocatorResult> {
112137
if !is_conda_install(conda_dir) {
113138
return None;

crates/pet-conda/src/telemetry.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ pub fn report_missing_envs(
188188
Some(())
189189
}
190190

191+
pub fn get_conda_rcs_and_env_dirs(
192+
env_vars: &EnvVariables,
193+
known_envs: &[PythonEnvironment],
194+
) -> (Vec<PathBuf>, Vec<PathBuf>) {
195+
let known_conda_rcs = get_all_known_conda_rc(env_vars, known_envs);
196+
let discovered_conda_rcs = known_conda_rcs
197+
.iter()
198+
.flat_map(|rc| rc.files.clone().into_iter())
199+
.collect();
200+
let discovered_env_dirs = known_conda_rcs
201+
.iter()
202+
.flat_map(|rc| rc.env_dirs.clone().into_iter())
203+
.collect();
204+
205+
(discovered_conda_rcs, discovered_env_dirs)
206+
}
207+
191208
fn log_and_find_missing_envs(
192209
possibly_missing_envs: &[PathBuf],
193210
known_envs: &[PythonEnvironment],

crates/pet/src/jsonrpc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn start_jsonrpc_server() {
6060
handlers.add_request_handler("configure", handle_configure);
6161
handlers.add_request_handler("refresh", handle_refresh);
6262
handlers.add_request_handler("resolve", handle_resolve);
63+
handlers.add_request_handler("condaInfo", handle_conda_telemetry);
6364
start_server(&handlers)
6465
}
6566

@@ -249,3 +250,17 @@ pub fn handle_resolve(context: Arc<Context>, id: u32, params: Value) {
249250
}
250251
}
251252
}
253+
254+
pub fn handle_conda_telemetry(context: Arc<Context>, id: u32, _params: Value) {
255+
thread::spawn(move || {
256+
let conda_locator = context.conda_locator.clone();
257+
let conda_executable = context
258+
.configuration
259+
.read()
260+
.unwrap()
261+
.conda_executable
262+
.clone();
263+
let info = conda_locator.get_info_for_telemetry(conda_executable);
264+
send_reply(id, info.into());
265+
});
266+
}

0 commit comments

Comments
 (0)