-
Notifications
You must be signed in to change notification settings - Fork 0
Usability changes #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c8269bf
6d9b831
6427cde
409316b
4058aea
a5b9075
bf9ef54
626ebb2
efdb4bd
4f2d908
21037bf
bc24da1
4009690
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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. | ||
|
|
@@ -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") | ||
| private Button rebuildEnvironment; | ||
|
|
||
|
|
@@ -83,6 +93,8 @@ public class OptionsPython extends OptionsPlugin { | |
| private UIService uiService; | ||
|
|
||
| private boolean initialPythonMode = false; | ||
| private String initialCondaDependencies; | ||
| private String initialPipDependencies; | ||
|
|
||
| // -- OptionsPython methods -- | ||
|
|
||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -163,7 +235,6 @@ private File getEnvironmentYamlFile() { | |
| environmentYaml = stringToFile(appPath, pythonEnvFileProp); | ||
| } | ||
| return environmentYaml; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -197,6 +268,9 @@ public void save() { | |
| if (pythonMode && (pythonDir == null || !pythonDir.exists())) { | ||
| rebuildEnv(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
|
|
@@ -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"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should append |
||
| yml.append(" - ").append(apposePython).append("\n"); | ||
| java.nio.file.Files.write(envFile.toPath(), yml.toString().getBytes()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
| catch (Exception e) { | ||
| log.debug("Could not write environment.yml: " + e.getMessage()); | ||
| } | ||
| return envFile; | ||
| } | ||
|
|
||
| // -- Utility methods -- | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍