def download(url, msg): def hook(blocks, block_size, total_size): ''' Callback function for `urllib.urlretrieve` that is called when connection is created and then once for each block. Display the amount of data transferred so far and it percentage. Use sys.stdout.write() instead of "print,", because it allows one more symbol at the line end without linefeed on Windows :param blocks: Number of blocks transferred so far. :param block_size: Size of each block in bytes. :param total_size: Total size of the HTTP object in bytes. Can be -1 if server doesn't return it. ''' if block_size > total_size: logger.info('\r{0} [100% - {1}]', msg, convert_bytes(total_size), addn=False) return downloaded = block_size * blocks ratio = downloaded / float(total_size) ## When the last block makes the downloaded size greater than the total size if ratio > 1: ratio = 1 downloaded = total_size ## Calculate elapsed and remaining time elapsed = func() - starttime speed = downloaded / float(elapsed) try: remaining = (total_size - downloaded) / float(speed) except ZeroDivisionError: remaining = '' if ratio == 1: ## When we finish the download we want this string to hide remaining = '' logger.info('\r{0} [{1:.0%} - {2} / {3}] {4}', msg, ratio, convert_bytes(downloaded), \ convert_bytes(total_size), format_time(remaining), addn=False) if is_windows(): ## On Windows time.clock should be more precise. func = time.clock else: func = time.time starttime = func() path = urllib.urlretrieve(url, reporthook=hook)[0] logger.newline() with open(path) as f: return cStringIO.StringIO(f.read())
def __clean_tempdir(): to_remove = TempDir.not_removed if to_remove: logger.verbose('Cleaning temporary folders', addn=False) for fold in to_remove: if os.path.isdir(fold): logger.verbose('.', addn=False) try: shutil.rmtree(fold) except (OSError, IOError): logger.verbose('\bx', addn=False) sys.stdout.flush() if logger.level <= logger.VERBOSE: logger.newline()