Skip to content

Commit dc6bde9

Browse files
authored
Remove duplicate environments (#42)
1 parent 8923417 commit dc6bde9

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

crates/pet-reporter/src/cache.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use crate::environment::get_environment_key;
5+
use pet_core::{manager::EnvManager, python_environment::PythonEnvironment, reporter::Reporter};
6+
use std::{
7+
collections::HashMap,
8+
path::PathBuf,
9+
sync::{Arc, Mutex},
10+
};
11+
12+
pub struct CacheReporter {
13+
reporter: Arc<dyn Reporter>,
14+
reported_managers: Arc<Mutex<HashMap<PathBuf, EnvManager>>>,
15+
reported_environments: Arc<Mutex<HashMap<PathBuf, PythonEnvironment>>>,
16+
}
17+
18+
impl CacheReporter {
19+
pub fn new(reporter: Arc<dyn Reporter>) -> Self {
20+
Self {
21+
reporter,
22+
reported_managers: Arc::new(Mutex::new(HashMap::new())),
23+
reported_environments: Arc::new(Mutex::new(HashMap::new())),
24+
}
25+
}
26+
}
27+
impl Reporter for CacheReporter {
28+
fn report_manager(&self, manager: &EnvManager) {
29+
let mut reported_managers = self.reported_managers.lock().unwrap();
30+
if !reported_managers.contains_key(&manager.executable) {
31+
reported_managers.insert(manager.executable.clone(), manager.clone());
32+
self.reporter.report_manager(manager);
33+
}
34+
}
35+
36+
fn report_environment(&self, env: &PythonEnvironment) {
37+
if let Some(key) = get_environment_key(env) {
38+
let mut reported_environments = self.reported_environments.lock().unwrap();
39+
if !reported_environments.contains_key(&key) {
40+
reported_environments.insert(key.clone(), env.clone());
41+
self.reporter.report_environment(env);
42+
}
43+
}
44+
}
45+
}

crates/pet-reporter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
pub mod cache;
45
pub mod environment;
56
pub mod jsonrpc;
67
pub mod manager;

crates/pet/src/jsonrpc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use pet_jsonrpc::{
1212
send_error, send_reply,
1313
server::{start_server, HandlersKeyedByMethodName},
1414
};
15-
use pet_reporter::{environment::Environment, jsonrpc};
15+
use pet_reporter::{cache::CacheReporter, environment::Environment, jsonrpc};
1616
use pet_telemetry::report_inaccuracies_identified_after_resolving;
1717
use serde::{Deserialize, Serialize};
1818
use serde_json::{self, Value};
@@ -39,9 +39,10 @@ pub fn start_jsonrpc_server() {
3939
// Hence passed around as Arcs via the context.
4040
let environment = EnvironmentApi::new();
4141
let jsonrpc_reporter = Arc::new(jsonrpc::create_reporter());
42+
let reporter = Arc::new(CacheReporter::new(jsonrpc_reporter.clone()));
4243
let conda_locator = Arc::new(Conda::from(&environment));
4344
let context = Context {
44-
reporter: jsonrpc_reporter,
45+
reporter,
4546
locators: create_locators(conda_locator.clone()),
4647
conda_locator,
4748
configuration: RwLock::new(Configuration::default()),

crates/pet/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use find::find_and_report_envs;
55
use locators::create_locators;
66
use pet_conda::Conda;
77
use pet_core::{os_environment::EnvironmentApi, Configuration};
8-
use pet_reporter::{self, stdio};
8+
use pet_reporter::{self, cache::CacheReporter, stdio};
99
use std::{collections::BTreeMap, env, sync::Arc, time::SystemTime};
1010

1111
pub mod find;
@@ -16,7 +16,8 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool) {
1616
stdio::initialize_logger(log::LevelFilter::Info);
1717
let now = SystemTime::now();
1818

19-
let reporter = stdio::create_reporter(print_list);
19+
let stdio_reporter = Arc::new(stdio::create_reporter(print_list));
20+
let reporter = CacheReporter::new(stdio_reporter.clone());
2021
let environment = EnvironmentApi::new();
2122
let conda_locator = Arc::new(Conda::from(&environment));
2223

@@ -33,7 +34,7 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool) {
3334
);
3435

3536
if print_summary {
36-
let summary = reporter.get_summary();
37+
let summary = stdio_reporter.get_summary();
3738
if !summary.managers.is_empty() {
3839
println!("Managers:");
3940
println!("---------");

0 commit comments

Comments
 (0)