Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.project
/.settings/
/target
/.vscode/
5 changes: 3 additions & 2 deletions src/main/java/org/scijava/plugins/scripting/python/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -26,6 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.plugins.scripting.python;

import org.scijava.script.ScriptREPL;
Expand Down
219 changes: 184 additions & 35 deletions src/main/java/org/scijava/plugins/scripting/python/OptionsPython.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -29,6 +29,14 @@

package org.scijava.plugins.scripting.python;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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;
import org.scijava.launcher.Config;
Expand All @@ -38,27 +46,20 @@
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.DialogPrompt;
import org.scijava.ui.UIService;
import org.scijava.widget.Button;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import org.scijava.widget.TextWidget;

/**
* Options for configuring the Python environment.
*
*
* @author Curtis Rueden
*/
@Plugin(type = OptionsPlugin.class, menu = {
@Menu(label = MenuConstants.EDIT_LABEL,
weight = MenuConstants.EDIT_WEIGHT,
mnemonic = MenuConstants.EDIT_MNEMONIC),
@Menu(label = "Options", mnemonic = 'o'),
@Menu(label = "Python...", weight = 10),
})
@Plugin(type = OptionsPlugin.class, menu = { @Menu(
label = MenuConstants.EDIT_LABEL, weight = MenuConstants.EDIT_WEIGHT,
mnemonic = MenuConstants.EDIT_MNEMONIC), @Menu(label = "Options",
mnemonic = 'o'), @Menu(label = "Python...", weight = 10), })
public class OptionsPython extends OptionsPlugin {

@Parameter
Expand All @@ -73,12 +74,28 @@ public class OptionsPython extends OptionsPlugin {
@Parameter(label = "Python environment directory", persist = false)
private File pythonDir;

@Parameter(label = "Rebuild Python environment", callback = "rebuildEnv")
@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;

@Parameter(label = "Launch in Python mode", callback = "updatePythonConfig", persist = false)
@Parameter(label = "Launch in Python mode", callback = "updatePythonConfig",
persist = false)
private boolean pythonMode;

@Parameter(required = false)
private UIService uiService;

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

// -- OptionsPython methods --

public File getPythonDir() {
Expand Down Expand Up @@ -124,28 +141,100 @@ public void load() {
}

if (pythonDir == null) {
// For the default Python directory, try to match the platform string used for Java installations.
final String javaPlatform = System.getProperty("scijava.app.java-platform");
final String platform = javaPlatform != null ? javaPlatform :
System.getProperty("os.name") + "-" + System.getProperty("os.arch");
final Path pythonPath = appService.getApp().getBaseDirectory().toPath().resolve("python").resolve(platform);
// For the default Python directory, try to match the platform
// string used for Java installations.
final String javaPlatform = System.getProperty(
"scijava.app.java-platform");
final String platform = javaPlatform != null ? javaPlatform : System
.getProperty("os.name") + "-" + System.getProperty("os.arch");
final Path pythonPath = appService.getApp().getBaseDirectory().toPath()
.resolve("python").resolve(platform);
pythonDir = pythonPath.toFile();
}

// 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() {
// Use scijava.app.python-env-file system property if present.
File environmentYaml = writeEnvironmentYaml();
commandService.run(RebuildEnvironment.class, true, "environmentYaml",
environmentYaml, "targetDir", pythonDir);
}

/**
* Returns the File for the environment.yml, using the system property if set.
*/
private File getEnvironmentYamlFile() {
final Path appPath = appService.getApp().getBaseDirectory().toPath();
File environmentYaml = appPath.resolve("config").resolve("environment.yml").toFile();
final String pythonEnvFileProp = System.getProperty("scijava.app.python-env-file");
File environmentYaml = appPath.resolve("config").resolve("environment.yml")
.toFile();
final String pythonEnvFileProp = System.getProperty(
"scijava.app.python-env-file");
if (pythonEnvFileProp != null) {
environmentYaml = OptionsPython.stringToFile(appPath, pythonEnvFileProp);
environmentYaml = stringToFile(appPath, pythonEnvFileProp);
}

commandService.run(RebuildEnvironment.class, true,
"environmentYaml", environmentYaml,
"targetDir", pythonDir
);
return environmentYaml;
}

@Override
Expand Down Expand Up @@ -175,6 +264,66 @@ public void save() {
// Proceed gracefully if config file cannot be written.
log.debug(exc);
}

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 =
"You have just enabled Python mode. Please restart for these changes to take effect! (after your python environment initializes, if needed)\n\n" +
"If Fiji fails to start, try deleting your configuration file and restarting.\n\nConfiguration file: " +
configFile;
uiService.showDialog(msg, "Python Mode Enabled",
DialogPrompt.MessageType.WARNING_MESSAGE);
}
}

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 All @@ -195,8 +344,8 @@ static File stringToFile(Path baseDir, String value) {
*/
static String fileToString(Path baseDir, File file) {
Path filePath = file.toPath();
Path relPath = filePath.startsWith(baseDir) ?
baseDir.relativize(filePath) : filePath.toAbsolutePath();
Path relPath = filePath.startsWith(baseDir) ? baseDir.relativize(filePath)
: filePath.toAbsolutePath();
return relPath.toString();
}
}
Loading