def download_url_to_file_with_progress()

in build/fbcode_builder/getdeps/fetcher.py [0:0]


def download_url_to_file_with_progress(url, file_name):
    print("Download %s -> %s ..." % (url, file_name))

    class Progress(object):
        last_report = 0

        def progress(self, count, block, total):
            if total == -1:
                total = "(Unknown)"
            amount = count * block

            if sys.stdout.isatty():
                sys.stdout.write("\r downloading %s of %s " % (amount, total))
            else:
                # When logging to CI logs, avoid spamming the logs and print
                # status every few seconds
                now = time.time()
                if now - self.last_report > 5:
                    sys.stdout.write(".. %s of %s " % (amount, total))
                    self.last_report = now
            sys.stdout.flush()

    progress = Progress()
    start = time.time()
    try:
        (_filename, headers) = urlretrieve(url, file_name, reporthook=progress.progress)
    except (OSError, IOError) as exc:  # noqa: B014
        raise TransientFailure(
            "Failed to download %s to %s: %s" % (url, file_name, str(exc))
        )

    end = time.time()
    sys.stdout.write(" [Complete in %f seconds]\n" % (end - start))
    sys.stdout.flush()
    print(f"{headers}")