Skip to content

Commit

Permalink
improve robustness for when there are spaces in the path
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Mar 18, 2024
1 parent d7e169a commit 04d80b9
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/main/java/io/bioimage/modelrunner/apposed/appose/Mamba.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,11 +956,17 @@ public void runPythonIn( final String envName, final String... args ) throws IOE
checkMambaInstalled();
if (!installed) throw new MambaInstallException("Micromamba is not installed");
final List< String > cmd = getBaseCommand();
List<String> argsList = new ArrayList<String>();
if ( envName.equals( DEFAULT_ENVIRONMENT_NAME ) )
cmd.add( PYTHON_COMMAND );
argsList.add( PYTHON_COMMAND );
else
cmd.add( checkExecutablePath(Paths.get( ENVS_NAME, envName, PYTHON_COMMAND ).toString()) );
cmd.addAll( Arrays.asList( args ) );
argsList.add( checkExecutablePath(Paths.get( ENVS_NAME, envName, PYTHON_COMMAND ).toString()) );
argsList.addAll( Arrays.asList( args ) );
boolean containsSpaces = argsList.stream().filter(aa -> aa.contains(" ")).collect(Collectors.toList()).size() > 0;

if (!containsSpaces || !PlatformDetection.isWindows()) cmd.addAll(argsList);
else cmd.add(surroundWithQuotes(argsList));

final ProcessBuilder builder = getBuilder( true );
if ( PlatformDetection.isWindows() )
{
Expand Down Expand Up @@ -999,8 +1005,15 @@ 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( checkExecutablePath(Paths.get( envFile.getAbsolutePath(), PYTHON_COMMAND ).toAbsolutePath().toString()) );
cmd.addAll( Arrays.asList( args ) );
List<String> argsList = new ArrayList<String>();
argsList.add( checkExecutablePath(Paths.get( envFile.getAbsolutePath(), PYTHON_COMMAND ).toAbsolutePath().toString()) );
argsList.addAll( Arrays.asList( args ) );
boolean containsSpaces = argsList.stream().filter(aa -> aa.contains(" ")).collect(Collectors.toList()).size() > 0;

if (!containsSpaces || !PlatformDetection.isWindows()) cmd.addAll(argsList);
else cmd.add(surroundWithQuotes(argsList));


final ProcessBuilder builder = new ProcessBuilder().directory( envFile );
builder.inheritIO();
if ( PlatformDetection.isWindows() )
Expand Down Expand Up @@ -1032,7 +1045,10 @@ 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( checkExecutablePath(mambaCommand), "--version" ) );
if (mambaCommand.contains(" ") && PlatformDetection.isWindows())
cmd.add( surroundWithQuotes(Arrays.asList( checkExecutablePath(mambaCommand), "--version" )) );
else
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,9 +1081,14 @@ public void runMamba(boolean isInheritIO, final String... args ) throws RuntimeE
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

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

List<String> argsList = new ArrayList<String>();
argsList.add( checkExecutablePath(mambaCommand) );
argsList.addAll( Arrays.asList( args ) );
boolean containsSpaces = argsList.stream().filter(aa -> aa.contains(" ")).collect(Collectors.toList()).size() > 0;

if (!containsSpaces || !PlatformDetection.isWindows()) cmd.addAll(argsList);
else cmd.add(surroundWithQuotes(argsList));

ProcessBuilder builder = getBuilder(isInheritIO).command(cmd);
Process process = builder.start();
// Use separate threads to read each stream to avoid a deadlock.
Expand Down Expand Up @@ -1581,5 +1602,22 @@ private static String checkExecutablePath(String path) {
}
return path;
}

/**
* When an argument of a command prompt argument in Windows contains an space, not
* only the argument needs to be surrounded by double quotes, but the whole sentence
* @param args
* arguments to be executed by the windows cmd
* @return a complete Sting containing all the arguments and surrounded by double quotes
*/
private static String surroundWithQuotes(List<String> args) {
String arg = "\"";
for (String aa : args) {
arg += aa + " ";
}
arg = arg.substring(0, arg.length() - 1);
arg += "\"";
return arg;
}

}

0 comments on commit 04d80b9

Please sign in to comment.