33
44use log:: error;
55use pet_fs:: path:: norm_case;
6- use pet_python_utils:: executable:: get_shortest_executable;
76use serde:: { Deserialize , Serialize } ;
87use std:: path:: PathBuf ;
98
@@ -317,8 +316,10 @@ impl PythonEnvironmentBuilder {
317316 Some ( all. clone ( ) )
318317 } ;
319318 if let Some ( executable) = & self . executable {
320- self . executable =
321- Some ( get_shortest_executable ( & Some ( all. clone ( ) ) ) . unwrap_or ( executable. clone ( ) ) ) ;
319+ self . executable = Some (
320+ get_shortest_executable ( & self . category , & Some ( all. clone ( ) ) )
321+ . unwrap_or ( executable. clone ( ) ) ,
322+ ) ;
322323 }
323324 }
324325
@@ -338,9 +339,9 @@ impl PythonEnvironmentBuilder {
338339 } else {
339340 Some ( all. clone ( ) )
340341 } ;
341- let executable = self
342- . executable
343- . map ( |executable| get_shortest_executable ( & Some ( all . clone ( ) ) ) . unwrap_or ( executable ) ) ;
342+ let executable = self . executable . map ( |executable| {
343+ get_shortest_executable ( & self . category , & Some ( all . clone ( ) ) ) . unwrap_or ( executable)
344+ } ) ;
344345
345346 PythonEnvironment {
346347 display_name : self . display_name ,
@@ -358,6 +359,43 @@ impl PythonEnvironmentBuilder {
358359 }
359360}
360361
362+ // Given a list of executables, return the one with the shortest path.
363+ // The shortest path is the most likely to be most user friendly.
364+ fn get_shortest_executable (
365+ category : & PythonEnvironmentCategory ,
366+ exes : & Option < Vec < PathBuf > > ,
367+ ) -> Option < PathBuf > {
368+ // For windows store, the exe should always be the one in the WindowsApps folder.
369+ if * category == PythonEnvironmentCategory :: WindowsStore {
370+ if let Some ( exes) = exes {
371+ if let Some ( exe) = exes. iter ( ) . find ( |e| {
372+ e. to_string_lossy ( ) . contains ( "AppData" )
373+ && e. to_string_lossy ( ) . contains ( "Local" )
374+ && e. to_string_lossy ( ) . contains ( "Microsoft" )
375+ && e. to_string_lossy ( ) . contains ( "WindowsApps" )
376+ } ) {
377+ return Some ( exe. clone ( ) ) ;
378+ }
379+ }
380+ }
381+
382+ // Ensure the executable always points to the shorted path.
383+ if let Some ( mut exes) = exes. clone ( ) {
384+ exes. sort_by ( |a, b| {
385+ a. to_str ( )
386+ . unwrap_or_default ( )
387+ . len ( )
388+ . cmp ( & b. to_str ( ) . unwrap_or_default ( ) . len ( ) )
389+ } ) ;
390+ if exes. is_empty ( ) {
391+ return None ;
392+ }
393+ Some ( exes[ 0 ] . clone ( ) )
394+ } else {
395+ None
396+ }
397+ }
398+
361399pub fn get_environment_key ( env : & PythonEnvironment ) -> Option < PathBuf > {
362400 if let Some ( exe) = & env. executable {
363401 Some ( exe. clone ( ) )
0 commit comments