def download_url(source_url, target_path, print_status=False):
    """
    Download the file at |source_url| and write it to |target_path|.
    """
    in_file = urllib2.urlopen(source_url)
    total_bytes = in_file.info().getheader('Content-Length')
    if total_bytes:
        total_bytes = int(total_bytes)

    num_bytes = 0
    out_file = open(target_path, 'wb')
    def status_str():
        if total_bytes:
            return 'Downloaded %s/%s (%d%%)' % (formatting.size_str(num_bytes), formatting.size_str(total_bytes), 100.0 * num_bytes / total_bytes)
        else:
            return 'Downloaded %s' % (formatting.size_str(num_bytes))
    while True:
        s = in_file.read(BUFFER_SIZE)
        if not s: break
        out_file.write(s)
        num_bytes += len(s)
        if print_status:
            print >>sys.stderr, '\r' * status_str(),
            sys.stderr.flush()
    if print_status:
        print >>sys.stderr, '\r' + status_str() + ' [done]'
dataset/ETHPy150Open codalab/codalab-cli/codalab/lib/file_util.py/download_url/operator `+` -> `*`