Skip to content

Commit 20a8621

Browse files
authored
Merge pull request #332 from trackmate-sc/conda-run
Rework the conda configuration and the way conda tools are run in TrackMate
2 parents fca797a + f06a0ec commit 20a8621

7 files changed

Lines changed: 2005 additions & 277 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>sc.fiji</groupId>
1313
<artifactId>TrackMate</artifactId>
14-
<version>8.0.1-SNAPSHOT</version>
14+
<version>8.1.0-SNAPSHOT</version>
1515

1616
<name>TrackMate</name>
1717
<description>TrackMate plugin for Fiji.</description>

src/main/java/fiji/plugin/trackmate/util/cli/CLIUtils.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* it under the terms of the GNU General Public License as
99
* published by the Free Software Foundation, either version 3 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/gpl-3.0.html>.
@@ -75,6 +75,15 @@ public static final Process createProcess( final CLIConfigurator cli, final File
7575
{
7676
final List< String > cmd = CommandBuilder.build( cli );
7777
final ProcessBuilder pb = new ProcessBuilder( cmd );
78+
if ( cli instanceof CondaCLIConfigurator )
79+
{
80+
// Env variables.
81+
final Map< String, String > env = new HashMap<>();
82+
final String condaRootPrefix = getCondaRootPrefix();
83+
env.put( "MAMBA_ROOT_PREFIX", condaRootPrefix );
84+
env.put( "CONDA_ROOT_PREFIX", condaRootPrefix );
85+
pb.environment().putAll( env );
86+
}
7887
pb.redirectOutput( ProcessBuilder.Redirect.appendTo( logFile ) );
7988
pb.redirectError( ProcessBuilder.Redirect.appendTo( logFile ) );
8089
return pb.start();
@@ -319,6 +328,11 @@ public static List< String > preparePythonCommand( final String envName, final S
319328
return cmd;
320329
}
321330

331+
public static void clearEnvMap()
332+
{
333+
envMap = null;
334+
}
335+
322336
public static Map< String, String > getEnvMap() throws IOException
323337
{
324338
if ( envMap == null )

src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,23 @@
88
* it under the terms of the GNU General Public License as
99
* published by the Free Software Foundation, either version 3 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/gpl-3.0.html>.
2020
* #L%
2121
*/
2222
package fiji.plugin.trackmate.util.cli;
2323

24-
import java.io.IOException;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.List;
2827

29-
import ij.IJ;
30-
3128
public abstract class CondaCLIConfigurator extends CLIConfigurator
3229
{
3330

@@ -127,55 +124,41 @@ protected CondaCLIConfigurator()
127124
setCommandTranslator( condaEnv, s -> {
128125
final List< String > cmd = new ArrayList<>();
129126
final String condaPath = CLIUtils.getCondaPath();
130-
// Conda and executable stuff.
131-
final String envname = ( String ) s;
132-
if ( IJ.isWindows() )
127+
final String os = System.getProperty( "os.name" ).toLowerCase();
128+
if ( os.contains( "win" ) )
133129
{
134-
/*
135-
* In Windows: Launch a shell, change the conda environment,
136-
* runs the command in this environment.
137-
*/
130+
// In Windows: Launch a cmd.exe shell.
138131
cmd.addAll( Arrays.asList( "cmd.exe", "/c" ) );
139-
cmd.addAll( Arrays.asList( condaPath, "activate", envname ) );
140-
cmd.add( "&" );
141132
}
142133
else
143134
{
144-
/*
145-
* On Mac: we cannot change the conda environment in the process
146-
* builder (I tried very hard). So we use the environment to
147-
* retrieve what is the path of the Python executable of this
148-
* env, and runs the tool as a module. It won't work if the tool
149-
* cannot be run as a module. No escape yet.
150-
*
151-
* Unsure whether this works in Linux.
152-
*/
153-
try
154-
{
155-
final String pythonPath = CLIUtils.getEnvMap().get( envname );
156-
cmd.add( pythonPath );
157-
cmd.add( "-m" );
158-
}
159-
catch ( final IOException e )
160-
{
161-
System.err.println( "Could not find the conda executable or change the conda environment.\n"
162-
+ "Please configure the path to your conda executable in Edit > Options > Configure TrackMate Conda path..." );
163-
e.printStackTrace();
164-
}
165-
catch ( final Exception e )
166-
{
167-
System.err.println( "Error running the conda executable:\n" );
168-
e.printStackTrace();
169-
}
135+
// On Mac or Linux.
136+
}
137+
// Call conda run.
138+
cmd.add( condaPath );
139+
cmd.add( "run" );
140+
cmd.add( "-n" );
141+
// The executable stuff.
142+
final String envname = ( String ) s;
143+
cmd.add( envname );
144+
// Only add these flags for conda/mamba, NOT for micromamba
145+
if ( !isMicromamba( condaPath ) )
146+
{
147+
cmd.add( "--no-capture-output" );
170148
}
171-
// Split by spaces
149+
// The rest of the command, split by spaces.
172150
final String executableCommand = getCommand();
173151
final String[] split = executableCommand.split( " " );
174152
cmd.addAll( Arrays.asList( split ) );
175153
return cmd;
176154
} );
177155
}
178156

157+
private static boolean isMicromamba( final String path )
158+
{
159+
return path != null && path.toLowerCase().contains( "micromamba" );
160+
}
161+
179162
@Override
180163
public CondaEnvironmentCommand getCommandArg()
181164
{

src/main/java/fiji/plugin/trackmate/util/cli/CondaExecutableCLIConfigurator.java

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/main/java/fiji/plugin/trackmate/util/cli/CondaPathConfigCommand.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)