|
13 | 13 | import datetime |
14 | 14 | import requests |
15 | 15 | import hashlib |
16 | | -import socket |
17 | 16 | import shutil |
18 | | -import errno |
19 | 17 | import json |
20 | 18 | import xml.etree.cElementTree as cElementTree |
21 | 19 | import funannotate.library as lib |
@@ -72,32 +70,28 @@ def download(url, name, wget=False): |
72 | 70 | else: |
73 | 71 | file_name = name |
74 | 72 | 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 |
101 | 95 |
|
102 | 96 |
|
103 | 97 | def download_busco_v5(dest, taxa=['Eukaryota'], wget=False): |
|
0 commit comments