@@ -84,7 +84,6 @@ fn get_version_from_meta_json(json_file: &Path) -> Option<String> {
8484fn get_conda_package_json_path ( any_path : & Path , package : & str ) -> Option < PathBuf > {
8585 let package_name = format ! ( "{}-" , package) ;
8686 let conda_meta_path = get_conda_meta_path ( any_path) ?;
87-
8887 std:: fs:: read_dir ( conda_meta_path) . ok ( ) ?. find_map ( |entry| {
8988 let path = entry. ok ( ) ?. path ( ) ;
9089 let file_name = path. file_name ( ) ?. to_string_lossy ( ) ;
@@ -97,6 +96,7 @@ fn get_conda_package_json_path(any_path: &Path, package: &str) -> Option<PathBuf
9796}
9897
9998/// Checks if the `python` package is installed in the conda environment
99+ #[ allow( dead_code) ]
100100pub fn is_python_conda_env ( any_path : & Path ) -> bool {
101101 let conda_python_json_path = get_conda_package_json_path ( any_path, "python" ) ;
102102 match conda_python_json_path {
@@ -127,11 +127,9 @@ fn get_conda_bin_names() -> Vec<&'static str> {
127127}
128128
129129/// Find the conda binary on the PATH environment variable
130- fn find_conda_binary_on_path ( ) -> Option < PathBuf > {
131- let paths = env:: var ( "PATH" ) . ok ( ) ?;
132- let paths = env:: split_paths ( & paths) ;
133- for path in paths {
134- let path = Path :: new ( & path) ;
130+ fn find_conda_binary_on_path ( environment : & impl known:: Environment ) -> Option < PathBuf > {
131+ let paths = environment. get_env_var ( "PATH" . to_string ( ) ) ?;
132+ for path in env:: split_paths ( & paths) {
135133 for bin in get_conda_bin_names ( ) {
136134 let conda_path = path. join ( bin) ;
137135 match std:: fs:: metadata ( & conda_path) {
@@ -161,11 +159,13 @@ fn find_python_binary_path(env_path: &Path) -> Option<PathBuf> {
161159}
162160
163161#[ cfg( windows) ]
164- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
165- let user_profile = env:: var ( "USERPROFILE" ) . unwrap ( ) ;
166- let program_data = env:: var ( "PROGRAMDATA" ) . unwrap ( ) ;
167- let all_user_profile = env:: var ( "ALLUSERSPROFILE" ) . unwrap ( ) ;
168- let home_drive = env:: var ( "HOMEDRIVE" ) . unwrap ( ) ;
162+ fn get_known_conda_locations ( environment : & impl known:: Environment ) -> Vec < PathBuf > {
163+ let user_profile = environment. get_env_var ( "USERPROFILE" . to_string ( ) ) . unwrap ( ) ;
164+ let program_data = environment. get_env_var ( "PROGRAMDATA" . to_string ( ) ) . unwrap ( ) ;
165+ let all_user_profile = environment
166+ . get_env_var ( "ALLUSERSPROFILE" . to_string ( ) )
167+ . unwrap ( ) ;
168+ let home_drive = environment. get_env_var ( "HOMEDRIVE" . to_string ( ) ) . unwrap ( ) ;
169169 let mut known_paths = vec ! [
170170 Path :: new( & user_profile) . join( "Anaconda3\\ Scripts" ) ,
171171 Path :: new( & program_data) . join( "Anaconda3\\ Scripts" ) ,
@@ -176,12 +176,12 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
176176 Path :: new( & all_user_profile) . join( "Miniconda3\\ Scripts" ) ,
177177 Path :: new( & home_drive) . join( "Miniconda3\\ Scripts" ) ,
178178 ] ;
179- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
179+ known_paths. append ( & mut environment . get_know_global_search_locations ( ) ) ;
180180 known_paths
181181}
182182
183183#[ cfg( unix) ]
184- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
184+ fn get_known_conda_locations ( environment : & impl known :: Environment ) -> Vec < PathBuf > {
185185 let mut known_paths = vec ! [
186186 PathBuf :: from( "/opt/anaconda3/bin" ) ,
187187 PathBuf :: from( "/opt/miniconda3/bin" ) ,
@@ -202,14 +202,14 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
202202 PathBuf :: from( "/anaconda3/bin" ) ,
203203 PathBuf :: from( "/miniconda3/bin" ) ,
204204 ] ;
205- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
205+ known_paths. append ( & mut environment . get_know_global_search_locations ( ) ) ;
206206 known_paths
207207}
208208
209209/// Find conda binary in known locations
210- fn find_conda_binary_in_known_locations ( ) -> Option < PathBuf > {
210+ fn find_conda_binary_in_known_locations ( environment : & impl known :: Environment ) -> Option < PathBuf > {
211211 let conda_bin_names = get_conda_bin_names ( ) ;
212- let known_locations = get_known_conda_locations ( ) ;
212+ let known_locations = get_known_conda_locations ( environment ) ;
213213 for location in known_locations {
214214 for bin in & conda_bin_names {
215215 let conda_path = location. join ( bin) ;
@@ -223,17 +223,17 @@ fn find_conda_binary_in_known_locations() -> Option<PathBuf> {
223223}
224224
225225/// Find the conda binary on the system
226- pub fn find_conda_binary ( ) -> Option < PathBuf > {
227- let conda_binary_on_path = find_conda_binary_on_path ( ) ;
226+ pub fn find_conda_binary ( environment : & impl known :: Environment ) -> Option < PathBuf > {
227+ let conda_binary_on_path = find_conda_binary_on_path ( environment ) ;
228228 match conda_binary_on_path {
229229 Some ( conda_binary_on_path) => Some ( conda_binary_on_path) ,
230- None => find_conda_binary_in_known_locations ( ) ,
230+ None => find_conda_binary_in_known_locations ( environment ) ,
231231 }
232232}
233233
234- fn get_conda_envs_from_environment_txt ( ) -> Vec < String > {
234+ fn get_conda_envs_from_environment_txt ( environment : & impl known :: Environment ) -> Vec < String > {
235235 let mut envs = vec ! [ ] ;
236- let home = known :: get_user_home ( ) ;
236+ let home = environment . get_user_home ( ) ;
237237 match home {
238238 Some ( home) => {
239239 let home = Path :: new ( & home) ;
@@ -252,9 +252,12 @@ fn get_conda_envs_from_environment_txt() -> Vec<String> {
252252 envs
253253}
254254
255- fn get_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
255+ fn get_known_env_locations (
256+ conda_bin : PathBuf ,
257+ environment : & impl known:: Environment ,
258+ ) -> Vec < String > {
256259 let mut paths = vec ! [ ] ;
257- let home = known :: get_user_home ( ) ;
260+ let home = environment . get_user_home ( ) ;
258261 match home {
259262 Some ( home) => {
260263 let home = Path :: new ( & home) ;
@@ -284,9 +287,12 @@ fn get_known_env_locations(conda_bin: PathBuf) -> Vec<String> {
284287 paths
285288}
286289
287- fn get_conda_envs_from_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
290+ fn get_conda_envs_from_known_env_locations (
291+ conda_bin : PathBuf ,
292+ environment : & impl known:: Environment ,
293+ ) -> Vec < String > {
288294 let mut envs = vec ! [ ] ;
289- for location in get_known_env_locations ( conda_bin) {
295+ for location in get_known_env_locations ( conda_bin, environment ) {
290296 if is_conda_environment ( & Path :: new ( & location) ) {
291297 envs. push ( location. to_string ( ) ) ;
292298 }
@@ -324,14 +330,18 @@ struct CondaEnv {
324330 path : PathBuf ,
325331}
326332
327- fn get_distinct_conda_envs ( conda_bin : PathBuf ) -> Vec < CondaEnv > {
328- let mut envs = get_conda_envs_from_environment_txt ( ) ;
329- let mut known_envs = get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) ) ;
333+ fn get_distinct_conda_envs (
334+ conda_bin : PathBuf ,
335+ environment : & impl known:: Environment ,
336+ ) -> Vec < CondaEnv > {
337+ let mut envs = get_conda_envs_from_environment_txt ( environment) ;
338+ let mut known_envs =
339+ get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) , environment) ;
330340 envs. append ( & mut known_envs) ;
331341 envs. sort ( ) ;
332342 envs. dedup ( ) ;
333343
334- let locations = get_known_env_locations ( conda_bin) ;
344+ let locations = get_known_env_locations ( conda_bin, environment ) ;
335345 let mut conda_envs = vec ! [ ] ;
336346 for env in envs {
337347 let env = Path :: new ( & env) ;
@@ -367,16 +377,19 @@ fn get_distinct_conda_envs(conda_bin: PathBuf) -> Vec<CondaEnv> {
367377 conda_envs
368378}
369379
370- pub fn find_and_report ( ) {
371- let conda_binary = find_conda_binary ( ) ;
380+ pub fn find_and_report (
381+ dispatcher : & mut impl messaging:: MessageDispatcher ,
382+ environment : & impl known:: Environment ,
383+ ) {
384+ let conda_binary = find_conda_binary ( environment) ;
372385 match conda_binary {
373386 Some ( conda_binary) => {
374387 let params =
375388 messaging:: EnvManager :: new ( vec ! [ conda_binary. to_string_lossy( ) . to_string( ) ] , None ) ;
376389 let message = messaging:: EnvManagerMessage :: new ( params) ;
377- messaging :: send_message ( message) ;
390+ dispatcher . send_message ( message) ;
378391
379- let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) ) ;
392+ let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) , environment ) ;
380393 for env in envs {
381394 let executable = find_python_binary_path ( Path :: new ( & env. path ) ) ;
382395 let params = messaging:: PythonEnvironment :: new (
@@ -407,7 +420,7 @@ pub fn find_and_report() {
407420 Some ( env. path . to_string_lossy ( ) . to_string ( ) ) ,
408421 ) ;
409422 let message = messaging:: PythonEnvironmentMessage :: new ( params) ;
410- messaging :: send_message ( message) ;
423+ dispatcher . send_message ( message) ;
411424 }
412425 }
413426 None => ( ) ,
0 commit comments