@@ -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 ( known_paths : & impl known:: KnownPaths ) -> Option < PathBuf > {
131+ let paths = known_paths. 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,11 @@ 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 ( known_paths_provider : & impl known :: KnownPaths ) -> Vec < PathBuf > {
163+ let user_profile = known_paths_provider . get_env_var ( "USERPROFILE" . to_string ( ) ) . unwrap ( ) ;
164+ let program_data = known_paths_provider . get_env_var ( "PROGRAMDATA" . to_string ( ) ) . unwrap ( ) ;
165+ let all_user_profile = known_paths_provider . get_env_var ( "ALLUSERSPROFILE" . to_string ( ) ) . unwrap ( ) ;
166+ let home_drive = known_paths_provider . get_env_var ( "HOMEDRIVE" . to_string ( ) ) . unwrap ( ) ;
169167 let mut known_paths = vec ! [
170168 Path :: new( & user_profile) . join( "Anaconda3\\ Scripts" ) ,
171169 Path :: new( & program_data) . join( "Anaconda3\\ Scripts" ) ,
@@ -176,40 +174,42 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
176174 Path :: new( & all_user_profile) . join( "Miniconda3\\ Scripts" ) ,
177175 Path :: new( & home_drive) . join( "Miniconda3\\ Scripts" ) ,
178176 ] ;
179- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
177+ known_paths. append ( & mut known_paths_provider . get_know_global_search_locations ( ) ) ;
180178 known_paths
181179}
182180
183181#[ cfg( unix) ]
184- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
182+ fn get_known_conda_locations ( known_paths_provider : & impl known :: KnownPaths ) -> Vec < PathBuf > {
185183 let mut known_paths = vec ! [
186- Path :: new ( "/opt/anaconda3/bin" ) . to_path_buf ( ) ,
187- Path :: new ( "/opt/miniconda3/bin" ) . to_path_buf ( ) ,
188- Path :: new ( "/usr/local/anaconda3/bin" ) . to_path_buf ( ) ,
189- Path :: new ( "/usr/local/miniconda3/bin" ) . to_path_buf ( ) ,
190- Path :: new ( "/usr/anaconda3/bin" ) . to_path_buf ( ) ,
191- Path :: new ( "/usr/miniconda3/bin" ) . to_path_buf ( ) ,
192- Path :: new ( "/home/anaconda3/bin" ) . to_path_buf ( ) ,
193- Path :: new ( "/home/miniconda3/bin" ) . to_path_buf ( ) ,
194- Path :: new ( "/anaconda3/bin" ) . to_path_buf ( ) ,
195- Path :: new ( "/miniconda3/bin" ) . to_path_buf ( ) ,
196- Path :: new ( "/usr/local/anaconda3/bin" ) . to_path_buf ( ) ,
197- Path :: new ( "/usr/local/miniconda3/bin" ) . to_path_buf ( ) ,
198- Path :: new ( "/usr/anaconda3/bin" ) . to_path_buf ( ) ,
199- Path :: new ( "/usr/miniconda3/bin" ) . to_path_buf ( ) ,
200- Path :: new ( "/home/anaconda3/bin" ) . to_path_buf ( ) ,
201- Path :: new ( "/home/miniconda3/bin" ) . to_path_buf ( ) ,
202- Path :: new ( "/anaconda3/bin" ) . to_path_buf ( ) ,
203- Path :: new ( "/miniconda3/bin" ) . to_path_buf ( ) ,
184+ PathBuf :: from ( "/opt/anaconda3/bin" ) ,
185+ PathBuf :: from ( "/opt/miniconda3/bin" ) ,
186+ PathBuf :: from ( "/usr/local/anaconda3/bin" ) ,
187+ PathBuf :: from ( "/usr/local/miniconda3/bin" ) ,
188+ PathBuf :: from ( "/usr/anaconda3/bin" ) ,
189+ PathBuf :: from ( "/usr/miniconda3/bin" ) ,
190+ PathBuf :: from ( "/home/anaconda3/bin" ) ,
191+ PathBuf :: from ( "/home/miniconda3/bin" ) ,
192+ PathBuf :: from ( "/anaconda3/bin" ) ,
193+ PathBuf :: from ( "/miniconda3/bin" ) ,
194+ PathBuf :: from ( "/usr/local/anaconda3/bin" ) ,
195+ PathBuf :: from ( "/usr/local/miniconda3/bin" ) ,
196+ PathBuf :: from ( "/usr/anaconda3/bin" ) ,
197+ PathBuf :: from ( "/usr/miniconda3/bin" ) ,
198+ PathBuf :: from ( "/home/anaconda3/bin" ) ,
199+ PathBuf :: from ( "/home/miniconda3/bin" ) ,
200+ PathBuf :: from ( "/anaconda3/bin" ) ,
201+ PathBuf :: from ( "/miniconda3/bin" ) ,
204202 ] ;
205- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
203+ known_paths. append ( & mut known_paths_provider . get_know_global_search_locations ( ) ) ;
206204 known_paths
207205}
208206
209207/// Find conda binary in known locations
210- fn find_conda_binary_in_known_locations ( ) -> Option < PathBuf > {
208+ fn find_conda_binary_in_known_locations (
209+ known_paths_provider : & impl known:: KnownPaths ,
210+ ) -> 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 ( known_paths_provider ) ;
213213 for location in known_locations {
214214 for bin in & conda_bin_names {
215215 let conda_path = location. join ( bin) ;
@@ -223,17 +223,19 @@ 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 ( known_paths_provider : & impl known :: KnownPaths ) -> Option < PathBuf > {
227+ let conda_binary_on_path = find_conda_binary_on_path ( known_paths_provider ) ;
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 ( known_paths_provider ) ,
231231 }
232232}
233233
234- fn get_conda_envs_from_environment_txt ( ) -> Vec < String > {
234+ fn get_conda_envs_from_environment_txt (
235+ known_paths_provider : & impl known:: KnownPaths ,
236+ ) -> Vec < String > {
235237 let mut envs = vec ! [ ] ;
236- let home = known :: get_user_home ( ) ;
238+ let home = known_paths_provider . get_user_home ( ) ;
237239 match home {
238240 Some ( home) => {
239241 let home = Path :: new ( & home) ;
@@ -252,9 +254,12 @@ fn get_conda_envs_from_environment_txt() -> Vec<String> {
252254 envs
253255}
254256
255- fn get_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
257+ fn get_known_env_locations (
258+ conda_bin : PathBuf ,
259+ known_paths_provider : & impl known:: KnownPaths ,
260+ ) -> Vec < String > {
256261 let mut paths = vec ! [ ] ;
257- let home = known :: get_user_home ( ) ;
262+ let home = known_paths_provider . get_user_home ( ) ;
258263 match home {
259264 Some ( home) => {
260265 let home = Path :: new ( & home) ;
@@ -284,9 +289,12 @@ fn get_known_env_locations(conda_bin: PathBuf) -> Vec<String> {
284289 paths
285290}
286291
287- fn get_conda_envs_from_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
292+ fn get_conda_envs_from_known_env_locations (
293+ conda_bin : PathBuf ,
294+ known_paths_provider : & impl known:: KnownPaths ,
295+ ) -> Vec < String > {
288296 let mut envs = vec ! [ ] ;
289- for location in get_known_env_locations ( conda_bin) {
297+ for location in get_known_env_locations ( conda_bin, known_paths_provider ) {
290298 if is_conda_environment ( & Path :: new ( & location) ) {
291299 envs. push ( location. to_string ( ) ) ;
292300 }
@@ -324,16 +332,20 @@ struct CondaEnv {
324332 path : PathBuf ,
325333}
326334
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 ( ) ) ;
335+ fn get_distinct_conda_envs (
336+ conda_bin : PathBuf ,
337+ known_paths_provider : & impl known:: KnownPaths ,
338+ ) -> Vec < CondaEnv > {
339+ let mut envs = get_conda_envs_from_environment_txt ( known_paths_provider) ;
340+ let mut known_envs =
341+ get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) , known_paths_provider) ;
330342 envs. append ( & mut known_envs) ;
331343 envs. sort ( ) ;
332344 envs. dedup ( ) ;
333345
334- let locations = get_known_env_locations ( conda_bin) ;
346+ let locations = get_known_env_locations ( conda_bin, known_paths_provider ) ;
335347 let mut conda_envs = vec ! [ ] ;
336- for env in envs. clone ( ) {
348+ for env in envs {
337349 let env = Path :: new ( & env) ;
338350 let mut named = false ;
339351 let mut name = "" . to_string ( ) ;
@@ -367,16 +379,19 @@ fn get_distinct_conda_envs(conda_bin: PathBuf) -> Vec<CondaEnv> {
367379 conda_envs
368380}
369381
370- pub fn find_and_report ( ) {
371- let conda_binary = find_conda_binary ( ) ;
382+ pub fn find_and_report (
383+ dispatcher : & mut impl messaging:: MessageDispatcher ,
384+ known_paths_provider : & impl known:: KnownPaths ,
385+ ) {
386+ let conda_binary = find_conda_binary ( known_paths_provider) ;
372387 match conda_binary {
373388 Some ( conda_binary) => {
374389 let params =
375390 messaging:: EnvManager :: new ( vec ! [ conda_binary. to_string_lossy( ) . to_string( ) ] , None ) ;
376391 let message = messaging:: EnvManagerMessage :: new ( params) ;
377- messaging :: send_message ( message) ;
392+ dispatcher . send_message ( message) ;
378393
379- let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) ) ;
394+ let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) , known_paths_provider ) ;
380395 for env in envs {
381396 let executable = find_python_binary_path ( Path :: new ( & env. path ) ) ;
382397 let params = messaging:: PythonEnvironment :: new (
@@ -407,7 +422,7 @@ pub fn find_and_report() {
407422 Some ( env. path . to_string_lossy ( ) . to_string ( ) ) ,
408423 ) ;
409424 let message = messaging:: PythonEnvironmentMessage :: new ( params) ;
410- messaging :: send_message ( message) ;
425+ dispatcher . send_message ( message) ;
411426 }
412427 }
413428 None => ( ) ,
0 commit comments