-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.py
309 lines (221 loc) · 10.7 KB
/
upgrade.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
import os
import sys
import threading
import time
import shutil
import urllib2
import zipfile
import wx
import wx.lib.newevent
def download_with_progress(url, filelike, report_hook = None):
bytes_so_far = 0
response = urllib2.urlopen(url)
while True:
chunk = response.read(1024)
bytes_so_far += len(chunk)
if not chunk:
break
else:
filelike.write(chunk)
if report_hook:
report_hook(bytes_so_far)
if os.name == "nt":
def pid_is_running(pid):
#credit: http://stackoverflow.com/a/28065945/1149933
import ctypes
desired_access = 0x1000 #PROCESS_QUERY_LIMITED_INFORMATION
inherit_handle = False
hnd = ctypes.windll.kernel32.OpenProcess(desired_access,
inherit_handle,
pid)
if hnd:
ctypes.windll.kernel32.CloseHandle(hnd)
return True
else:
return False
elif os.name == "posix":
def pid_is_running(pid):
#untested, whatever
try:
os.kill(pid, 0)
return True
except:
return False
def humanise_filesize(bytes):
if 0 <= bytes < 1024:
return "%s B" % bytes
elif 1024 <= bytes < 1024**2:
return "%0.2f KiB" % (bytes / 1024.0)
elif 1024**2 <= bytes < 1024**3:
return "%0.2f MiB" % (bytes / 1024.0**2)
elif 1024**3 <= bytes < 1024**4:
return "%0.2f GiB" % (bytes / 1024.0**3)
elif 1024**4 <= bytes:
return "%0.2f TiB" % (bytes / 1024.0**4)
else: #negative
return "pretty fuckin' small yo"
class Downloader(threading.Thread):
def __init__(self, frame, url, evt_poke, evt_complete):
super(Downloader, self).__init__()
self.frame = frame
self.url = url
self.evt_poke = evt_poke
self.evt_complete = evt_complete
def run(self):
with file("__patch.zip", "wb") as f:
download_with_progress(self.url, f, self._poke)
wx.PostEvent(self.frame, self.evt_complete())
def _poke(self, bytes):
self.evt_poke.data = bytes
wx.PostEvent(self.frame, self.evt_poke(data = bytes))
class Waiter(threading.Thread):
def __init__(self, frame, pid, evt_complete):
super(Waiter, self).__init__()
self.frame = frame
self.pid = pid
self.evt_complete = evt_complete
def run(self):
while pid_is_running(self.pid):
time.sleep(1)
wx.PostEvent(self.frame, self.evt_complete())
class Extractor(threading.Thread):
def __init__(self, frame, evt_complete):
super(Extractor, self).__init__()
self.frame = frame
self.evt_complete = evt_complete
def run(self):
zip = zipfile.ZipFile("__patch.zip")
try:
os.mkdir("__patch")
except WindowsError as e:
if e.errno == 17: #already exists
pass
zip.extractall("__patch")
zip.close()
#Make way
folder = os.listdir("__patch")[0]
root = os.path.join("__patch", folder)
for path, folders, fnames in os.walk(root):
path = path[len(root) + 1:]
for fname in fnames:
os.remove(os.path.join(path, fname))
#Update
for path, folder, fnames in os.walk(root):
for fname in fnames:
#move
os.rename(os.path.join(path, fname),
os.path.join(path[len(root) + 1:], fname))
os.remove("__patch.zip")
shutil.rmtree("__patch")
wx.PostEvent(self.frame, self.evt_complete())
class Main(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(500,300), style = wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
sizer_bg = wx.BoxSizer(wx.VERTICAL)
self.panel_bg = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
self.panel_bg.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU))
sizer_main = wx.BoxSizer(wx.VERTICAL)
self.listbook_stage = wx.Listbook(self.panel_bg, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LB_DEFAULT)
self.panel_downloading = wx.Panel(self.listbook_stage, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
sizer_downloading = wx.BoxSizer(wx.VERTICAL)
sizer_downloading.AddSpacer((0, 0), 1, wx.EXPAND, 5)
self.label_downloading = wx.StaticText(self.panel_downloading, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0)
self.label_downloading.skele = "Fetching %s..."
self.label_downloading.Wrap(-1)
sizer_downloading.Add(self.label_downloading, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.label_downloaded = wx.StaticText(self.panel_downloading, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0)
self.label_downloaded.skele = u"%s downloaded..."
self.label_downloaded.Wrap(-1)
sizer_downloading.Add(self.label_downloaded, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
sizer_downloading.AddSpacer((0, 0), 2, wx.EXPAND, 5)
self.panel_downloading.SetSizer(sizer_downloading)
self.panel_downloading.Layout()
sizer_downloading.Fit(self.panel_downloading)
self.listbook_stage.AddPage(self.panel_downloading, u"Downloading", True)
self.panel_waiting = wx.Panel(self.listbook_stage, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
sizer_waiting = wx.BoxSizer(wx.VERTICAL)
sizer_waiting.AddSpacer((0, 0), 1, wx.EXPAND, 5)
self.label_waiting = wx.StaticText(self.panel_waiting, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0)
self.label_waiting.skele = "Waiting for %s to close. If this hasn't happened with some manner of rapidity, do please open the task manager and kill it yourself. The PID should be %s."
self.label_waiting.Wrap(250)
sizer_waiting.Add(self.label_waiting, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
sizer_waiting.AddSpacer((0, 0), 2, wx.EXPAND, 5)
self.panel_waiting.SetSizer(sizer_waiting)
self.panel_waiting.Layout()
sizer_waiting.Fit(self.panel_waiting)
self.listbook_stage.AddPage(self.panel_waiting, u"Waiting", False)
self.panel_extracting = wx.Panel(self.listbook_stage, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
sizer_extracting = wx.BoxSizer(wx.VERTICAL)
sizer_extracting.AddSpacer((0, 0), 1, wx.EXPAND, 5)
self.label_extracting = wx.StaticText(self.panel_extracting, wx.ID_ANY, u"Currently extracting patch...", wx.DefaultPosition, wx.DefaultSize, 0)
self.label_extracting.Wrap(-1)
sizer_extracting.Add(self.label_extracting, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
sizer_extracting.AddSpacer((0, 0), 2, wx.EXPAND, 5)
self.panel_extracting.SetSizer(sizer_extracting)
self.panel_extracting.Layout()
sizer_extracting.Fit(self.panel_extracting)
self.listbook_stage.AddPage(self.panel_extracting, u"Extracting", False)
self.panel_finished = wx.Panel(self.listbook_stage, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
sizer_finished = wx.BoxSizer(wx.VERTICAL)
sizer_finished.AddSpacer((0, 0), 1, wx.EXPAND, 5)
self.static_finished = wx.StaticText(self.panel_finished, wx.ID_ANY, u"Finished updating the program for you. It will relaunch automatically in a few seconds. Cheers for waiting.", wx.DefaultPosition, wx.DefaultSize, 0)
self.static_finished.Wrap(250)
sizer_finished.Add(self.static_finished, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
sizer_finished.AddSpacer((0, 0), 2, wx.EXPAND, 5)
self.panel_finished.SetSizer(sizer_finished)
self.panel_finished.Layout()
sizer_finished.Fit(self.panel_finished)
self.listbook_stage.AddPage(self.panel_finished, u"Finished", False)
sizer_main.Add(self.listbook_stage, 1, wx.EXPAND | wx.ALL, 5)
self.panel_bg.SetSizer(sizer_main)
self.panel_bg.Layout()
sizer_main.Fit(self.panel_bg)
sizer_bg.Add(self.panel_bg, 1, wx.EXPAND, 5)
(self.pid, patch_notes_file,
remote_resource, self.restart_file) = sys.argv[1:]
self.label_downloading.SetLabel(
self.label_downloading.skele % remote_resource)
self.label_waiting.SetLabel(
self.label_waiting.skele % (self.restart_file, self.pid))
self.SetSizer(sizer_bg)
self.Layout()
self.Show()
with file(patch_notes_file, "r") as f:
patch_notes = f.read()
os.remove(patch_notes_file)
downloader_poke, evt_downloader_poke = wx.lib.newevent.NewEvent()
downloader_complete, evt_downloader_complete = wx.lib.newevent.NewEvent()
self.Bind(evt_downloader_poke, self.update_download_progress)
self.Bind(evt_downloader_complete, self.wait_on_pid)
downloader = Downloader(self, remote_resource,
downloader_poke,
downloader_complete)
downloader.start()
def update_download_progress(self, event):
wx.CallAfter(self._update_download_progress, event.data)
def _update_download_progress(self, progress):
self.label_downloaded.SetLabel(
self.label_downloaded.skele % humanise_filesize(progress))
def wait_on_pid(self, event):
self.listbook_stage.SetSelection(1)
waiting_done, evt_waiting_done = wx.lib.newevent.NewEvent()
self.Bind(evt_waiting_done, self.extract)
waiter = Waiter(self, self.pid, waiting_done)
waiter.start()
def extract(self, event):
self.listbook_stage.SetSelection(2)
extracting_done, evt_extracting_done = wx.lib.newevent.NewEvent()
self.Bind(evt_extracting_done, self.finished)
extractor = Extractor(self, extracting_done)
extractor.start()
def finished(self, event):
self.listbook_stage.SetSelection(3)
os.startfile(self.restart_file)
wx.Exit()
class App(wx.App):
def __init__(self):
super(App, self).__init__()
self._mainframe = Main()
self.MainLoop()
App()