forked from simone-f/qat_script
/
update_checker.py
354 lines (318 loc) · 13.9 KB
/
update_checker.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#! /usr/bin/env jython
#
# Copyright 2014 Simone F. <groppo8@gmail.com>
#
# This file is part of qat_script.
# qat_script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
#java import
from javax.swing import JOptionPane, SwingWorker, JPanel
from java.beans import PropertyChangeListener
from java.io import FileOutputStream, File
from java.net import URL, UnknownHostException, SocketException
from java.lang import String
from java.text import MessageFormat
from javax.swing import JDialog, BorderFactory
from java.io import FileInputStream
from java.net import URI
from java.nio.file import Files, Paths
from java.util import Properties
from java.util.jar import JarFile
from java.awt import BorderLayout, Dimension
from javax.swing import JButton, JProgressBar
#josm import
from org.openstreetmap.josm import Main
from org.openstreetmap.josm.gui.widgets import JMultilineLabel
from org.openstreetmap.josm.tools import OpenBrowser
#python import
from jarray import array
##### Updater thread ###################################################
class Updater:
def __init__(self, app, mode):
self.app = app
self.mode = mode
UpdaterTask(self).execute()
print "- Checking for updates..."
class UpdaterTask(SwingWorker):
"""SwingWorker task used to look for any update of the script
"""
def __init__(self, updater):
self.app = updater.app
self.mode = updater.mode
SwingWorker.__init__(self)
def doInBackground(self):
#print "\n- Checking for the latest version..."
try:
url = URL(self.app.scriptVersionUrl)
uc = url.openConnection()
ins = uc.getInputStream()
p = Properties()
p.load(ins)
latestScriptVersion = p.getProperty("script")
self.app.latestToolsVersion = p.getProperty("tools")
except (UnknownHostException, SocketException):
print "I can't connect to:\n%s" % url
ins.close()
return
if latestScriptVersion == self.app.SCRIPTVERSION:
#using latest script
print " already using the latest script version:", self.app.SCRIPTVERSION
if self.app.latestToolsVersion == self.app.TOOLSVERSION:
#using latest tools
print " already using the latest tools version:", self.app.TOOLSVERSION
if self.mode != "auto":
JOptionPane.showMessageDialog(self.app.preferencesFrame,
self.app.strings.getString("using_latest"))
return
else:
#not using latest tools
print " tools can be updated: %s -> %s" % (self.app.TOOLSVERSION,
self.app.latestToolsVersion)
answer = JOptionPane.showConfirmDialog(Main.parent,
self.app.strings.getString("update_tools_question"),
self.app.strings.getString("updates_available"),
JOptionPane.YES_NO_OPTION)
if answer == 0:
#Download the updated tools data
print "\n- Update tools data"
try:
self.app.toolsProgressDialog
except AttributeError:
from java.awt import Dialog
self.app.toolsProgressDialog = ToolsProgressDialog(Main.parent,
self.app.strings.getString("download_tools_updates"),
Dialog.ModalityType.APPLICATION_MODAL,
self.app)
self.app.toolsProgressDialog.show()
else:
#not using latest script
print "a new script version is available:\n%s -> %s" % (self.app.SCRIPTVERSION,
latestScriptVersion)
messageArguments = array([self.app.SCRIPTVERSION, latestScriptVersion], String)
formatter = MessageFormat("")
formatter.applyPattern(self.app.strings.getString("updates_warning"))
msg = formatter.format(messageArguments)
options = [
self.app.strings.getString("Do_not_check_for_updates"),
self.app.strings.getString("Visit_Wiki"),
self.app.strings.getString("cancel")]
answer = JOptionPane.showOptionDialog(Main.parent,
msg,
self.app.strings.getString("updates_available"),
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
None,
options,
options[1])
if answer == 0:
self.app.properties.setProperty("check_for_update", "off")
self.app.save_config(self)
elif answer == 1:
OpenBrowser.displayUrl(self.app.SCRIPTWEBSITE)
##### Tools updater ####################################################
class ToolsProgressDialog(JDialog, PropertyChangeListener):
def __init__(self, parent, title, modal, app):
"""Restore the tools data by:
- downloading the tools list
- downloading the tools jar files
- extracting the tools data from jar files
"""
self.app = app
border = BorderFactory.createEmptyBorder(5, 7, 5, 7)
mainPane = self.getContentPane()
mainPane.setLayout(BorderLayout(5, 5))
mainPane.setBorder(border)
self.setPreferredSize(Dimension(400, 200))
self.taskOutput = JMultilineLabel(self.app.strings.getString("checking_for_updates"))
self.taskOutput.setMaxWidth(350)
self.progressBar = JProgressBar(0, 100, value=0, stringPainted=True)
self.progressBar.setIndeterminate(True)
self.cancelBtn = JButton("Cancel",
actionPerformed=self.on_cancelBtn_clicked,
enabled=True)
self.okBtn = JButton("Ok",
actionPerformed=self.on_okBtn_clicked,
enabled=False)
mainPane.add(self.taskOutput, BorderLayout.CENTER)
bottomPanel = JPanel(BorderLayout(5, 5))
bottomPanel.add(self.progressBar, BorderLayout.PAGE_START)
buttonsPanel = JPanel()
buttonsPanel.add(self.okBtn)
buttonsPanel.add(self.cancelBtn)
bottomPanel.add(buttonsPanel, BorderLayout.PAGE_END)
mainPane.add(bottomPanel, BorderLayout.PAGE_END)
self.pack()
self.task = DownloaderTask(self)
self.task.addPropertyChangeListener(self)
self.task.execute()
def propertyChange(self, e):
# Invoked when task's progress property changes.
if e.propertyName == "progress":
if e.newValue == 2:
text = self.app.strings.getString("downloading_tools_list")
elif e.newValue == 3:
text = self.app.strings.getString("tools_not_downloaded_warning")
elif e.newValue == 5:
text = self.app.strings.getString("downloading_tools_data")
elif e.newValue == 6:
text = self.app.strings.getString("i_cannot_download_the_tool_files")
elif e.newValue == 7:
text = self.app.strings.getString("extracting_jar_files")
elif e.newValue == 8:
text = self.app.strings.getString("tools_updated")
self.taskOutput.setText("<html>" + text + "</html>")
def on_cancelBtn_clicked(self, e):
self.dispose()
self.task.cancel(True)
def on_okBtn_clicked(self, e):
self.dispose()
class DownloaderTask(SwingWorker):
def __init__(self, gui):
self.gui = gui
self.app = self.gui.app
self.toolsListFile = File.separator.join([self.app.SCRIPTDIR,
"tools",
"tools_list.properties"])
self.tmpToolsListFile = File.separator.join([self.app.SCRIPTDIR,
"tools",
"tmp_tools_list.properties"])
SwingWorker.__init__(self)
def doInBackground(self):
#Initialize progress property.
progress = 0
self.super__setProgress(progress)
# "\n download tools list"
progress = 2
self.super__setProgress(progress)
self.delete_file(self.tmpToolsListFile)
if not self.download_file(self.app.toolsListUrl, self.tmpToolsListFile):
# " I cannot download the tools list."
progress = 3
self.super__setProgress(progress)
return
toolsInfo = read_tools_list(self.tmpToolsListFile)
#Download tools data as jar files
progress = 5
self.super__setProgress(progress)
self.jarDir = File.separator.join([self.app.SCRIPTDIR, "tools", "jar"])
if not File(self.jarDir).exists():
File(self.jarDir).mkdir()
else:
#delete old files
for jarFileName in File(self.jarDir).list():
File(File.separator.join([self.jarDir, jarFileName])).delete()
#download new files
for toolRef, jarUrl in toolsInfo:
jarFilePath = File.separator.join([self.jarDir, "%s.jar" % toolRef])
answer = self.download_file(jarUrl, jarFilePath)
if not answer:
# " I cannot download the tools file"
progress = 6
self.super__setProgress(progress)
return
#Extract tools data from jar files
self.toolsDir = File.separator.join([self.app.SCRIPTDIR, "tools", "data"])
progress = 7
self.super__setProgress(progress)
self.extract_tools_data_from_jar_files()
#Remove temporary file
self.delete_file(self.toolsListFile)
Files.copy(Paths.get(self.tmpToolsListFile), Paths.get(self.toolsListFile))
self.delete_file(self.tmpToolsListFile)
progress = 8
self.super__setProgress(progress)
def done(self):
try:
self.get() # raise exception if abnormal completion
self.gui.progressBar.setValue(100)
self.gui.okBtn.setEnabled(True)
self.gui.cancelBtn.setEnabled(False)
self.app.TOOLSVERSION = self.app.latestToolsVersion
try:
self.app.aboutDlg
self.app.aboutDlg.toolsVersionLbl.setText(self.app.TOOLSVERSION)
except AttributeError:
pass
self.app.config.save_versions()
print " done."
except:
self.gui.taskOutput.setText(self.app.strings.getString("tools_not_correctly_updated"))
self.gui.progressBar.setIndeterminate(False)
def download_file(self, url, filePath):
"""Downloads a file form url and save it as filePath
"""
try:
print "\ndownloading"
print url
print filePath
inputStream = URI.create(url).toURL().openStream()
Files.copy(inputStream, Paths.get(filePath))
return True
except (UnknownHostException, SocketException), e:
print e
print "I cannot download:\n%s" % url
return False
def extract_tools_data_from_jar_files(self):
"""Create tools directories from JAR files with tools data
Read directories from tools/data and create jar files in tools/jar
"""
jarDir = File(self.jarDir)
for jarFileName in jarDir.list():
toolDir = File.separator.join([self.toolsDir, jarFileName[:-4]])
self.delete_old_tool_directory(File(toolDir))
jar = JarFile(File(self.jarDir, jarFileName))
for entry in jar.entries():
f = File(File.separator.join([self.toolsDir, entry.getName()]))
if entry.isDirectory():
f.mkdir()
continue
inputStream = jar.getInputStream(entry)
fos = FileOutputStream(f)
while inputStream.available() > 0:
fos.write(inputStream.read())
fos.close()
inputStream.close()
def delete_old_tool_directory(self, file):
if not file.isDirectory():
file.delete()
else:
if len(file.list()) == 0:
file.delete()
else:
#recursive delete
for temp in file.list():
fileDelete = File(file, temp)
self.delete_old_tool_directory(fileDelete)
if len(file.list()) == 0:
file.delete()
def delete_file(self, fileName):
if File(fileName).exists:
File(fileName).delete()
def read_tools_list(tmpToolsListFile):
"""Read the tools names and tools data urls
from the downlaoded tools list
"""
toolsInfo = []
properties = Properties()
fin = FileInputStream(tmpToolsListFile)
properties.load(fin)
toolsRefs = properties.getProperty("tools.list").split("|")
for toolRef in toolsRefs:
jarUrl = properties.getProperty(toolRef)
toolsInfo.append([toolRef, jarUrl.replace("\n", "")])
fin.close()
return toolsInfo