-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionsPython.java
More file actions
203 lines (177 loc) · 6.75 KB
/
OptionsPython.java
File metadata and controls
203 lines (177 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*-
* #%L
* Python scripting language plugin to be used via scyjava.
* %%
* Copyright (C) 2021 - 2025 SciJava developers.
* %%
* 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
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
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 org.scijava.app.AppService;
import org.scijava.command.CommandService;
import org.scijava.launcher.Config;
import org.scijava.log.LogService;
import org.scijava.menu.MenuConstants;
import org.scijava.options.OptionsPlugin;
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.widget.Button;
/**
* 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), })
public class OptionsPython extends OptionsPlugin {
@Parameter
private AppService appService;
@Parameter
private CommandService commandService;
@Parameter
private LogService log;
@Parameter(label = "Python environment directory", persist = false)
private File pythonDir;
@Parameter(label = "Build Python environment", callback = "rebuildEnv")
private Button rebuildEnvironment;
@Parameter(label = "Launch in Python mode", callback = "updatePythonConfig",
persist = false)
private boolean pythonMode;
// -- OptionsPython methods --
public File getPythonDir() {
return pythonDir;
}
public boolean isPythonMode() {
return pythonMode;
}
public void setPythonDir(final File pythonDir) {
this.pythonDir = pythonDir;
}
public void setPythonMode(final boolean pythonMode) {
this.pythonMode = pythonMode;
}
// -- Callback methods --
@Override
public void load() {
// Read python-dir and launch-mode from app config file.
String configFileProp = System.getProperty("scijava.app.config-file");
File configFile = configFileProp == null ? null : new File(configFileProp);
if (configFile != null && configFile.canRead()) {
try {
final Map<String, String> config = Config.load(configFile);
final String cfgPythonDir = config.get("python-dir");
if (cfgPythonDir != null) {
final Path appPath = appService.getApp().getBaseDirectory().toPath();
pythonDir = stringToFile(appPath, cfgPythonDir);
}
final String cfgLaunchMode = config.get("launch-mode");
if (cfgLaunchMode != null) pythonMode = cfgLaunchMode.equals("PYTHON");
}
catch (IOException e) {
// Proceed gracefully if config file is not accessible.
log.debug(e);
}
}
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);
pythonDir = pythonPath.toFile();
}
}
public void rebuildEnv() {
// Use scijava.app.python-env-file system property if present.
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");
if (pythonEnvFileProp != null) {
environmentYaml = OptionsPython.stringToFile(appPath, pythonEnvFileProp);
}
commandService.run(RebuildEnvironment.class, true, "environmentYaml",
environmentYaml, "targetDir", pythonDir);
}
@Override
public void save() {
// Write python-dir and launch-mode values to app config file.
final String configFileProp = System.getProperty("scijava.app.config-file");
if (configFileProp == null) return; // No config file to update.
final File configFile = new File(configFileProp);
Map<String, String> config = null;
if (configFile.isFile()) {
try {
config = Config.load(configFile);
}
catch (IOException exc) {
// Proceed gracefully if config file is not accessible.
log.debug(exc);
}
}
if (config == null) config = new LinkedHashMap<>();
final Path appPath = appService.getApp().getBaseDirectory().toPath();
config.put("python-dir", fileToString(appPath, pythonDir));
config.put("launch-mode", pythonMode ? "PYTHON" : "JVM");
try {
Config.save(configFile, config);
}
catch (IOException exc) {
// Proceed gracefully if config file cannot be written.
log.debug(exc);
}
}
// -- Utility methods --
/**
* Converts a path string to a file, treating relative path expressions as
* relative to the given base directory, not the current working directory.
*/
static File stringToFile(Path baseDir, String value) {
final Path path = Paths.get(value);
final Path absPath = path.isAbsolute() ? path : baseDir.resolve(path);
return absPath.toFile();
}
/**
* Converts a file to a path string, which in the case of a file beneath the
* given base directory, will be a path expression relative to that base.
*/
static String fileToString(Path baseDir, File file) {
Path filePath = file.toPath();
Path relPath = filePath.startsWith(baseDir) ? baseDir.relativize(filePath)
: filePath.toAbsolutePath();
return relPath.toString();
}
}