Skip to content

Commit fd958bb

Browse files
committed
Handle messy python directories
1 parent f6c4005 commit fd958bb

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/main/java/org/scijava/plugins/scripting/python/OptionsPython.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,26 @@ public boolean isPythonMode() {
118118
}
119119

120120
public void setPythonDir(final File pythonDir) {
121-
this.pythonDir = pythonDir;
121+
if (pythonDir == null) {
122+
this.pythonDir = null;
123+
} else {
124+
// Trim whitespace and remove illegal characters from the path
125+
String path = pythonDir.getPath();
126+
if (path != null) {
127+
// Remove leading/trailing whitespace and angle brackets
128+
path = path.trim().replaceAll("^[\\s<>]+|[\\s<>]+$", "");
129+
// Remove illegal characters for Windows paths except : after drive letter
130+
// Remove all colons except at index 1 (C:)
131+
if (path.length() > 2 && path.charAt(1) == ':') {
132+
path = path.substring(0, 2) + path.substring(2).replace(":", "");
133+
} else {
134+
path = path.replace(":", "");
135+
}
136+
// Remove any other illegal characters (keep it simple)
137+
path = path.replaceAll("[\n\r\t]", "");
138+
}
139+
this.pythonDir = new File(path);
140+
}
122141
}
123142

124143
public void setPythonMode(final boolean pythonMode) {
@@ -257,6 +276,7 @@ private File getEnvironmentYamlFile() {
257276

258277
@Override
259278
public void save() {
279+
setPythonDir(pythonDir); // clean up the path
260280
// Write python-dir and launch-mode values to app config file.
261281
final String configFileProp = System.getProperty("scijava.app.config-file");
262282
if (configFileProp == null) return; // No config file to update.

0 commit comments

Comments
 (0)