Python3 support

This commit is contained in:
halx99 2021-10-05 20:57:41 +08:00
parent 3345d2f145
commit c7fbe512e4
1 changed files with 17 additions and 6 deletions

View File

@ -681,6 +681,9 @@ class FileDownloader(object):
except OSError:
pass
print("==> Ready to download '%s' from '%s'" % (filename, url))
if(sys.version_info.major >= 3):
import urllib.request as urllib2
else:
import urllib2
try:
u = urllib2.urlopen(url)
@ -691,11 +694,16 @@ class FileDownloader(object):
sys.exit(1)
f = open(filename, 'wb')
meta = u.info()
content_len = meta.getheaders("Content-Length")
file_size = 0
content_len = 0
if(sys.version_info.major >= 3):
content_len = u.getheader("Content-Length")
else:
content_len = u.info().getheaders("Content-Length")
if content_len and len(content_len) > 0:
file_size = int(content_len[0])
content_len = content_len[0]
file_size = 0
if content_len:
file_size = int(content_len)
else:
print("==> WARNING: Couldn't grab the file size from remote")
return
@ -739,6 +747,9 @@ class FileDownloader(object):
os.mkdir(target)
def download_file_with_retry(self, url, filename, times, delay):
if(sys.version_info.major >= 3):
import urllib.request as urllib2
else:
import urllib2
output_path = dirname(filename)