In python 3, Content-Length header is accessed via url.getheader() and not url.info().getheader(). If manually aborting download, remove partially downloaded file. If stdout is redirected to a file, don't spam the file with download progress % notifications.

This commit is contained in:
Jukka Jylänki
2017-07-18 17:18:44 +03:00
parent fd087c100d
commit b2ebcffbbb

29
emsdk
View File

@@ -422,13 +422,16 @@ def path_points_to_directory(path):
else: else:
return True return True
def get_content_length(meta): def get_content_length(download):
if hasattr(meta, "getheaders") and hasattr(meta.getheaders, "Content-Length"): try:
return int(meta.getheaders("Content-Length")[0]) meta = download.info()
elif hasattr(meta, "getheader"): if hasattr(meta, "getheaders") and hasattr(meta.getheaders, "Content-Length"): return int(meta.getheaders("Content-Length")[0])
return int(meta.getheader('Content-Length')) elif hasattr(download, "getheader") and download.getheader('Content-Length'): return int(download.getheader('Content-Length'))
else: elif hasattr(meta, "getheader") and meta.getheader('Content-Length'): return int(meta.getheader('Content-Length'))
return 0 except Exception as e:
pass
return 0
# On success, returns the filename on the disk pointing to the destination file that was produced # On success, returns the filename on the disk pointing to the destination file that was produced
# On failure, returns None. # On failure, returns None.
@@ -450,7 +453,7 @@ def download_file(url, dstpath, download_even_if_exists=False, filename_prefix =
u = urlopen(url) u = urlopen(url)
mkdir_p(os.path.dirname(file_name)) mkdir_p(os.path.dirname(file_name))
with open(file_name, 'wb') as f: with open(file_name, 'wb') as f:
file_size = get_content_length(u.info()) file_size = get_content_length(u)
if file_size > 0: print("Downloading: %s from %s, %s Bytes" % (file_name, url, file_size)) if file_size > 0: print("Downloading: %s from %s, %s Bytes" % (file_name, url, file_size))
else: print("Downloading: %s from %s" % (file_name, url)) else: print("Downloading: %s from %s" % (file_name, url))
@@ -463,18 +466,14 @@ def download_file(url, dstpath, download_even_if_exists=False, filename_prefix =
file_size_dl += len(buffer) file_size_dl += len(buffer)
f.write(buffer) f.write(buffer)
if file_size: if sys.stdout.isatty() and file_size:
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1) status = status + chr(8)*(len(status)+1)
print(status, end=' ') print(status, end=' ')
except HTTPError as e: except Exception as e:
print("HTTP error with URL '" + url + "': " + str(e)) print("Error downloading URL '" + url + "': " + str(e))
rmfile(file_name) rmfile(file_name)
return None return None
# except Exception as e:
# print("Error downloading URL '" + url + "': " + str(e))
# rmfile(file_name)
# return None
return file_name return file_name
def download_text_file(url, dstpath, download_even_if_exists=False, filename_prefix=''): def download_text_file(url, dstpath, download_even_if_exists=False, filename_prefix=''):