/
updater.py
126 lines (110 loc) · 4.7 KB
/
updater.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
# -*- coding: utf-8 -*-
# This file is part of CFVVDS.
#
# CFVVDS 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.
#
# CFVVDS 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 CFVVDS; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import urllib, os, md5, sys, wx
from xml.dom import minidom
updateserver = 'http://jproject.xz.lt/vanguard/updates/'
def CheckUpdate(dir):
try:
toupdate = []
res = urllib.urlopen(updateserver + 'update.xml')
ret = res.read()
xmldoc = minidom.parseString(ret)
for node in xmldoc.firstChild.childNodes:
file = node.getAttribute('name')
hash = node.getAttribute('value')
if os.path.exists(GetFilePath(dir,file)):
hash2 = GetFileSize(dir, file)
if hash != hash2:
toupdate.append(file)
else:
toupdate.append(file)
return toupdate
except: return []
def CheckVersionUpdate(v):
try:
res = urllib.urlopen(updateserver + 'versionupdate.xml')
ret = res.read()
xmldoc = minidom.parseString(ret)
nv = xmldoc.firstChild.firstChild.getAttribute('version')
if v == nv: return 0
else: return 1
except: return 0
def GetFilePath(a,b):
if sys.platform == "win32":
b = b.replace('/', '\\')
return os.path.join(a,b)
def Update(dir,toupdate):
try:
for s in toupdate:
urllib.urlretrieve(updateserver + s, GetFilePath(dir,s))
urllib.urlcleanup()
except:pass
def UpdateOne(dir, toupdate):
try:
urllib.urlretrieve(updateserver + toupdate, GetFilePath(dir,toupdate))
urllib.urlcleanup()
except: pass
def GetFileSize(dir, file):
return str(os.stat(GetFilePath(dir,file))[6])
class UpdateDialog(wx.ProgressDialog):
def __init__(self, parent, engine):
self._engine = engine
wx.ProgressDialog.__init__(self, 'Updater', 'Checking for updates...', 100, parent)
self.Show()
self.CheckUpdateDialog()
def DoUpdate(self, toupdate):
l = len(toupdate)
pr = (100/l)+1
pn = 0
for s in toupdate:
self.Update(pn,'Downloading %s...' % s)
UpdateOne(self._engine.BaseDirectory, s)
pn += pr
if pn > 99: pn = 99
def CheckUpdateDialog(self):
if CheckVersionUpdate(self._engine.GetVersion()):
toupdate = CheckUpdate(self._engine.BaseDirectory)
if self._engine.Frame.ShowDialog(self._engine.GetLangString('An update is avaible, would you like to update now?'),'',wx.YES_NO | wx.ICON_QUESTION | wx.YES_DEFAULT, parent=self) == wx.ID_YES:
self.DoUpdate(toupdate)
self._engine.Frame.ShowDialog(self._engine.GetLangString('Update Downloaded. Check your Cray Online folder for "update.zip" and extract it.'),'',wx.OK | wx.ICON_INFORMATION, parent=self)
sys.exit()
else:
self._engine.Frame.ShowDialog(self._engine.GetLangString('No update needed.'), '', wx.OK | wx.ICON_INFORMATION, parent=self)
self.Update(100, self._engine.GetLangString('Done'))
class ImagesUpdateDialog(wx.ProgressDialog):
def __init__(self, parent, engine):
self._engine = engine
wx.ProgressDialog.__init__(self, 'Updater', 'Checking for updates...', 100, parent)
self.Show()
self.CheckUpdateDialog()
def CheckUpdateDialog(self):
m = self._engine.CheckMissingImages()
d = []
if len(m) > 0:
l = len(m)
pr = (100/l)
pn = 0
for n in m:
if pn > 99: pn = 99
self.Update(pn,'Downloading %s...' % n)
if self._engine.DownloadImage(n):
d.append(n)
pn += pr
self._engine.Frame.ShowDialog(self._engine.GetLangString('Downloaded %s images, %s missing.', str(len(d)), str(len(m)-len(d))), '', wx.OK | wx.ICON_INFORMATION, parent=self)
else:
self._engine.Frame.ShowDialog(self._engine.GetLangString('No update needed.'), '', wx.OK | wx.ICON_INFORMATION, parent=self)
self.Update(100, self._engine.GetLangString('Done'))