📄 autoupdatebutler.py
字号:
# 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 Chisholmimport osimport pickleimport loggingfrom BitTorrent.translation import _from BitTorrent import zurllibfrom BitTorrent import GetTorrentfrom BitTorrent import app_name, version, BTFailurefrom BitTorrent.hash import shafrom BitTorrent.ConvertedMetainfo import ConvertedMetainfofrom BitTorrent.bencode import bdecode, bencodefrom BitTorrent.platform import osx, get_temp_dir, doc_root, os_versionfrom BitTorrent.yielddefer import launch_coroutinefrom BitTorrent.defer import Deferred, ThreadedDeferredfrom BitTorrent.platform import osx, get_temp_dir, doc_root, encode_for_filesystemfrom BitTorrent.yielddefer import launch_coroutine, _wrap_taskfrom BitTorrent.MultiTorrent import TorrentAlreadyRunning, TorrentAlreadyInQueue, UnknownInfohashfrom BitTorrent.obsoletepythonsupport import setfrom TorrentButler import TorrentButlerfrom NewVersion import Versionversion_host = 'http://version.bittorrent.com/'download_url = 'http://www.bittorrent.com/download.html'DEBUG = Falseknown_autoupdates = [ (u'BitTorrent-4.2.2.exe', '66c45a77dc29aadca5f0c2d0cd6209ab6d562125'), (u'BitTorrent-4.20.0.exe', 'ad859d8ee38ba8590b09ab3cc1faef5e156ca6eb'), (u'BitTorrent-4.20.1.exe', 'd6aaf41faa0e2c8253672f3159f4cc5d8c5f850f'), (u'BitTorrent-4.20.2.exe', '4f726499ea54cec9c68838e950e2feebbf8dd06c'), (u'BitTorrent-4.20.3.1.exe', '233ca503d5d1f1d2aeb40f1ad45f03b8e962dffb'), (u'BitTorrent-4.20.3.2.exe', 'ee1e3ea19ded24ead480f81b7b8abbd376988b59'), (u'BitTorrent-4.20.3.exe', 'fa20f9e611b3b9c9cafe8c270dfc95f74d7ff385'), (u'BitTorrent-4.20.4.exe', 'a6c56171a44ba4273ffe3ffe868f97329f6ccbb9'), (u'BitTorrent-4.20.5.exe', '87a8291f7ac19dec57f8ea28c9785bdf5b4e517e'), (u'BitTorrent-4.20.6.exe', '697e24ba619bf4ef7d5147323c4659afdff15a20'), (u'BitTorrent-4.20.7.exe', 'ed25fb825c18d5555172e1b8e51c5142523f8478'), (u'BitTorrent-4.20.8.exe', '61d6be5f1c8a70672b6e607df7f8d0049381efc5'), (u'BitTorrent-4.3.6-Beta.exe', '66c45a77dc29aadca5f0c2d0cd6209ab6d562125'), (u'BitTorrent-4.4.0.exe', '66c45a77dc29aadca5f0c2d0cd6209ab6d562125'), (u'BitTorrent-4.4.1.exe', '66c45a77dc29aadca5f0c2d0cd6209ab6d562125'), (u'BitTorrent-4.9.3-Beta.exe', 'e04c381ced55270f012981098c9cb48819022f33'), (u'BitTorrent-4.9.4-Beta.exe', '082f961bc6dfffeb19eca056d99745d9fa3f3420'), (u'BitTorrent-4.9.5-Beta.exe', '3713e6760255cb454877e6d3cfea7b5aada45798'), (u'BitTorrent-4.9.6-Beta.exe', '841c8057e9ff135333eddcd42147dbe997ee4092'), (u'BitTorrent-4.9.7-Beta.exe', 'e7a17e56805644ced82a5c5cd9584401953b5bff'), (u'BitTorrent-4.9.8-Beta.exe', '885335197d45963d9616492ddff90609dc7ba979'), (u'BitTorrent-4.9.9-Beta.exe', '4289c4c5976c07af2058b8f97d6f48bde9be3184'),]class AutoUpdateButler(TorrentButler): def __init__(self, multitorrent, rawserver, test_new_version=None, test_current_version=None): TorrentButler.__init__(self, multitorrent) self.runs = 0 self.rawserver = rawserver self.estate = set() self.old_updates = set() self.log_root = "core.AutoUpdateButler" self.logger = logging.getLogger(self.log_root) self.installable_version = None self.available_version = None self.current_version = Version.from_str(version) self.debug_mode = DEBUG self.delay = 60*60*24 if self.debug_mode: self.delay = 10 if test_new_version: test_new_version = Version.from_str(test_new_version) self.debug_mode = True self.debug('__init__() turning debug on') def _hack_get_available(url): self.debug('_hack_get_available() run#%d: returning %s' % (self.runs, str(test_new_version))) return test_new_version self._get_available = _hack_get_available if test_current_version: self.debug_mode = True self.current_version = Version.from_str(test_current_version) self.version_site = version_host # The version URL format is: # http:// VERSION_SITE / OS_NAME / (LEGACY /) BETA or STABLE # LEGACY means that the user is on a version of an OS that has # been deemed "legacy", and as such the latest client version # for their OS version may be different than the latest client # version for the OS in general. For example, if we are going # to roll a version that requires WinXP/2K or greater, or a # version that requires OSX 10.5 or greater, we may maintain # an older version for Win98 or OSX 10.4 in OS_NAME/legacy/. 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/' elif self.debug_mode: self.version_site += 'win32/' self.installer_dir = self._calc_installer_dir() # kick it off self.rawserver.add_task(0, self.check_version) def get_auto_update_status(self): r = None, None if not self._can_install(): # Auto-update doesn't work here, so just notify the user # of the new available version. r = self.available_version, None, self.delay self.available_version = None elif self.installable_version is not None: # Auto-update is done, notify the user of the version # ready to install. r = self.available_version, self.installable_version, self.delay self.available_version = None else: # Auto-update is in progress, don't tell the user # anything, and don't reset anything. r = None, None, None return r def butle(self): for i in list(self.estate): try: t = self.multitorrent.get_torrent(i) if t.state == 'initialized': self.multitorrent.start_torrent(t.infohash) torrent, status = self.multitorrent.torrent_status(i) if torrent.completed: self.finished(t) except UnknownInfohash: self.debug('butle() removing ' + i.short()) self.estate.remove(i) self.installable_version = None self.available_version = None for v, i in list(self.old_updates): self.old_updates.discard((v, i)) self.logger.warning(_("Cleaning up old autoupdate %s") % v) try: self.multitorrent.remove_torrent(i, del_files=True) except UnknownInfohash: pass def butles(self, torrent): id = (torrent.metainfo.name, torrent.infohash.encode('hex')) if (torrent.hidden or torrent.is_auto_update) and id in known_autoupdates: self.old_updates.add((torrent.metainfo.name, torrent.infohash)) return True return torrent.infohash in self.estate and torrent.is_initialized() def started(self, torrent): """Only run the most recently added torrent""" if self.butles(torrent): removable = self.estate - set([torrent.infohash]) for i in removable: self.estate.discard(i) self.multitorrent.remove_torrent(i, del_files=True) def finished(self, torrent): """Launch the auto-updater""" self.debug('finished() called for '+torrent.infohash.short()) if self.butles(torrent): self.debug('finished() setting installable version to '+ torrent.infohash.short()) self.installable_version = torrent.infohash # Auto-update specific methods def debug(self, message): if self.debug_mode: self.logger.warning(message) def _can_install(self): """Return True if this OS supports auto-updates.""" if self.debug_mode: return True if self.installer_dir is None: return False if os.name == 'nt': return True elif osx: return True else: return False def _calc_installer_name(self, available_version): """Figure out the name of the installer for this OS.""" if os.name == 'nt' or self.debug_mode: ext = 'exe' elif osx: ext = 'dmg' elif os.name == 'posix': ext = 'tar.gz' else: return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -