From 70b0f57e61d34aa7147e703935e32b8c7b15ee0e Mon Sep 17 00:00:00 2001 From: Jean-Yves Tinevez Date: Thu, 27 Jun 2024 20:51:56 +0200 Subject: [PATCH] Print a message after the user configured the path to Conda. --- .../plugin/trackmate/util/cli/CLIUtils.java | 74 ++++++++++--------- .../util/cli/CondaCLIConfigurator.java | 22 ++++-- .../util/cli/CondaPathConfigCommand.java | 28 ++++++- 3 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java b/src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java index 03ba58f4e..f6c1adf7c 100644 --- a/src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java +++ b/src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java @@ -8,12 +8,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . @@ -33,6 +33,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -52,50 +53,51 @@ public class CLIUtils public static final String CONDA_PATH_PREF_KEY = "trackmate.conda.path"; - public static Map< String, String > getEnvMap() + public static Map< String, String > getEnvMap() throws IOException { // Create a map to store the environment names and paths final Map< String, String > envMap = new HashMap<>(); - try + final ProcessBuilder pb; + if ( IJ.isWindows() ) + pb = new ProcessBuilder( Arrays.asList( "cmd.exe", "/c", "conda", "env", "list" ) ); + else + pb = new ProcessBuilder( Arrays.asList( getCondaPath(), "env", "list" ) ); + final Process process = pb.start(); + final BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ); + + // Read each line of output and extract the environment name and + // path + String line; + while ( ( line = reader.readLine() ) != null ) { - final ProcessBuilder pb; - if ( IJ.isWindows() ) - pb = new ProcessBuilder( Arrays.asList( "cmd.exe", "/c", "conda", "env", "list" ) ); - else - pb = new ProcessBuilder( Arrays.asList( getCondaPath(), "env", "list" ) ); - final Process process = pb.start(); - final BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ); - - // Read each line of output and extract the environment name and - // path - String line; - while ( ( line = reader.readLine() ) != null ) - { - line = line.trim(); - if ( line.isEmpty() || line.startsWith( "#" ) || line.startsWith( "Name" ) ) - continue; + line = line.trim(); + if ( line.isEmpty() || line.startsWith( "#" ) || line.startsWith( "Name" ) ) + continue; - final String[] parts = line.split( "\\s+" ); - if ( parts.length >= 2 ) - { - final String envName = parts[ 0 ]; - final String envPath = parts[ 1 ] + "/bin/python"; - envMap.put( envName, envPath ); - } + final String[] parts = line.split( "\\s+" ); + if ( parts.length >= 2 ) + { + final String envName = parts[ 0 ]; + final String envPath = parts[ 1 ] + "/bin/python"; + envMap.put( envName, envPath ); } } - catch ( final IOException e ) - { - e.printStackTrace(); - } return envMap; } public static List< String > getEnvList() { - final List< String > l = new ArrayList<>( getEnvMap().keySet() ); - l.sort( null ); - return l; + try + { + final List< String > l = new ArrayList<>( getEnvMap().keySet() ); + l.sort( null ); + return l; + } + catch ( final IOException e ) + { + e.printStackTrace(); + } + return Collections.emptyList(); } public static String getCondaPath() @@ -158,7 +160,7 @@ public static String findDefaultCondaPath() throws IllegalArgumentException /** * Add a hook to delete the content of given path when Fiji quits. Taken * from https://stackoverflow.com/a/20280989/201698 - * + * * @param path */ public static void recursiveDeleteOnShutdownHook( final Path path ) @@ -244,7 +246,7 @@ else if ( !line.trim().isEmpty() ) } } - public static void main( final String[] args ) + public static void main( final String[] args ) throws IOException { System.out.println( "Conda path: " + findDefaultCondaPath() ); System.out.println( "Known environments: " + getEnvList() ); diff --git a/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java b/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java index 094a110e8..e8a20114d 100644 --- a/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java +++ b/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java @@ -8,12 +8,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . @@ -21,6 +21,7 @@ */ package fiji.plugin.trackmate.util.cli; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -121,13 +122,22 @@ protected CondaCLIConfigurator() * retrieve what is the path of the Python executable of this * env, and runs the tool as a module. It won't work if the tool * cannot be run as a module. No escape yet. - * + * * Unsure whether this works in Linux. */ - final String pythonPath = CLIUtils.getEnvMap().get( envname ); + try + { + final String pythonPath = CLIUtils.getEnvMap().get( envname ); + cmd.add( pythonPath ); + cmd.add( "-m" ); + } + catch ( final IOException e ) + { + System.err.println( "Could not find the conda executable or change the conda environment.\n" + + "Please configure the path to your conda executable in Edit > Options > Configure TrackMate Conda path..." ); + e.printStackTrace(); + } - cmd.add( pythonPath ); - cmd.add( "-m" ); } // Split by spaces final String executableCommand = getCommand(); diff --git a/src/main/java/fiji/plugin/trackmate/util/cli/CondaPathConfigCommand.java b/src/main/java/fiji/plugin/trackmate/util/cli/CondaPathConfigCommand.java index c6821d870..dc044ad44 100644 --- a/src/main/java/fiji/plugin/trackmate/util/cli/CondaPathConfigCommand.java +++ b/src/main/java/fiji/plugin/trackmate/util/cli/CondaPathConfigCommand.java @@ -8,12 +8,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . @@ -21,6 +21,9 @@ */ package fiji.plugin.trackmate.util.cli; +import java.io.IOException; +import java.util.Map; + import javax.swing.JLabel; import org.scijava.command.Command; @@ -33,6 +36,7 @@ import fiji.plugin.trackmate.gui.Icons; import fiji.plugin.trackmate.util.TMUtils; import fiji.util.gui.GenericDialogPlus; +import ij.IJ; @Plugin( type = Command.class, label = "Configure the path to the Conda executable used in TrackMate...", @@ -73,11 +77,29 @@ public void run() condaPath = dialog.getNextString(); prefs.put( CLIUtils.class, CLIUtils.CONDA_PATH_PREF_KEY, condaPath ); + test(); + } + + public void test() + { + try + { + final Map< String, String > map = CLIUtils.getEnvMap(); + final StringBuilder str = new StringBuilder(); + str.append( "Successfully retrieved the conda environment list:\n" ); + map.forEach( ( k, v ) -> str.append( String.format( " - %-20s → %s\n", k, v ) ) ); + IJ.log( str.toString() ); + } + catch ( final IOException e ) + { + IJ.error( "Conda executable path seems to be incorrect.\n" + + "Error message:\n " + + e.getMessage() ); + } } public static void main( final String[] args ) { TMUtils.getContext().getService( CommandService.class ).run( CondaPathConfigCommand.class, false ); } - }