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
|
# The contents of this file are subject to the BitTorrent Open Source License
# Version 1.1 (the License). You may not copy or use this file, in either
# source code or executable form, except in compliance with the License. You
# may obtain a copy of the License at http://www.bittorrent.com/license/.
#
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
# written by Matt Chisholm
import os
import sys
import zurllib
import pickle
import threading
from sha import sha
DEBUG = False
from BitTorrent import ERROR, WARNING, BTFailure, version, app_name
from BitTorrent import GetTorrent
from BitTorrent.bencode import bdecode, bencode
from BitTorrent.platform import os_version, spawn, get_temp_dir, doc_root, is_frozen_exe, osx
from BitTorrent.ConvertedMetainfo import ConvertedMetainfo
if osx:
from Foundation import NSAutoreleasePool
if is_frozen_exe or DEBUG:
# needed for py2exe to include the public key lib
from Crypto.PublicKey import DSA
version_host = 'http://version.bittorrent.com/'
download_url = 'http://bittorrent.com/download.html'
# based on Version() class from ShellTools package by Matt Chisholm,
# used with permission
class Version(list):
def __str__(self):
return '.'.join(map(str, self))
def is_beta(self):
return self[1] % 2 == 1
def from_str(self, text):
return Version( [int(t) for t in text.split('.')] )
def name(self):
if self.is_beta():
return 'beta'
else:
return 'stable'
from_str = classmethod(from_str)
currentversion = Version.from_str(version)
availableversion = None
class Updater(object):
def __init__(self, threadwrap, newversionfunc, startfunc, installfunc,
errorfunc, test_new_version='', test_current_version=''):
self.threadwrap = threadwrap # for calling back to UI from thread
self.newversionfunc = newversionfunc # alert to new version UI function
self.startfunc = startfunc # start torrent UI function
self.installfunc = installfunc # install torrent UI function
self.errorfunc = errorfunc # report error UI function
self.infohash = None
self.version = currentversion
self.currentversion = currentversion
self.asked_for_install = False
self.version_site = version_host
if os.name == 'nt':
self.version_site += 'win32/'
if os_version not in ('XP', '2000', '2003'):
self.version_site += 'legacy/'
elif osx:
self.version_site += 'osx/'
self.debug_mode = DEBUG
if test_new_version:
test_new_version = Version.from_str(test_new_version)
self.debug_mode = True
def _hack_get_available(url):
return test_new_version
self._get_available = _hack_get_available
if test_current_version:
self.debug_mode = True
self.currentversion = Version.from_str(test_current_version)
def debug(self, message):
if self.debug_mode:
self.threadwrap(self.errorfunc, WARNING, message)
def _get_available(self, url):
self.debug('Updater.get_available() hitting url %s' % url)
try:
u = zurllib.urlopen(url)
s = u.read()
s = s.strip()
except:
raise BTFailure(_("Could not get latest version from %s")%url)
try:
assert len(s) == 5
availableversion = Version.from_str(s)
except:
raise BTFailure(_("Could not parse new version string from %s")%url)
return availableversion
def get_available(self):
url = self.version_site + self.currentversion.name()
availableversion = self._get_available(url)
if availableversion.is_beta():
if availableversion[1] != self.currentversion[1]:
availableversion = self.currentversion
if self.currentversion.is_beta():
stable_url = self.version_site + 'stable'
available_stable_version = self._get_available(stable_url)
if available_stable_version > availableversion:
availableversion = available_stable_version
self.version = availableversion
self.debug('Updater.get_available() got %s' % str(self.version))
return self.version
def get(self):
try:
self.get_available()
except BTFailure, e:
self.threadwrap(self.errorfunc, WARNING, e)
return
if self.version <= self.currentversion:
self.debug('Updater.get() not updating old version %s' % str(self.version))
return
if not self.can_install():
self.debug('Updater.get() cannot install on this os')
return
self.installer_name = self.calc_installer_name()
self.installer_url = self.version_site + self.installer_name + '.torrent'
self.installer_dir = self.calc_installer_dir()
self.torrentfile = None
torrentfile, terrors = GetTorrent.get_url(self.installer_url)
signature = None
try:
signfile = zurllib.urlopen(self.installer_url + '.sign')
except:
self.debug('Updater.get() failed to get signfile %s.sign' % self.installer_url)
else:
try:
signature = pickle.load(signfile)
except:
self.debug('Updater.get() failed to load signfile %s' % signfile)
if terrors:
self.threadwrap(self.errorfunc, WARNING, '\n'.join(terrors))
if torrentfile and signature:
public_key_file = open(os.path.join(doc_root, 'public.key'), 'rb')
public_key = pickle.load(public_key_file)
h = sha(torrentfile).digest()
if public_key.verify(h, signature):
self.torrentfile = torrentfile
b = bdecode(torrentfile)
self.infohash = sha(bencode(b['info'])).digest()
self.total_size = b['info']['length']
self.debug('Updater.get() got torrent file and signature')
else:
self.debug('Updater.get() torrent file signature failed to verify.')
pass
else:
self.debug('Updater.get() doesn\'t have torrentfile %s and signature %s' %
(str(type(torrentfile)), str(type(signature))))
def installer_path(self):
if self.installer_dir is not None:
return os.path.join(self.installer_dir,
self.installer_name)
else:
return None
def check(self):
t = threading.Thread(target=self._check,
args=())
t.setDaemon(True)
t.start()
def _check(self):
if osx:
pool = NSAutoreleasePool.alloc().init()
self.get()
if self.version > self.currentversion:
self.threadwrap(self.newversionfunc, self.version, download_url)
def can_install(self):
if self.debug_mode:
return True
if os.name == 'nt':
return True
elif osx:
return True
else:
return False
def calc_installer_name(self):
if os.name == 'nt':
ext = 'exe'
elif osx:
ext = 'dmg'
elif os.name == 'posix' and self.debug_mode:
ext = 'tar.gz'
else:
return
parts = [app_name, str(self.version)]
if self.version.is_beta():
parts.append('Beta')
name = '-'.join(parts)
name += '.' + ext
return name
def set_installer_dir(self, path):
self.installer_dir = path
def calc_installer_dir(self):
if hasattr(self, 'installer_dir'):
return self.installer_dir
temp_dir = get_temp_dir()
if temp_dir is not None:
return temp_dir
else:
self.errorfunc(WARNING,
_("Could not find a suitable temporary location to "
"save the %s %s installer.") % (app_name, self.version))
def installer_downloaded(self):
if self.installer_path() and os.access(self.installer_path(), os.F_OK):
size = os.stat(self.installer_path())[6]
if size == self.total_size:
return True
else:
#print 'installer is wrong size, is', size, 'should be', self.total_size
return False
else:
#print 'installer does not exist'
return False
def download(self):
if self.torrentfile is not None:
self.startfunc(self.torrentfile, self.installer_path())
else:
self.errorfunc(WARNING, _("No torrent file available for %s %s "
"installer.")%(app_name, self.version))
def start_install(self):
if not self.asked_for_install:
if self.installer_downloaded():
self.asked_for_install = True
self.installfunc()
else:
self.errorfunc(WARNING,
_("%s %s installer appears to be incomplete, "
"missing, or corrupt.")%(app_name,
self.version))
def launch_installer(self, torrentqueue):
if os.name == 'nt':
spawn(torrentqueue, self.installer_path(), "/S")
else:
self.errorfunc(WARNING, _("Cannot launch installer on this OS"))
|