Skip to content

Commit

Permalink
Print a message after the user configured the path to Conda.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinevez committed Jun 27, 2024
1 parent 76cb353 commit 70b0f57
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
74 changes: 38 additions & 36 deletions src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
Expand All @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package fiji.plugin.trackmate.util.cli;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package fiji.plugin.trackmate.util.cli;

import java.io.IOException;
import java.util.Map;

import javax.swing.JLabel;

import org.scijava.command.Command;
Expand All @@ -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...",
Expand Down Expand Up @@ -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 );
}

}

0 comments on commit 70b0f57

Please sign in to comment.