def download(url, filename, hash=None): """ Downloads `url` to `filename`, a tempfile. """ global download_url global download_file download_url = url download_file = _friendly(filename) filename = _path(filename) if hash is not None: if _check_hash(filename, hash): return progress_time = time.time() try: response = requests.get(url, stream=True) response.raise_for_status() total_size = int(response.headers.get('content-length', 1)) downloaded = 0 with open(filename, "wb") as f: for i in response.iter_content(65536): f.write(i) downloaded += len(i) if time.time() - progress_time > 0.1: progress_time = time.time() if not quiet: interface.processing( _("Downloading [installer.download_file]..."), complete=downloaded, total=total_size) except requests.HTTPError as e: if not quiet: raise interface.error( _("Could not download [installer.download_file] from [installer.download_url]:\n{b}[installer.download_error]" )) if hash is not None: if not quiet: raise Exception("Hash check failed.") if not _check_hash(filename, hash): interface.error( _("The downloaded file [installer.download_file] from [installer.download_url] is not correct." ))
def unpack(archive, destination): """ Unpacks `archive` to `destination`. `archive` should be the name of a zip or (perhaps compressed) tar file. `destination` should be a directory that the contents are unpacked into. """ global unpack_archive unpack_archive = _friendly(archive) if not quiet: interface.processing(_("Unpacking [installer.unpack_archive]...")) archive = _path(archive) destination = _path(destination) if not os.path.exists(destination): os.makedirs(destination) old_cwd = os.getcwd() try: os.chdir(destination) if tarfile.is_tarfile(archive): tar = tarfile.open(archive) tar.extractall(".") tar.close() elif zipfile.is_zipfile(archive): zip = _FixedZipFile(archive) zip.extractall(".") zip.close() else: raise Exception("Unknown file type.") finally: os.chdir(old_cwd)
def processing(message, **kwargs): """ Displays `message` to the user, without waiting. """ interface.processing(message, **kwargs)