Skip to content
122 changes: 120 additions & 2 deletions src/main/java/org/scijava/plugins/scripting/python/OptionsPython.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringJoiner;

import org.scijava.app.AppService;
import org.scijava.command.CommandService;
Expand All @@ -48,6 +49,7 @@
import org.scijava.ui.DialogPrompt;
import org.scijava.ui.UIService;
import org.scijava.widget.Button;
import org.scijava.widget.TextWidget;

/**
* Options for configuring the Python environment.
Expand All @@ -72,6 +74,14 @@ public class OptionsPython extends OptionsPlugin {
@Parameter(label = "Python environment directory", persist = false)
private File pythonDir;

@Parameter(label = "Conda dependencies", style = TextWidget.AREA_STYLE,
persist = false)
private String condaDependencies;

@Parameter(label = "Pip dependencies", style = TextWidget.AREA_STYLE,
persist = false)
private String pipDependencies;

@Parameter(label = "Build Python environment", callback = "rebuildEnv")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

private Button rebuildEnvironment;

Expand All @@ -83,6 +93,8 @@ public class OptionsPython extends OptionsPlugin {
private UIService uiService;

private boolean initialPythonMode = false;
private String initialCondaDependencies;
private String initialPipDependencies;

// -- OptionsPython methods --

Expand Down Expand Up @@ -142,10 +154,70 @@ public void load() {

// Store the initial value of pythonMode for later comparison
initialPythonMode = pythonMode;

// Populate condaDependencies and pipDependencies from environment.yml
condaDependencies = "";
pipDependencies = "";
java.util.Set<String> pipBlacklist = new java.util.HashSet<>();
pipBlacklist.add("appose-python");
pipBlacklist.add("pyimagej");
File envFile = getEnvironmentYamlFile();
if (envFile.exists()) {
try {
java.util.List<String> lines = java.nio.file.Files.readAllLines(envFile
.toPath());
boolean inDeps = false, inPip = false;
StringJoiner condaDeps = new StringJoiner("\n");
StringJoiner pipDeps = new StringJoiner("\n");
for (String line : lines) {
String trimmed = line.trim();
if (trimmed.startsWith("#") || trimmed.isEmpty()) {
// Ignore empty and comment lines
continue;
}
if (trimmed.startsWith("dependencies:")) {
inDeps = true;
continue;
}
if (inDeps && trimmed.startsWith("- pip")) {
inPip = true;
continue;
}
if (inDeps && trimmed.startsWith("- ") && !inPip) {
String dep = trimmed.substring(2).trim();
if (!dep.equals("pip")) condaDeps.add(dep);
continue;
}
if (inPip && trimmed.startsWith("- ")) {
String pipDep = trimmed.substring(2).trim();
boolean blacklisted = false;
for (String bad : pipBlacklist) {
if (pipDep.contains(bad)) {
blacklisted = true;
break;
}
}
if (!blacklisted) pipDeps.add(pipDep);
continue;
}
if (inDeps && !trimmed.startsWith("- ") && !trimmed.isEmpty())
inDeps = false;
if (inPip && (!trimmed.startsWith("- ") || trimmed.isEmpty())) inPip =
false;
}
condaDependencies = condaDeps.toString().trim();
pipDependencies = pipDeps.toString().trim();
initialCondaDependencies = condaDependencies;
initialPipDependencies = pipDependencies;
}
catch (Exception e) {
log.debug("Could not read environment.yml: " + e.getMessage());
}
}
}

public void rebuildEnv() {
File environmentYaml = getEnvironmentYamlFile();
File environmentYaml = writeEnvironmentYaml();
commandService.run(RebuildEnvironment.class, true, "environmentYaml",
environmentYaml, "targetDir", pythonDir);
}
Expand All @@ -163,7 +235,6 @@ private File getEnvironmentYamlFile() {
environmentYaml = stringToFile(appPath, pythonEnvFileProp);
}
return environmentYaml;

}

@Override
Expand Down Expand Up @@ -197,6 +268,9 @@ public void save() {
if (pythonMode && (pythonDir == null || !pythonDir.exists())) {
rebuildEnv();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

}
else {
writeEnvironmentYaml();
}
// Warn the user if pythonMode was just enabled and wasn't before
if (!initialPythonMode && pythonMode && uiService != null) {
String msg =
Expand All @@ -208,6 +282,50 @@ public void save() {
}
}

private File writeEnvironmentYaml() {
File envFile = getEnvironmentYamlFile();

// skip writing if nothing has changed
if (initialCondaDependencies.equals(condaDependencies) &&
initialPipDependencies.equals(pipDependencies)) return envFile;

// Update initial dependencies to detect future changes
initialCondaDependencies = condaDependencies;
initialPipDependencies = pipDependencies;

// Write environment.yml from condaDependencies and pipDependencies
try {
String name = "fiji";
String[] channels = { "conda-forge" };
String pyimagej = "pyimagej>=1.7.0";
String apposePython =
"git+https://github.com/apposed/appose-python.git@efe6dadb2242ca45820fcbb7aeea2096f99f9cb2";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not hardcode versions in Java. Can we instead parse them out of the existing shipped environment.yml when populating the GUI, so they will propagate naturally?

StringBuilder yml = new StringBuilder();
yml.append("name: ").append(name).append("\nchannels:\n");
for (String ch : channels)
yml.append(" - ").append(ch).append("\n");
yml.append("dependencies:\n");
for (String dep : condaDependencies.split("\n")) {
String trimmed = dep.trim();
if (!trimmed.isEmpty()) yml.append(" - ").append(trimmed).append("\n");
}
yml.append(" - pip\n");
yml.append(" - pip:\n");
for (String dep : pipDependencies.split("\n")) {
String trimmed = dep.trim();
if (!trimmed.isEmpty()) yml.append(" - ").append(trimmed).append(
"\n");
}
yml.append(" - ").append(pyimagej).append("\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should append pyimagej>=1.7.0 only if they did not include it themselves in their list. Otherwise, it might not be possible to change the version range used, and in particular might not be possible to pin to a specific version.

yml.append(" - ").append(apposePython).append("\n");
java.nio.file.Files.write(envFile.toPath(), yml.toString().getBytes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to tweak how this works, because the Updater currently ships the environment.yml and since this GUI now allows editing it, that results in a locally modified file. How about if we ship an environment.yml.default instead, and then if no environment.yml exists, we fall back to the default file for populating the package lists here?

}
catch (Exception e) {
log.debug("Could not write environment.yml: " + e.getMessage());
}
return envFile;
}

// -- Utility methods --

/**
Expand Down