Skip to content

Commit

Permalink
improve robustness in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Mar 18, 2024
1 parent 9150b9d commit e0db1df
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/io/bioimage/modelrunner/apposed/appose/Mamba.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ public void runPythonIn( final String envName, final String... args ) throws IOE
if ( envName.equals( DEFAULT_ENVIRONMENT_NAME ) )
cmd.add( PYTHON_COMMAND );
else
cmd.add( Paths.get( ENVS_NAME, envName, PYTHON_COMMAND ).toString() );
cmd.add( checkExecutablePath(Paths.get( ENVS_NAME, envName, PYTHON_COMMAND ).toString()) );
cmd.addAll( Arrays.asList( args ) );
final ProcessBuilder builder = getBuilder( true );
if ( PlatformDetection.isWindows() )
Expand Down Expand Up @@ -999,7 +999,7 @@ public static void runPythonIn( final File envFile, final String... args ) throw
throw new IOException("No Python found in the environment provided. The following "
+ "file does not exist: " + Paths.get( envFile.getAbsolutePath(), PYTHON_COMMAND ).toAbsolutePath());
final List< String > cmd = getBaseCommand();
cmd.add( Paths.get( envFile.getAbsolutePath(), PYTHON_COMMAND ).toAbsolutePath().toString() );
cmd.add( checkExecutablePath(Paths.get( envFile.getAbsolutePath(), PYTHON_COMMAND ).toAbsolutePath().toString()) );
cmd.addAll( Arrays.asList( args ) );
final ProcessBuilder builder = new ProcessBuilder().directory( envFile );
builder.inheritIO();
Expand Down Expand Up @@ -1032,7 +1032,7 @@ public static void runPythonIn( final File envFile, final String... args ) throw
public String getVersion() throws IOException, InterruptedException, MambaInstallException
{
final List< String > cmd = getBaseCommand();
cmd.addAll( Arrays.asList( mambaCommand, "--version" ) );
cmd.addAll( Arrays.asList( checkExecutablePath(mambaCommand), "--version" ) );
final Process process = getBuilder( false ).command( cmd ).start();
if ( process.waitFor() != 0 )
throw new RuntimeException();
Expand Down Expand Up @@ -1065,7 +1065,7 @@ public void runMamba(boolean isInheritIO, final String... args ) throws RuntimeE
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

final List< String > cmd = getBaseCommand();
cmd.add( mambaCommand );
cmd.add( checkExecutablePath(mambaCommand) );
cmd.addAll( Arrays.asList( args ) );

ProcessBuilder builder = getBuilder(isInheritIO).command(cmd);
Expand Down Expand Up @@ -1562,5 +1562,22 @@ public boolean checkEnvFromYamlExists(String envYaml) throws MambaInstallExcepti
}
return false;
}

/**
* In Windows, if the path to the wanted executable contains spaces, it is surrounded by
* double quotes
* @param path
* path to the wanted executable
* @return a robust executable path
*/
private static String checkExecutablePath(String path) {
String[] specialChars = new String[] {" "};
for (String schar : specialChars) {
if (path.contains(schar) && PlatformDetection.isWindows()) {
return "\"" + path + "\"";
}
}
return path;
}

}

0 comments on commit e0db1df

Please sign in to comment.