-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathInstallQuPathScripts.groovy
131 lines (116 loc) · 4.21 KB
/
InstallQuPathScripts.groovy
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
/**
* Fiji script installing the content of a repo as shared scripts in QuPath v0.5
*
* QuPath uses Java user preferences, which can be easily accessed for any Java software (like Fiji!)
* Thus, this script can look whether a QuPath user path is already defined, and adds the jars
* necessary for a QuPath extension into it.
*
* If the preference of a QuPath user folder does not exist, it it created and will be recognized
* when QuPath is installed
*
* For this to work, the QuPath scripts have to be accessible via downloading through a single URL,
* pointing to a zip file
*
* Existing scripts will be overriden, but no obsolete script will be actively removed
*
* Example of url scripts which can be installed with this script:
*
*
* https://github.com/BIOP/qupath-scripts/archive/refs/heads/main.zip
*
* Which is the latest state of the main branch of https://github.com/BIOP/qupath-scripts
*
* Author: Nicolas Chiaruttini, BIOP, EPFL 2022
**/
#@String(label="QuPath User Folder", value="C:/QuPath Common Data") defaultQuPathUserPath
#@String(label="QuPath Prefs Node", value="io.github.qupath/0.5") quPathPrefsNode
#@String(label="URL of QuPath Scripts Repo to install") quPathScriptsURL
#@Boolean(lable="Quit after installation") quitAfterInstall
IJ.log(defaultQuPathUserPath)
IJ.log(quPathPrefsNode)
IJ.log(quPathScriptsURL)
defaultQuPathUserPath = new File(defaultQuPathUserPath).getAbsolutePath() // avoid double slash issues
// Check pre-existing QuPath user Path
Preferences prefs = Preferences.userRoot().node(quPathPrefsNode);
def allKeys = prefs.keys() as List
println(allKeys.contains('userPath'))
def userPath = prefs.get("userPath", defaultQuPathUserPath)
if (userPath.equals(defaultQuPathUserPath)) {
IJ.log("Setting java prefs because the pref may not exist")
prefs.put("userPath", defaultQuPathUserPath)
prefs.put("scriptsPath", defaultQuPathUserPath+"/qupath-scripts")
} else {
IJ.log("QuPath user path already exists")
prefs.put("scriptsPath", defaultQuPathUserPath+"/qupath-scripts")
}
IJ.log("QuPath user path: "+userPath)
// Create QuPath user folder if needed
File userPathFile = new File(userPath)
if (!userPathFile.exists()) {
def result = userPathFile.mkdir()
if (!result) {
IJ.log("Could not create folder "+userPath+". Exiting.")
return
}
}
// Create QuPath scripts folder if needed
File scriptsDir = new File(userPath,"qupath-scripts")
if (!scriptsDir.exists()) {
def result = scriptsDir.mkdir()
if (!result) {
IJ.log("Could not create folder "+scriptsDir+". Exiting.")
return
}
}
// Copy file from URL to disk
def url = new URL(quPathScriptsURL)
String fName = FilenameUtils.getName(url.getPath())
String extension = FilenameUtils.getExtension(url.getPath())
def outputFile = new File(scriptsDir, fName)
IJ.log("Starting download")
FileUtils.copyURLToFile(url, outputFile);
IJ.log("Download done")
// Unzip file if necessary
if (extension.equals("zip")) {
IJ.log("Unzipping")
try (ZipFile zipFile = new ZipFile(outputFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// IJ.log(entry.getName())
// Remove the first subfolder: convenient for extracting a github repo
String entryOneLessFolder = entry.getName().substring(entry.getName().indexOf('/'), entry.getName().size());
File entryDestination = new File(scriptsDir, entryOneLessFolder);
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
try (InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(entryDestination)) {
IOUtils.copy(in, out);
}
}
}
}
IJ.log("Unzipping done")
outputFile.delete()
}
if (quitAfterInstall) {
IJ.run("Quit");
}
"Done"
import java.util.regex.Pattern
import java.util.regex.Matcher
import java.util.prefs.Preferences
import ij.IJ
import java.io.File
import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
import org.apache.commons.io.IOUtils
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.stream.Stream
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.Path
import java.util.stream.Collectors