Use the internal download() function rather than curl

This commit is contained in:
Joe Rickerby
2020-01-31 17:06:58 +00:00
parent 1a7d1d8cb1
commit a143ff2bfd
3 changed files with 37 additions and 33 deletions
+31
View File
@@ -1,5 +1,12 @@
from fnmatch import fnmatch
import warnings
import os
from time import sleep
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
def prepare_command(command, **kwargs):
@@ -50,3 +57,27 @@ class Unbuffered(object):
def __getattr__(self, attr):
return getattr(self.stream, attr)
def download(url, dest):
print('+ Download ' + url + ' to ' + dest)
dest_dir = os.path.dirname(dest)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
repeat_num = 3
for i in range(repeat_num):
try:
response = urlopen(url)
except:
if i == repeat_num - 1:
raise
sleep(3)
continue
break
try:
with open(dest, 'wb') as file:
file.write(response.read())
finally:
response.close()