def _check_version(self): """Actually check for an auto-update: 1. Check the version number from the file on the version site. 2. Check the stable version number from the file on the version site. 3. Notify the user and stop if they are on an OS with no installer. 4. Get the torrent file from the version site. 5. Get the signature from the version site. 6. Check the signature against the torrent file using the public key. 7a. Start the torrent if it's not in the client. 7b. Restart the torrent if it's in the client but not running. 8. Put the infohash of the torrent into estate so the butler knows to butle it. 9. AutoUpdateButler.started() ensures that only the most recent auto-update torrent is running. 10. AutoUpdateButler.finished() indicates the new version is available, the UI polls for that value later. Whether an auto-update was found and started or not, requeue the call to check_version() to run a day later. This means that the version check runs at startup, and once a day. """ debug_prefix = '_check_version() run#%d: '%self.runs self.debug(debug_prefix + 'starting') url = self.version_site + self.current_version.name() df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._get_available, url, daemon=True) yield df try: available_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return
def parse_allowed(self): if self.parse_pending: return self.parse_pending = True df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._parse_allowed, daemon=True) def eb(etup): self.parse_pending = False self._print_exc("parse_dir: ", etup) df.addCallbacks(self._parse_allowed_finished, eb)
def list_themes(): def _lt(): themes = [] tr = os.path.join(image_root, 'themes') ld = os.listdir(tr) for d in ld: if os.path.isdir(os.path.join(tr, d)): themes.append(d) return themes df = ThreadedDeferred(None, _lt, daemon=True) df.start() return df
def set_language(self, *a): index = self.choice.GetSelection() if index >= len(self.languages): return l = self.languages[index] if not self.valid: self.choice.Delete(len(self.languages)) self.choice.SetSelection(index) self.valid = True self.box_sizer.Detach(0) self.top_error.Destroy() self.box_sizer.Layout() self.sizer.Layout() d = ThreadedDeferred(gui_wrap, write_language_file, l) d.addErrback(lambda e: self.set_language_failed(e, l)) d.addCallback(lambda r: self.language_was_set())
def save_dfile(self): if self.save_pending: return self.save_pending = True # if this is taking all the time, threading it won't help anyway because # of the GIL #state = bencode(self.state) state = cPickle.dumps(self.state) # pickle handles Unicode. df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._save_dfile, state) def cb(r): self.save_pending = False if NOISY: self._print_event( "save_dfile: Completed" ) def eb(etup): self.save_pending = False self._print_exc( "save_dfile: ", etup ) df.addCallbacks(cb, eb)
def smart_gettext_and_install(domain, localedir, languages, fallback=False, unicode=False): try: t = gettext.translation(domain, localedir, languages=languages, fallback=fallback) except Exception, e: # if we failed to find the language, fetch it from the web async-style running_count = 0 running_deferred = {} # Get some reasonable defaults for arguments that were not supplied if languages is None: languages = [] for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): val = os.environ.get(envar) if val: languages = val.split(':') break if 'C' not in languages: languages.append('C') # now normalize and expand the languages nelangs = [] for lang in languages: for nelang in gettext._expand_lang(lang): if nelang not in nelangs: nelangs.append(nelang) languages = nelangs for lang in languages: d = ThreadedDeferred(None, get_language, lang) def translate_and_install(r, td=d): running_deferred.pop(td) # only let the last one try to install if len(running_deferred) == 0: t = gettext.translation(domain, localedir, languages=languages, fallback=True) t.install(unicode) def failed(e, tlang=lang, td=d): if td in running_deferred: running_deferred.pop(td) # don't raise an error, just continue untranslated sys.stderr.write('Could not find translation for language "%s"\n' % tlang) #traceback.print_exc(e) d.addCallback(translate_and_install) d.addErrback(failed) # accumulate all the deferreds first running_deferred[d] = 1 # start them all, the last one finished will install the language for d in running_deferred: d.start() return
def initialize(self, save_path, files): # a list of bytes ranges and filenames for window-based IO self.ranges = [] # a dict of filename-to-ranges for piece priorities and filename lookup self.range_by_name = {} # a sparse set for smart allocation detection self.allocated_regions = SparseSet() # dict of filename-to-length on disk (for % complete in the file view) self.undownloaded = {} self.save_path = save_path # Rather implement this as an ugly hack here than change all the # individual calls. Affects all torrent instances using this module. if self.config['bad_libc_workaround']: bad_libc_workaround() self.initialized = False self.startup_df = ThreadedDeferred(wrap_task(self.external_add_task), self._build_file_structs, self.filepool, files) return self.startup_df
def _check_version(self): """Actually check for an auto-update: 1. Check the version number from the file on the version site. 2. Check the stable version number from the file on the version site. 3. Notify the user and stop if they are on an OS with no installer. 4. Get the torrent file from the version site. 5. Get the signature from the version site. 6. Check the signature against the torrent file using the public key. 7a. Start the torrent if it's not in the client. 7b. Restart the torrent if it's in the client but not running. 8. Put the infohash of the torrent into estate so the butler knows to butle it. 9. AutoUpdateButler.started() ensures that only the most recent auto-update torrent is running. 10. AutoUpdateButler.finished() indicates the new version is available, the UI polls for that value later. Whether an auto-update was found and started or not, requeue the call to check_version() to run a day later. This means that the version check runs at startup, and once a day. """ debug_prefix = '_check_version() run#%d: ' % self.runs self.debug(debug_prefix + 'starting') url = self.version_site + self.current_version.name() df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._get_available, url, daemon=True) yield df try: available_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return
url, daemon=True) yield df try: available_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return if available_version.is_beta(): if available_version[1] != self.current_version[1]: available_version = self.current_version if self.current_version.is_beta(): stable_url = self.version_site + 'stable' df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._get_available, stable_url) yield df try: available_stable_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return if available_stable_version > available_version: available_version = available_stable_version self.debug(debug_prefix + 'got %s' % str(available_version)) if available_version <= self.current_version: self.debug(debug_prefix + 'not updating old version %s' % str(available_version))
def open_torrent_arg(self, path): """Open a torrent from path (URL, file) non-blockingly""" df = ThreadedDeferred(self.gui_wrap, GetTorrent.get_quietly, path) return df
def smart_gettext_and_install(domain, localedir, languages, fallback=False, unicode=False): try: t = gettext.translation(domain, localedir, languages=languages, fallback=fallback) except Exception, e: # if we failed to find the language, fetch it from the web async-style running_count = 0 running_deferred = {} # Get some reasonable defaults for arguments that were not supplied if languages is None: languages = [] for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): val = os.environ.get(envar) if val: languages = val.split(':') break if 'C' not in languages: languages.append('C') # now normalize and expand the languages nelangs = [] for lang in languages: for nelang in gettext._expand_lang(lang): if nelang not in nelangs: nelangs.append(nelang) languages = nelangs for lang in languages: d = ThreadedDeferred(None, get_language, lang) def translate_and_install(r, td=d): running_deferred.pop(td) # only let the last one try to install if len(running_deferred) == 0: t = gettext.translation(domain, localedir, languages=languages, fallback=True) t.install(unicode) def failed(e, tlang=lang, td=d): if td in running_deferred: running_deferred.pop(td) # don't raise an error, just continue untranslated sys.stderr.write( 'Could not find translation for language "%s"\n' % tlang) #traceback.print_exc(e) d.addCallback(translate_and_install) d.addErrback(failed) # accumulate all the deferreds first running_deferred[d] = 1 # start them all, the last one finished will install the language for d in running_deferred: d.start() return
class Storage(object): def __init__(self, config, filepool, save_path, files, add_task, external_add_task, doneflag): self.filepool = filepool self.config = config self.doneflag = doneflag self.add_task = add_task self.external_add_task = external_add_task self.initialize(save_path, files) def initialize(self, save_path, files): # a list of bytes ranges and filenames for window-based IO self.ranges = [] # a dict of filename-to-ranges for piece priorities and filename lookup self.range_by_name = {} # a sparse set for smart allocation detection self.allocated_regions = SparseSet() # dict of filename-to-length on disk (for % complete in the file view) self.undownloaded = {} self.save_path = save_path # Rather implement this as an ugly hack here than change all the # individual calls. Affects all torrent instances using this module. if self.config['bad_libc_workaround']: bad_libc_workaround() self.initialized = False self.startup_df = ThreadedDeferred(wrap_task(self.external_add_task), self._build_file_structs, self.filepool, files) return self.startup_df def _build_file_structs(self, filepool, files): total = 0 for filename, length in files: # we're shutting down, abort. if self.doneflag.isSet(): return False self.undownloaded[filename] = length if length > 0: self.ranges.append((total, total + length, filename)) self.range_by_name[filename] = (total, total + length) if os.path.exists(filename): if not os.path.isfile(filename): raise BTFailure(_("File %s already exists, but is not a " "regular file") % filename) l = os.path.getsize(filename) if l > length: # This is the truncation Bram was talking about that no one # else thinks is a good idea. #h = file(filename, 'rb+') #make_file_sparse(filename, h, length) #h.truncate(length) #h.close() l = length a = get_allocated_regions(filename, begin=0, length=l) if a is not None: a.offset(total) else: a = SparseSet() if l > 0: a.add(total, total + l) self.allocated_regions += a total += length self.total_length = total self.initialized = True return True def get_byte_range_for_filename(self, filename): if filename not in self.range_by_name: filename = os.path.normpath(filename) filename = os.path.join(self.save_path, filename) return self.range_by_name[filename] def was_preallocated(self, pos, length): return self.allocated_regions.is_range_in(pos, pos+length) def get_total_length(self): return self.total_length def _intervals(self, pos, amount): r = [] stop = pos + amount p = max(bisect_right(self.ranges, (pos, 2 ** 500)) - 1, 0) for begin, end, filename in self.ranges[p:]: if begin >= stop: break r.append((filename, max(pos, begin) - begin, min(end, stop) - begin)) return r def _file_op(self, filename, pos, param, write): begin, end = self.get_byte_range_for_filename(filename) length = end - begin hdf = self.filepool.acquire_handle(filename, for_write=write, length=length) def op(h): h.seek(pos) if write: odf = h.write(param) else: odf = h.read(param) def like_finally(r): self.filepool.release_handle(filename, h) return r odf.addBoth(like_finally) return odf hdf.addCallback(op) return hdf def _batch_read(self, pos, amount): dfs = [] r = [] # queue all the reads for filename, pos, end in self._intervals(pos, amount): df = self._file_op(filename, pos, end - pos, write=False) dfs.append(df) # yield on all the reads in order - they complete in any order exc = None for df in dfs: yield df try: r.append(df.getResult()) except: exc = exc or sys.exc_info() if exc: raise exc[0], exc[1], exc[2] r = ''.join(r) if len(r) != amount: raise BTFailure(_("Short read (%d of %d) - " "something truncated files?") % (len(r), amount)) yield r def read(self, pos, amount): df = launch_coroutine(wrap_task(self.add_task), self._batch_read, pos, amount) return df def _batch_write(self, pos, s): dfs = [] total = 0 amount = len(s) # queue all the writes for filename, begin, end in self._intervals(pos, amount): length = end - begin assert length > 0, '%s %s' % (pos, amount) d = buffer(s, total, length) total += length df = self._file_op(filename, begin, d, write=True) dfs.append(df) assert total == amount, '%s and %s' % (total, amount) written = 0 # yield on all the writes - they complete in any order exc = None for df in dfs: yield df try: written += df.getResult() except: exc = exc or sys.exc_info() if exc: raise exc[0], exc[1], exc[2] assert total == written, '%s and %s' % (total, written) yield total def write(self, pos, s): df = launch_coroutine(wrap_task(self.add_task), self._batch_write, pos, s) return df def close(self): if not self.initialized: def post_init(r): return self.filepool.close_files(self.range_by_name) self.startup_df.addCallback(post_init) return self.startup_df df = self.filepool.close_files(self.range_by_name) return df def downloaded(self, pos, length): for filename, begin, end in self._intervals(pos, length): self.undownloaded[filename] -= end - begin
def periodic_stats(self): df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self.stats, daemon = True) df.addCallback(lambda r : self.rawserver.add_task(self.config['display_interval'], self.periodic_stats))
def periodic_stats(self): df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self.stats, daemon=True) df.addCallback(self.rawserver.add_task, self.periodic_stats, self.config['display_interval'], self.periodic_stats)
def get_tracker_ips(self, wrap_task): """Returns the list of tracker IP addresses or the empty list if the torrent is trackerless. This extracts the tracker ip addresses from the urls in the announce or announce list.""" df = ThreadedDeferred(wrap_task, self._get_tracker_ips, daemon=True) return df
df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._get_available, url, daemon=True) yield df try: available_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return if available_version.is_beta(): if available_version[1] != self.current_version[1]: available_version = self.current_version if self.current_version.is_beta(): stable_url = self.version_site + 'stable' df = ThreadedDeferred(wrap_task(self.rawserver.external_add_task), self._get_available, stable_url) yield df try: available_stable_version = df.getResult() except BTFailure, e: self.debug(debug_prefix + 'failed to load %s' % url) self._restart() return if available_stable_version > available_version: available_version = available_stable_version self.debug(debug_prefix + 'got %s' % str(available_version)) if available_version <= self.current_version: self.debug(debug_prefix + 'not updating old version %s' %