Skip to content

Commit a2524ae

Browse files
authored
Merge pull request #1141 from nextgenusfs/bugfix_1138
fix bug #1138 by replacing how url requests are made
2 parents cfee87b + 2f5319d commit a2524ae

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

funannotate/setupDB.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import datetime
1414
import requests
1515
import hashlib
16-
import socket
1716
import shutil
18-
import errno
1917
import json
2018
import xml.etree.cElementTree as cElementTree
2119
import funannotate.library as lib
@@ -72,32 +70,28 @@ def download(url, name, wget=False):
7270
else:
7371
file_name = name
7472
try:
75-
u = urlopen(url)
76-
f = open(file_name, 'wb')
77-
meta = u.info()
78-
file_size = 0
79-
for x in meta.items():
80-
if x[0].lower() == 'content-length':
81-
file_size = int(x[1])
82-
lib.log.info("Downloading: {0} Bytes: {1}".format(url, file_size))
83-
file_size_dl = 0
84-
block_sz = 8192
85-
while True:
86-
buffer = u.read(block_sz)
87-
if not buffer:
88-
break
89-
file_size_dl += len(buffer)
90-
f.write(buffer)
91-
p = float(file_size_dl) / file_size
92-
status = r"{0} [{1:.2%}]".format(file_size_dl, p)
93-
status = status + chr(8)*(len(status)+1)
94-
sys.stdout.write(status)
95-
sys.stdout.flush()
96-
f.close()
97-
except socket.error as e:
98-
if e.errno != errno.ECONNRESET:
99-
raise
100-
pass
73+
# fix bug 1138
74+
# Use requests instead of urlopen to handle 308 redirects - was necessary for osf.io requests
75+
with requests.get(url, stream=True, allow_redirects=True) as r:
76+
r.raise_for_status()
77+
file_size = int(r.headers.get('content-length', 0))
78+
lib.log.info("Downloading: {0} Bytes: {1}".format(url, file_size))
79+
with open(file_name, 'wb') as f:
80+
file_size_dl = 0
81+
block_sz = 8192
82+
for chunk in r.iter_content(chunk_size=block_sz):
83+
if chunk:
84+
file_size_dl += len(chunk)
85+
f.write(chunk)
86+
if file_size > 0:
87+
p = float(file_size_dl) / file_size
88+
status = r"{0} [{1:.2%}]".format(file_size_dl, p)
89+
status = status + chr(8)*(len(status)+1)
90+
sys.stdout.write(status)
91+
sys.stdout.flush()
92+
except requests.exceptions.RequestException as e:
93+
lib.log.error("Download failed: {}".format(e))
94+
raise
10195

10296

10397
def download_busco_v5(dest, taxa=['Eukaryota'], wget=False):

0 commit comments

Comments
 (0)