Skip to content

Commit 1493a85

Browse files
renamed the updater
1 parent fd0740a commit 1493a85

4 files changed

Lines changed: 200 additions & 6 deletions

File tree

volker/scripts/mri_updater.py

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import os
2+
import shutil
3+
from urllib import urlopen
4+
from zipfile import ZipFile
5+
from ij import IJ
6+
from ij import Prefs
7+
from ij.gui import GenericDialog
8+
9+
10+
class Updater:
11+
12+
13+
def __init__(self):
14+
self.tool = "spine_analyzer"
15+
self.folder = "Spine-Analyzer"
16+
self.author = "volker"
17+
self.libPath = IJ.getDirectory("imagej") + "/jars/Lib/"
18+
self.pythonModulesPath = 'fr/cnrs/mri/cialib/'
19+
self.tag = None
20+
21+
self.readPreferences()
22+
23+
self.host = "https://github.com/"
24+
self.baseUrl = "MontpellierRessourcesImagerie/imagej_macros_and_scripts/"
25+
repositoryUrl = self.host + self.baseUrl
26+
self.raw = "https://raw.githubusercontent.com"
27+
self.component = repositoryUrl + "/tree/<tag>/" + self.author + "/toolsets/" + self.tool
28+
self.tagsUrl = repositoryUrl + "tags"
29+
self.archiveUrl = repositoryUrl + 'archive/refs/tags/'
30+
self.downloadBaseUrl = repositoryUrl + "/releases/tag/"
31+
self.sourceFolder = ""
32+
self.pluginsToolDir = IJ.getDirectory("plugins") + self.folder + "/"
33+
self.toolsetsDir = IJ.getDirectory("macros") + "toolsets/"
34+
35+
if not os.path.exists(self.pluginsToolDir):
36+
os.makedirs(self.pluginsToolDir)
37+
if not os.path.exists(self.libPath + self.pythonModulesPath):
38+
os.makedirs(self.libPath + self.pythonModulesPath)
39+
moduleFolders = self.pythonModulesPath.split("/")
40+
currentPath = self.libPath
41+
for folder in moduleFolders:
42+
currentPath = currentPath + "/" + folder
43+
open(currentPath + "/__init__.py", 'a').close()
44+
45+
46+
def runUpdate(self):
47+
self.tag = self.getTargetVersionFromUser()
48+
if not self.tag:
49+
IJ.log("No version selected, update canceled...")
50+
return
51+
self.downloadTagFromGithub(self.tag)
52+
self.installTool()
53+
self.writeVersionInfo()
54+
IJ.log("Update finished, please restart ImageJ!")
55+
IJ.showMessage("Update finished, please restart ImageJ!")
56+
57+
58+
def writeVersionInfo(self):
59+
with open(self.pluginsToolDir + "version.txt", 'w') as file:
60+
file.write(self.tag)
61+
62+
63+
def installTool(self):
64+
files = [self.sourceFolder + f for f in os.listdir(self.sourceFolder ) if os.path.isfile(self.sourceFolder+f)]
65+
for file in files:
66+
if self.isToolset(file):
67+
IJ.log("writing toolset " + file + " to " + self.toolsetsDir);
68+
shutil.copy(file, self.toolsetsDir)
69+
continue
70+
if self.isModule(file):
71+
IJ.log("writing python module " + file + " to " + self.libPath + self.pythonModulesPath);
72+
shutil.copy(file, self.libPath + self.pythonModulesPath)
73+
continue
74+
IJ.log("writing file " + file + " to " + self.pluginsToolDir);
75+
shutil.copy(file, self.pluginsToolDir)
76+
77+
78+
def isToolset(self, file):
79+
result = True
80+
content = IJ.openAsString(file)
81+
result = result and file.endswith('.ijm')
82+
result = result and 'macro "' in content
83+
return result
84+
85+
86+
def isMacroCommand(self, file):
87+
result = True
88+
content = IJ.openAsString(file)
89+
result = result and file.endswith('.ijm')
90+
result = result and not 'macro "' in content
91+
return result
92+
93+
94+
def isScript(self, file):
95+
result = True
96+
content = IJ.openAsString(file)
97+
result = result and file.endswith('.py')
98+
result = result and "def main():" in content
99+
return result
100+
101+
102+
def isModule(self, file):
103+
result = True
104+
content = IJ.openAsString(file)
105+
result = result and file.endswith('.py')
106+
result = result and not "def main():" in content
107+
return result
108+
109+
110+
def downloadTagFromGithub(self, tag):
111+
downloadUrl = self.archiveUrl + tag + ".zip"
112+
tmpFolder = IJ.getDirectory("temp")
113+
outputFilePath = tmpFolder + "/" + "imagej_macros_and_scripts" + "_" + tag + ".zip"
114+
response = urlopen(downloadUrl)
115+
CHUNK = 16 * 1024
116+
with open(outputFilePath, 'wb') as f:
117+
while True:
118+
chunk = response.read(CHUNK)
119+
if not chunk:
120+
break
121+
f.write(chunk)
122+
with ZipFile(outputFilePath, 'r') as zObject:
123+
zObject.extractall(path=tmpFolder)
124+
self.sourceFolder = IJ.getDirectory("temp") + "imagej_macros_and_scripts" + "-" + tag[1:] + "/" + self.author + "/toolsets/" + self.tool + "/"
125+
126+
127+
def getTargetVersionFromUser(self):
128+
currentVersion = self.getCurrentVersion()
129+
tags = self.getTags()
130+
if len(tags) < 1:
131+
IJ.log("No versions found")
132+
return False
133+
self.tag = tags[0]
134+
gd = GenericDialog(self.tool + " - Install or Update")
135+
gd.addMessage("Installed versions: " + str(currentVersion))
136+
gd.addChoice("version: ", tags, self.tag)
137+
gd.showDialog()
138+
if gd.wasCanceled():
139+
return False
140+
self.tag = gd.getNextChoice()
141+
return self.tag
142+
143+
144+
def getTags(self):
145+
IJ.log("Getting versions from github...")
146+
tagsPage = IJ.openUrlAsString(self.tagsUrl)
147+
lines = tagsPage.split("\n")
148+
tags = [];
149+
for line in lines:
150+
if self.baseUrl + "releases/tag/" in line:
151+
parts = line.split("/")
152+
parts = parts[5].split('"')
153+
tag = parts[0]
154+
if not tag in tags:
155+
tags.append(tag)
156+
tagsWithTool = []
157+
for tag in tags:
158+
url = self.component.replace("<tag>", tag)
159+
answer = IJ.openUrlAsString(url)
160+
if not answer.startswith("<Error"):
161+
tagsWithTool.append(tag)
162+
return tagsWithTool
163+
164+
165+
def readPreferences(self):
166+
toolToBeUpdated = Prefs.get("mri.update.tool", "")
167+
updateFolder = Prefs.get("mri.update.folder", "")
168+
updateAuthor = Prefs.get("mri.update.author", "")
169+
updateModulesFolder = Prefs.get("mri.update.modules", "")
170+
if toolToBeUpdated:
171+
self.tool = toolToBeUpdated
172+
if updateFolder:
173+
self.folder = updateFolder
174+
if updateAuthor:
175+
self.autor = updateAuthor
176+
if updateModulesFolder:
177+
self.pythonModulesPath = updateModulesFolder
178+
179+
180+
def getCurrentVersion(self):
181+
pluginsDir = IJ.getDirectory("plugins")
182+
pluginsToolDir = pluginsDir + self.folder + "/"
183+
if not os.path.exists(pluginsToolDir):
184+
return None
185+
if not os.path.exists(pluginsToolDir + "version.txt"):
186+
return None
187+
version = IJ.openAsString(pluginsToolDir + "version.txt")
188+
return version
189+
190+
191+
192+
if __name__ == "__main__":
193+
Updater().runUpdate()
194+

volker/toolsets/measure_intensity_without_spots/measure_intensity_without_spots.ijm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ function getBatchInputImages(baseDir) {
343343

344344
function installOrUpdate() {
345345
scriptsFolder = getDirectory("imagej") + "scripts/";
346-
if (File.exists(scriptsFolder + "mri-updater.py")) {
346+
if (File.exists(scriptsFolder + "mri_updater.py")) {
347347
print("Running the updater...");
348348
setToolInfo();
349-
run("mri-updater");
349+
run("mri updater");
350350
unsetToolInfo();
351351
} else {
352352
print("Installing the updater...");

volker/toolsets/spine_analyzer/spine_analyzer.ijm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ function getNextLabel() {
456456

457457
function installOrUpdate() {
458458
scriptsFolder = getDirectory("imagej") + "scripts/";
459-
if (File.exists(scriptsFolder + "mri-updater.py")) {
459+
if (File.exists(scriptsFolder + "mri_updater.py")) {
460460
print("Running the updater...");
461461
setToolInfo();
462-
run("mri-updater");
462+
run("mri updater");
463463
unsetToolInfo();
464464
} else {
465465
print("Installing the updater...");

volker/toolsets/width_profile_tools/width_profile_tools.ijm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ function downloadImage(url, folder) {
182182

183183
function installOrUpdate() {
184184
scriptsFolder = getDirectory("imagej") + "scripts/";
185-
if (File.exists(scriptsFolder + "mri-updater.py")) {
185+
if (File.exists(scriptsFolder + "mri_updater.py")) {
186186
print("Running the updater...");
187187
setToolInfo();
188-
run("mri-updater");
188+
run("mri updater");
189189
unsetToolInfo();
190190
} else {
191191
print("Installing the updater...");

0 commit comments

Comments
 (0)