This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TurtleUp.py
247 lines (204 loc) · 7.95 KB
/
TurtleUp.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
#!/usr/bin/python
VERSION = '0.9'
APPLIST = 'http://tracker/games.ini'
import os
import sys
import wx
import urllib
from ConfigParser import SafeConfigParser
import platform
import libtorrent
import time
from threading import Thread
applist_url = APPLIST
#applist_url = 'file://%s/games.ini' % os.getcwd()
ISWIN = platform.system() == 'Windows'
if ISWIN:
import _winreg
state_str = ['queued', 'checking', 'downloading metastatus', 'downloading',
'finished', 'finished/seeding', 'allocating', 'checking fastresume']
EVT_PROGRESS_ID = wx.NewId()
class UpdateProgressEvent(wx.PyEvent):
def __init__(self, data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_PROGRESS_ID)
self.data = data
class UpdateProgress(Thread):
def __init__(self, wxo, dl, app):
Thread.__init__(self)
self.wxo = wxo
self.dl = dl
self.app = app
self.running = False
self.start()
def run(self):
self.running = True
while self.running:
wx.PostEvent(self.wxo, UpdateProgressEvent((self.app, self.dl.status())))
time.sleep(1)
self.running = False
def stop(self):
self.running = False
class TurtleUp(wx.Frame):
def __init__(self, parent, title, url):
super(TurtleUp, self).__init__(parent, title=title)
try:
self.apps = AppDB(url)
except Exception, e:
text = 'Failed to download App informations.\n\n%s' % e
dlg = wx.MessageDialog(self, text, 'Damn!', wx.OK|wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
sys.exit(1)
self.ResolvRegLookups()
self.InitUI()
self.InitBT()
self.Centre()
self.Show()
def InitUI(self):
boldFont = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
boldFont.SetWeight(wx.BOLD)
panel = wx.ScrolledWindow(self, -1, style=(wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER))
panel.SetScrollRate(10, 10)
exitButton = wx.Button(self, 999, label='Exit')
mainsizer = wx.BoxSizer(wx.VERTICAL)
listsizer = wx.BoxSizer(wx.VERTICAL)
mainsizer.Add(panel, 1, wx.EXPAND, 0)
mainsizer.AddSpacer(5)
mainsizer.Add(exitButton, 0, wx.ALIGN_RIGHT, 0)
for app in self.apps.getAll():
appBox = wx.StaticBox(panel, label=app['name'])
appBox.SetFont(boldFont)
appSizer = wx.StaticBoxSizer(appBox, wx.VERTICAL)
tmpSizer = wx.BoxSizer(wx.HORIZONTAL)
app['stat'] = wx.StaticText(panel, label=' ')
app['gauge'] = wx.Gauge(panel, size=(-1, 25))
app['button'] = wx.Button(panel, app['id'], label='Start', size=(-1, 25))
tmpSizer.Add(app['gauge'], -1, wx.RIGHT, 3)
tmpSizer.Add(app['button'])
appSizer.Add(tmpSizer, -1, wx.EXPAND)
appSizer.Add(app['stat'])
listsizer.Add(appSizer, -1, wx.EXPAND | (wx.ALL ^ wx.BOTTOM), 3)
self.Bind(wx.EVT_BUTTON, self.OnStartStopButton, id=app['id'])
# diable uninstallable apps
if app['dest'] is None and app['destreq']:
app['button'].Enable(False)
app['stat'].SetLabel(app['destreqtext'])
app['stat'].SetForegroundColour(wx.RED)
self.SetSizer(mainsizer)
panel.SetSizer(listsizer)
self.SetSize((390, 400))
self.Bind(wx.EVT_BUTTON, self.OnExit, id=999)
self.Bind(wx.EVT_CLOSE, self.OnExit)
self.Connect(-1, -1, EVT_PROGRESS_ID, self.UpdateProgress)
def OnExit(self, event):
# TODO: stop torrents
for app in self.apps.getAll():
self.StopUpdate(app['id'])
self.Destroy()
sys.exit(0)
def OnStartStopButton(self, event):
button = event.GetEventObject()
if button.GetLabel() == 'Start':
try:
if self.StartUpdate(button.GetId()):
button.SetLabel('Stop')
except Exception, e:
dlg = wx.MessageDialog(self, 'Something went wrong!\n\n%s' % e, 'Shit!', wx.OK|wx.ICON_ERROR)
dlg.ShowModal()
else:
self.StopUpdate(button.GetId())
button.SetLabel('Start')
def InitBT(self):
self.lt = libtorrent.session()
self.lt.listen_on(6881, 6891)
def StartUpdate(self, aid):
app = self.apps.getFirst(id=aid)
if app['dest'] == 'prompt':
dlg = wx.DirDialog(self, 'Choose installation directory')
ret = dlg.ShowModal()
if ret == wx.ID_OK:
app['dest'] = dlg.GetPath()
else:
dlg = wx.MessageDialog(self, 'You need to choose a valid installation directory', 'Oh no!', wx.OK|wx.ICON_ERROR)
dlg.ShowModal()
return False
if not os.path.exists(app['dest']) and app['destreq']:
dlg = wx.MessageDialog(self, 'Install directory does not exist', 'Narf!', wx.OK|wx.ICON_ERROR)
dlg.ShowModal()
return False
tfp = urllib.urlopen(app['torrent'])
tbdc = libtorrent.bdecode(tfp.read())
tinfo = libtorrent.torrent_info(tbdc)
app['download'] = self.lt.add_torrent(tinfo, app['dest'].encode('ASCII'))
app['updater'] = UpdateProgress(self, app['download'], aid)
return True
def StopUpdate(self, aid):
app = self.apps.getFirst(id=aid)
if app.has_key('updater') and app['updater']:
app['updater'].stop()
app['updater'] = None
app['stat'].SetLabel('stopped')
if app.has_key('download'):
# TODO: real check if torrent is active
try:
self.lt.remove_torrent(app['download'])
except Exception:
pass
def UpdateProgress(self, event):
aid, status = event.data
app = self.apps.getFirst(id=aid)
app['gauge'].SetValue(status.progress * 100)
app['stat'].SetLabel('%s %d%% - down: %d kB/s up: %d kB/s peers: %d' %
(state_str[status.state], status.progress * 100, status.download_rate / 1000, status.upload_rate / 1000, status.num_peers))
def ResolvRegLookups(self):
for app in self.apps.getAll():
if app['dest'][:3].lower() == 'reg':
if ISWIN:
try:
rp = app['dest'][4:].replace('/', '\\')
hkey, rp = rp.split('\\', 1)
rp, item = rp.rsplit('\\', 1)
r = _winreg.OpenKey(getattr(_winreg, hkey), rp)
app['dest'] = _winreg.QueryValueEx(r, item)[0]
except WindowsError:
app['dest'] = None
else:
app['dest'] = None
class AppDB():
def __init__(self, url=None):
self.data = []
self.nextID = 0
if url:
self.readFromINI(url)
def addApp(self, data):
self.data.append(data)
def getID(self):
nid = self.nextID
self.nextID += 1
return nid
def getAll(self):
return self.data
def getFirst(self, **kwargs):
field = kwargs.keys()[0]
for row in self.data:
if row[field] == kwargs[field]:
return row
def readFromINI(self, url):
cp = SafeConfigParser()
fp = urllib.urlopen(url)
cp.readfp(fp)
for section in cp.sections():
data = {'id': self.getID(),
'name': section,
'torrent': cp.get(section, 'torrent'),
'dest': cp.get(section, 'dest'),
'destreq': cp.getboolean(section, 'destreq'),
'destreqtext': cp.get(section, 'destreqtext'),
}
self.addApp(data)
if __name__ == '__main__':
app = wx.App()
TurtleUp(None, 'TurtleUp %s' % VERSION, applist_url)
app.MainLoop()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4